0

I have the following working MWE file:

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}
%
\begin{document}
\begin{table}[!hbt]
\renewcommand{\arraystretch}{1.5}%
\centering%
\setlength{\fboxsep}{0pt}%
\arrayrulecolor{blue}%
\colorbox{lightgray}{%
\begin{tabular}{l l}
\toprule%
Column 1 & Column 2\tabularnewline% Unchanging column headers
\midrule%
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
\bottomrule%
\end{tabular}}
\end{table}
\end{document}

I would like to extract the static portions of this setup into either a new environment or a new command so that only the changing content lines will be passed to the new environment, say mytable, as

\begin{mytable}
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
\end{mytable}

or new command, say \mytable, as

\mytable{%
One-one & One-two\tabularnewline% Changing content line one
Two-one & Two-two\tabularnewline% Changing content line two
}

The \colorbox seems to throw a spanner in the works for this effort. How might I surmount this problem, please?

EDIT

In answer to @David Carlisle's query, here is the command version of what I tried:

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}
%
\newcommand{\mycontents}[1]{%
\renewcommand{\arraystretch}{1.5}%
\centering%
\setlength{\fboxsep}{0pt}%
\arrayrulecolor{blue}%
\colorbox{lightgray}{%
\begin{tabular}{l l}
\toprule%
Column 1 & Column 2\tabularnewline% Column headers
\midrule%
#1 % mind no braces {} around #1; that caused the error reported below
\bottomrule%
\end{tabular}}} % table environment removed as suggested by @egreg
%
\begin{document}

\mycontents{One-one & One-two\tabularnewline% Two-one & Two-two\tabularnewline}

\end{document}

The error was:

```! Missing } inserted.
<inserted text> 
}
l.25 Two-one & Two-two\tabularnewline}

?```

chandra
  • 3,084
  • 2
  • it requires bit of care to use any macro on an environment body but your second form as an argument would just be a simple \newcommand replacing One-...line two by #1. what error did you get? – David Carlisle Nov 19 '22 at 10:42
  • @DavidCarlisle: I get this error: ```! Missing } inserted. } l.25 Two-one & Two-two\tabularnewline}

    ?```

    I have appended my question above with the command version just in case it has an obvious error.

    – chandra Nov 19 '22 at 14:15
  • why did you add {} ? They were not in your original code, #1 not {#1} That is why you should always put full code in the question, we would have had no clue about the error in code until you show it – David Carlisle Nov 19 '22 at 14:30
  • Thanks, @ David Carlisle. I was not aware that {} pairs could lead to such problems in LaTeX. I think that adding gratuitous brace pairs is a carry over from bash where the defensive programming recommendation is to add them rather than leave them out. I can confirm that it works correctly now, And the command version is straightforward as you have alluded to. – chandra Nov 19 '22 at 15:09
  • I have commented and corrected the example above so that it works, and may be helpful to others. – chandra Nov 19 '22 at 15:18

1 Answers1

1

I wouldn't use table: when there's no caption, the table environment doesn't really make sense, because if the table floats there's no way to identify it.

Instead of \begingroup and \endgroup in the code below you can use \begin{center} and \end{center} if you want all these tables to be centered. But maybe you want to have two of them side-by-side and it would be impossible.

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}

\newcommand{\mytable}[1]{% \begingroup \renewcommand{\arraystretch}{1.5}% \setlength{\fboxsep}{0pt}% \arrayrulecolor{blue}% \colorbox{lightgray}{% \begin{tabular}{l l} \toprule Column 1 & Column 2 \ \midrule #1 \bottomrule \end{tabular}% }% \endgroup }

\begin{document}

\mytable{ One-one & One-two \ Two-one & Two-two \ }

\end{document}

Note that you either must always add \\ at the end or never, if you place the trailing \\ before \bottomrule.

enter image description here

Can the condition be relieved? Yes, but with some further work.

\documentclass{article}
\usepackage[rgb,dvipsnames,svgnames,table]{xcolor}
\usepackage{array}
\usepackage{booktabs}

\NewDocumentCommand{\mytableheaders}{}{Column 1 & Column 2}

\ExplSyntaxOn \NewDocumentCommand{\mytable}{m} { \group_begin: \renewcommand{\arraystretch}{1.5}% \setlength{\fboxsep}{0pt}% \arrayrulecolor{blue}% \colorbox{lightgray} { \begin{tabular}{l l} \toprule \mytableheaders \ \midrule \chandra_mytable_body:n { #1 } \bottomrule \end{tabular} } \group_end: }

\cs_new_protected:Nn \chandra_mytable_body:n { \regex_match:nnTF { (\c{\} | \c{tabularnewline})\s* \Z } { #1 } {% there was a trailing \ #1 } {% no trailing \ #1 \ } }

\ExplSyntaxOff

\begin{document}

\mytable{ One-one & One-two \ Two-one & Two-two \ }

\bigskip

\mytable{ One-one & One-two \tabularnewline Two-one & Two-two \tabularnewline }

\bigskip

\mytable{ One-one & One-two \ Two-one & Two-two }

\bigskip

\mytable{ One-one & One-two \tabularnewline Two-one & Two-two }

\end{document}

The argument is checked for a trailing \\ or \tabularnewline followed by spaces (that may be inserted by the endline after \\). If it's absent, one is added.

enter image description here

egreg
  • 1,121,712
  • I agree that table should not be part of the command. In your modified examples, for the third example, should not the second line be missing a \\? Where can I learn to understand the augmented experimental syntax you have used? – chandra Nov 20 '22 at 02:48
  • @chandra Yes, there was an unwanted \\. Look for \ExplSyntaxOn on the site and you’ll find many examples to study. – egreg Nov 20 '22 at 08:48
  • Thanks @egreg. I have found this discussion to be a good starting point: https://tex.stackexchange.com/questions/108696/what-do-explsyntaxon-and-explsyntaxoff-do/108703#108703 – chandra Nov 20 '22 at 16:10