# LaTeX error: illegal parameter number in definition of \pgffor@b

I am trying to use a loop. The following does not work:

\foreach \a in {no,has}{
\begin{table}[H]
\footnotesize
\captionsetup{width=.9\linewidth}
\caption{text}
\vspace{-22pt}
\label{}
\begin{center}
{
\def\sym#1{\ifmmode^{#1}\else$$^{#1}$$\fi}
\begin{tabular}{l*{6}{c}}
\toprule \toprule \\
~~~~text \\
\hline
\input{../text1_\a_c.tex}
\hline \\
~~~~text \\
\hline
\input{../text2_\a_c.tex}
\hline
\bottomrule
\end{tabular}
}
\end{center}
\caption*{\begin{footnotesize}  \end{footnotesize}}
\end{table}
}


What I am trying to do is to access four similarly-named files with the loop — two the first time through (named ...no...) and two the second time through (named ...has...).

I get this error:

illegal parameter number in definition of \pgffor@b


Note that the following does work:

This works:

\foreach \a in {no,has}{
\a
}


What should I try to fix this?

• Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what packages etc you are using before we can compile your code. – Andrew Sep 10 '18 at 22:12
• – Andrew Sep 10 '18 at 22:14
• Btw, filenames with underscores in them are problematic in latex... Renaming them as text2-\a-c.tex might solve your problem. If not rather using \foreach from pgffor package you'll probably have more luck with \docsvlist from the etoolbox package. – Andrew Sep 10 '18 at 22:15

Since the definition of \sym occurs in the argument to another command, it should be
\def\sym##1{\ifmmode^{##1}\else$$^{##1}$$\fi}