4

I want to arrange EPS graphics (included via \includegraphics) in a tabular using \foreach. Currently this is done without any loops like this:

\begin{tabular}{*5{>{\centering\arraybackslash}m{.15\textwidth}}}
    $X_{40}$ & $X_{60}$ & $X_{80}$ & $X_{120}$ & $X_{160}$ \\

    \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_40.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_60.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_80.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_140.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_160.eps} \\

    \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_40.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_60.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_80.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_140.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_160.eps} \\
\end{tabular}

As you can see, the second and third row have some redundancy. Additionally in each row all columns differ only in the numbers 40, 60, 80, 140, 160. So my idea is, to use at least a \foreach in each row to put the numbers in there.

My attempt doing so arose from https://tex.stackexchange.com/a/22276. However, using \includegraphics in there does not seem to work (Argument of \reserved@a has an extra }). This is the code used:

\documentclass[draft]{article}

\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage{grffile}
\usepackage{array}
\usepackage{pgffor}
\usepackage{etoolbox}

\begin{document}

\let\tabularContent\empty

\newcommand{\plotChannels}{
    \foreach \channel in {40,60,80,120,160}{
        \begingroup\edef\x{
            \endgroup\noexpand\gappto\noexpand\tabularContent{
                & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_\channel.eps}
            }
        }\x
    }
    \gappto\tabularContent{\\}
}

\plotChannels


\begin{tabular}{*6{>{\centering\arraybackslash}m{.15\textwidth}}}
    $X_{40}$ & $X_{60}$ & $X_{80}$ & $X_{120}$ & $X_{160}$ \\

    \tabularContent

    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_40.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_60.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_80.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_140.eps}
    & \includegraphics[width=.15\textwidth]{./testrun/testvar_type_Xest_160.eps} \\
\end{tabular}

\end{document}

Thanks for your help.

  • Welcome to TeX.SX. Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Jul 30 '14 at 11:45
  • you are missing (at least) a \noexpand before \includegraphics – David Carlisle Jul 30 '14 at 11:49
  • Thanks for your useful comments. I added the MWE. Putting \noexpand before \includegraphics solves the problem. – jsh Jul 30 '14 at 12:03
4

You are missing a \noexpand before \includegraphics

5
\documentclass{article}
\usepackage{pgffor,array,ragged2e}
\usepackage[demo]{graphicx}
\newtoks\tabtoks
\newcommand\addtabtoks[1]{\tabtoks\expandafter{\the\tabtoks#1}}
\newcommand*\printtabtoks{\the\tabtoks}

\begin{document}
\tabtoks{}
\addtabtoks{\tabular{*5{>{\Centering}m{.15\textwidth}}@{}l@{}}}
\addtabtoks{$X_{40}$ & $X_{60}$ & $X_{80}$ & $X_{120}$ & $X_{160}$ \\ }
\foreach\channel in {40,60,80,120,160}{% 
  \global\addtabtoks{\includegraphics[width=.15\textwidth]{./testrun/testvar_type_X_\channel} &}}
\addtabtoks{\endtabular}
\printtabtoks

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.