Tikz `\ifeven` 触发错误

Tikz `\ifeven` 触发错误

为什么下面的示例在 pdflatex 中\ifeven使用时编译失败?

它在第 34 行(最后一个右括号所在位置)出现许多错误,并失败。错误如下:Undefined control sequence. },,,。Extra }, or forgoten \endgroup.}Extra \else.}Extra \fi }

\documentclass[tikz]{standalone}

\newcommand{\stencilptreg}[4][]{\node[circle,fill,draw,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}
\newcommand{\stencilptstg}[4][]{\node[circle,fill,white,draw=black,inner sep=1.5pt,label={below:#4},#1] at (#2) (#3) {}}

\begin{document}
        \begin{tikzpicture}[x=2cm,y=2cm]
            \pgfmathsetmacro\N{5};
            \pgfmathsetmacro\M{3};

            \pgfmathsetmacro\NN{(\N*2)};            % Number of points in staggered grid
            \pgfmathsetmacro\MM{(\M*2)};            % Number of points in staggered grid            

            \foreach \j [evaluate=\j as \q using {{int(\j/2)}}] in {0,1,...,\MM} {

                \ifodd\j
                    \draw [black!50,thin] (0,\j) node[red,left] {$n=\frac{\j}{2}$} -- (\N,\j);                  
                \else
                    \draw [black!50,thin] (0,\j) node[red,left] {$n=\q$} -- (\N,\j);
                \fi

                \foreach \i [evaluate=\i as \p using {(\i/2)}] in {0,1,...,\NN} 
                {
                    \ifodd\i
                        \ifodd\j
                            \stencilptstg{\p,\j}{x-\i-\j}{$\p$};
                        \fi
                    \else
                        %\ifeven\j
                            \pgfmathsetmacro{\p}{{int(\p)}};                
                            \stencilptreg{\p,\j}{x-\i-\j}{$\p$};
                        %\fi
                    \fi
                }
            }
        \end{tikzpicture}
\end{document}

答案1

您应该在日志文件中查找精确的错误消息,而不是依赖 TeXStudio 或其他前端显示的短标签。

以批处理模式运行文件时,第一个错误消息是

! Extra }, or forgotten \endgroup.
<argument> ...p ,\j }{x-\i -\j }{$\p $}; \fi \fi }

l.36 }

不是很清楚,但往下几行你会看到

! Extra \else.
\pgffor@collectargument ...pgffor@body {#1}}\else 
                                                  \expandafter \long \expand...
l.36 }

I'm ignoring this; it doesn't match any \if.

! Extra \fi.
\pgffor@collectargument ...r {\pgffor@body #1}\fi 
                                                  \pgffor@iterate 
l.36 }

我们越来越接近了!让我们再往下看一点:

! Undefined control sequence.
\pgffor@body ...i -\j }{$\p $}; \fi \else \ifeven 
                                                  \j \pgfmathsetmacro {\p }{...
l.36 }

The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

嘿!这就是问题所在!

TeX 没有\ifeven,而且 TiZ 没有提供此功能。实际上,不可能定义一个与仅反转测试\ifeven相同的命令。\ifodd

您可以选择

  \unless\ifodd\j\relax
    \pgfmathsetmacro{\p}{{int(\p)}};
    \stencilptreg{\p,\j}{x-\i-\j}{$\p$};
  \fi

或者

  \ifodd\j\relax\else
    \pgfmathsetmacro{\p}{{int(\p)}};
    \stencilptreg{\p,\j}{x-\i-\j}{$\p$};
  \fi

我建议\relax不要触发过早扩展。这里没什么大不了的,但在其他情况下,这可能是至关重要的。但是,它不应该添加到任何数字条件中,特别是在需要完全扩展的上下文中。

更安全的保护措施可能是

\ifodd\numexpr\j\relax\else

或者

\unless\ifodd\numexpr\j\relax

但这样就太浪费了。

答案2

因为没有\ifeven。然而,它相当于

\ifodd\j\else
  % <\ifeven> content
\fi

在此处输入图片描述

相关内容