![如何在 TikZ \foreach 循环中测试偶数/奇数页](https://linux22.com/image/420594/%E5%A6%82%E4%BD%95%E5%9C%A8%20TikZ%20%5Cforeach%20%E5%BE%AA%E7%8E%AF%E4%B8%AD%E6%B5%8B%E8%AF%95%E5%81%B6%E6%95%B0%2F%E5%A5%87%E6%95%B0%E9%A1%B5.png)
我正在使用 TikZ foreach 循环创建文档。以下是代码片段
\documentclass{article}
\usepackage{tikz}
\begin{document}
\foreach \myi in {1,2,...,17}
{
hello(\myi)
\clearpage
\ifoddpage
A
\else
B
\fi
}
\end{document}
但这会导致以下错误:
! Extra }, or forgotten \endgroup.
\pgffor@collectargument ...fter {\pgffor@body {#1}
}\else \expandafter \long ...
l.14 }
有什么想法可以解决这个问题吗?
答案1
\ifoddpage
默认情况下未定义。您需要将该包添加到您的序言中,并\checkoddpage
在调用检查之前添加\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}
以下是使用相同的方法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}