2

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?

3
  • 1
    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.
    – user30471
    Commented Sep 10, 2018 at 22:12
  • 1
    Related: tex.stackexchange.com/questions/367979/…
    – user30471
    Commented Sep 10, 2018 at 22:14
  • 1
    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.
    – user30471
    Commented Sep 10, 2018 at 22:15

1 Answer 1

5

Since the definition of \sym occurs in the argument to another command, it should be

\def\sym##1{\ifmmode^{##1}\else\(^{##1}\)\fi}

in this code.

4
  • The other command that the definition of \sym occurs in is the \foreach command? I'm confused, I only knew that adding additional # is necessary if defining a macro inside the replacement text of another macro, why is it also needed when passing a marco definition as an argument when executing another macro?
    – Andi Bauer
    Commented Jul 17, 2020 at 12:02
  • 1
    @AndiBauer That's more complicated and would require an analysis of what \foreach does.
    – egreg
    Commented Jul 17, 2020 at 12:12
  • Ok but you're saying that (for some reason) whenever I'm defining a macro inside a \foreach loop, I need to add the extra #, right?
    – Andi Bauer
    Commented Jul 18, 2020 at 15:53
  • @AndiBauer Yes, I guess so.
    – egreg
    Commented Jul 18, 2020 at 17:38

You must log in to answer this question.

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