TransWikia.com

How do I test for even/odd page within TikZ foreach loop

TeX - LaTeX Asked on March 28, 2021

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?

One Answer

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
  ifoddgetpagerefnumber{opl-theoddpagelabel}
    A
  else
    B
  fi
  hello(myi)
}

end{document}

Correct answer by Werner on March 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP