3

I'm creating a document using the TikZ foreach loop. Here's a snippet

\documentclass{article}
\usepackage{tikz}
\begin{document}

\foreach \myi in {1,2,...,17}
  {
    hello(\myi)
    \clearpage
    \ifoddpage
      A
    \else
      B
    \fi
  }

\end{document}

But this creates the following error:

! Extra }, or forgotten \endgroup.
\pgffor@collectargument ...fter {\pgffor@body {#1}
                                                  }\else \expandafter \long ...
l.14   }

Any ideas of how to work around this?

1
  • Immediately after a \clearpage, \ifodd\value{page} will do. Sep 11, 2020 at 21:17

1 Answer 1

3

\ifoddpage is not defined by default. You need to add that package to your preamble and also \checkoddpage before calling checking \ifoddpage:

\documentclass{article}

\usepackage{tikz,ifoddpage}

\begin{document}

\foreach \myi in {1,2,...,17}{%
  \clearpage
  \checkoddpage
  \ifoddpage
    A
  \else
    B
  \fi
  hello(\myi)
}

\end{document}

Here's the same approach using refcount:

\documentclass{article}

\usepackage{tikz,refcount}

\newcounter{oddpagelabel}
\newcommand{\checkoddpage}{\stepcounter{oddpagelabel}\label{opl-\theoddpagelabel}}

\begin{document}

\foreach \myi in {1,2,...,17}{%
  \clearpage
  \checkoddpage
  \ifodd\getpagerefnumber{opl-\theoddpagelabel}
    A
  \else
    B
  \fi
  hello(\myi)
}

\end{document}

You must log in to answer this question.

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