1

I am trying to make a macro for the rows in my table, just to make it look a bit more elegant. However, the document fails to compile when I use more than one row.

Here's the code I'm trying to duplicate:

\usepackage{tabularx}

\begin{tabularx}{\textwidth}{ r X }
  \bfseries{Topic1:} & Item1, Item2, Item3 \\
  \bgseries{Topic2:} & Item1, Item2, Item3 \\
\end{tabularx}

And my attempt to create a macro:

\usepackage{tabularx}

\begin{tabularx}{\textwidth}{ r X }
  \newcommand{\row}[2]{\bfseries{#1:} & #2 \\}
  \row{Topic1}{Item1, Item2, Item3}
  \row{Topic2}{Item1, Item2, Item3}
\end{tabularx}

And the compiler error I'm getting is "Undefined control sequence." Is there any way to get this to work?

djorna
  • 11
  • 4
    Table cells create groups, so definitions made inside a tabular cell don't make it until the next one. Move the definition outside the table. – Phelype Oleinik Jun 13 '19 at 23:05
  • Wow that was embarrassingly easy, thanks! – djorna Jun 13 '19 at 23:08
  • 1
    Don't worry, everyone did that at least once :-) Also note that your usage of \bfseries is incorrect. \bfseries doesn't take an argument and changes the font until the current group ends, so you should use as {\bfseries #1:}. Alternatively, use \textbf, which does take an argument: \textbf{#1:}. By the way, welcome to TeX.SX! – Phelype Oleinik Jun 13 '19 at 23:11
  • 5
    I'm voting to close this question as off-topic because it was solved by moving the definition outside the table. – Phelype Oleinik Jun 13 '19 at 23:11
  • Fair enough, thanks for your help. – djorna Jun 13 '19 at 23:13
  • Related: https://tex.stackexchange.com/questions/367979/latex-foreach-in-tabular-environment –  Jun 13 '19 at 23:59

0 Answers0