0

I want to print some images according to a variable being changed in a loop: if variable group is 1, then the image should not be printed. But if the value of group is 2, then the image should be printed. So I am looking for sort of an "if" condition.

I have already looked at the if conditions in LaTeX but I am not finding a solution that fits my case (controlling the if with a variable which is changing in a loop).

\newcounter{group}
\newcounter{subj}
\newcounter{number_of_runs_simona}
\forloop{group}{1}{\value{group} < 2}{
    \forloop{subj}{1}{\value{subj} < 2}{
    \forloop{number_of_runs_simona}{1}{\value{number_of_runs} < 26}{
    \centering

    \textbf{\large Group \arabic{group}, Subject \arabic{subj}, Run \arabic{number_of_runs}}

    \begin{figure}[!ht]
      \centering
        \includegraphics{\FILENAMEONE{\arabic{group}}{\arabic{subj}}{\arabic{number_of_runs_simona}}}
    \end{figure}

    \clearpage
    }
    }
    }

Figure "FILENAMEONE" should only be printed when the variable group has value 2.

Thank you very much for your help.

2

Something like this?

\documentclass{article}

\begin{document}

\newcount\group
\newcount\subj
\newcount\nors

\group=0
\loop
  \advance\group by 1
  \subj=0
  {\loop
     \advance\subj by 1
     \nors=0
     {\loop
       \advance\nors by 1
        {
          group \the\group, subject \the\subj, run \the\nors
          \par
          \ifnum\group=2
            \begin{figure}[h!]
              \centering
              \emph{includegraphics filenameone-\the\group-\the\subj-\the\nors}
              \caption{Test of filenameone-\the\group-\the\subj-\the\nors}
            \end{figure}
            \par
          \fi
        }
        \ifnum\nors<26
      \repeat}
     \ifnum\subj<2  
   \repeat}
  \ifnum\group<2
\repeat

\end{document} 

Sorry, I have always used a mixture of low-level \if commands and counter definitions, even if it is probably a bad practice.

I am not sure how \ifnum\value{group}=2 would work in your case but maybe that is all you need to wrap around your figure environment to get your original example working ...

1
  • Indeed, adding \ifnum\value{group}=2 solved my problem. Thanks a lot!!!! – Miguel Mendes May 19 '16 at 15:15
1

If you don't mind an additional package, pgffor is quite handy to provide foreach-loops.

\documentclass{article}
\usepackage{graphicx}
\usepackage{pgffor}

\begin{document}

test

\foreach \n in {1,...,2}{
    \ifnum\n=2%
        \includegraphics{pic}%
    \fi%
}

\end{document}

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.