1

I made a command \statementinput which makes a table easily with given two lists of elements. Most of the elements are math formulas, and I don't want to put each of them in \(\), so I used the array environment inside \[\].

When I used \mathrm in an element, it gave errors. I thought the reason is that \mathrm is fragile, so I added \robustify{\mathrm} and everything seemed fine.

Now I need to use \mathbf. Still \robustify{\mathbf} works, but now using \(\mathbf{A}\) in a normal place does not work, giving an error Use of \reserved@a doesn't match its definition. \(\mathbf{A}. How can I solve this problem? Thanks in advance!

Here is a MWE:

\documentclass{article}

\usepackage{tikz, etoolbox}
\usepackage{amsmath, amssymb}
\usepackage{array}

\makeatletter
\NewDocumentCommand{\statementapp}{m m m m}{%
    \def\rowAname{#1}%
    \def\rowBname{#2}%
    \def\listA{#3}%
    \def\listB{#4}%
    \newcount\listlen
    %
    \let\arraycontent\empty
    \xappto\arraycontent{\text{\rowAname}}
    \foreach \Ae in \listA {%
        \xappto\arraycontent{& \Ae}%
    }%
    \gappto\arraycontent{\\ \hline}
    \xappto\arraycontent{\text{\rowBname}}
    \foreach \Be [count=\n] in \listB {%
        \xappto\arraycontent{& \Be}%
        \global\let\listlen\n
    }%
    \[%
    \begin{array}{|r<{\hspace{0.3pc}}|*{\listlen}{>{\hspace{0.3pc}}l|}}
        \hline
        \arraycontent \\
        \hline
    \end{array}\]%
}
\makeatother

\NewDocumentCommand{\statementinput}{m m}{%
    \statementapp{Parameters}{Playing the role of}
    {#1}{#2}%
}

\newcommand{\oo}{{\mathrm{o}}}

\robustify{\mathrm}
\robustify{\mathbf}



\begin{document}

\statementinput{\mathrm{Ab}Cd, 2}{B^\oo,4}
\statementinput{\mathbf{Ab}Cd, 2}{B^\oo,4} 

%\(\mathbf{A}\) % does not work

\end{document}

Screenshot of the MWE

3
  • Do not use \xappto if you handle arbitrary input, that is bound to break somewhere (untested, but here probably at the line break), and do not allocate a count inside a definition body). Commented Mar 11, 2023 at 6:33
  • @UlrikeFischer I used \xappto because I wanted for example \Ae to be expanded when I append it to \arraycontent. What should I use instead? Commented Mar 11, 2023 at 7:39
  • with a current latex something like this \gappto\arraycontent{&}\ExpandArgs{No}\gappto\arraycontent{\Be}. With older latex you need a bunch of \expandafter to control the expansion. Commented Mar 11, 2023 at 8:25

1 Answer 1

1

You're using the wrong tools.

\documentclass{article}
\usepackage{array}

\ExplSyntaxOn

\NewDocumentCommand{\statementapp}{mmmm}
 {
  \greatseo_statementapp:nnnn { #1 } { #2 } { #3 } { #4 }
 }

\cs_new_protected:Nn \greatseo_statementapp:nnnn
 {
  \begin{tabular}
   {
    |r<{\hspace{0.3pc}}|*{\clist_count:n { #3 }}{>{\hspace{0.3pc}$}l<{$}|}
   }
  \hline
  #1 & \clist_use:nn { #3 } { & } \\
  \hline
  #2 & \clist_use:nn { #4 } { & } \\
  \hline
  \end{tabular}
 }

\ExplSyntaxOff

\NewDocumentCommand{\statementinput}{m m}{%
    \statementapp{Parameters}{Playing the role of}
    {#1}{#2}%
}

\newcommand{\oo}{{\mathrm{o}}}

\begin{document}

\statementinput{\mathrm{Ab}Cd, 2}{B^\oo,4}

\medskip

\statementinput{\mathbf{Ab}Cd, 2}{B^\oo,4}

\medskip

\statementinput{\mathbf{Ab}Cd, 2, 3, 4}{B^\oo,4,5,6}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .