Tikz:处理循环内的异常

Tikz:处理循环内的异常

\foreach我很喜欢蒂克兹,但我经常发现自己处于这样的情况:我想为循环的一次迭代设置例外。例如,

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

  \begin{tikzpicture}[every node/.style={draw, circle}]
  \node[label=above:a] (a) at (3,1) {};
  \node[label=below:b] (b) at (3,-1) {};

  \foreach \i in {0,1,2,3,4,5,6} {
    \node[label=above:\i] (x\i) at (\i, 0) {};
    \draw (x\i)--(b);
  }
  \draw (a)--(x3);
  \end{tikzpicture}

  \end{document}

我希望label=right:\i 节点 3 有,因为否则它会与边相交a。我可以想到一些丑陋的解决方案(例如,将这种情况从循环中取出并单独复制带有差异的代码),但您有更好的方法来实现这一点吗?

答案1

您正在寻找\ifnum<condition> \else \fi构造。请参阅下面的代码。

请注意,\ifnum您只能比较整数。可以使用例如来完成更复杂的条件\pgfmathparse

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

\begin{tikzpicture}[every node/.style={draw, circle}]
\node[label=above:a] (a) at (3,1) {};
\node[label=below:b] (b) at (3,-1) {};

\foreach \i in {0,...,6} {
    \ifnum\i=3
        \node[label=right:\i] (x\i) at (\i, 0) {};
    \else
        \node[label=above:\i] (x\i) at (\i, 0) {};
    \fi
    \draw (x\i)--(b);
}
\draw (a)--(x3);
\end{tikzpicture}

\end{document}

编辑:为了避免在构造中复制可能大量的代码\ifnum \else \fi,您可以这样做。我不能 100% 确定这是可靠的,但在这里它是有效的。

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

\begin{tikzpicture}[every node/.style={draw, circle}]
\node[label=above:a] (a) at (3,1) {};
\node[label=below:b] (b) at (3,-1) {};

\foreach \i in {0,...,6} {      
    \ifnum\i=3 \def\location{right} \else \def\location{above} \fi  % change here
    \node [label=\location:\i] (x\i) at (\i, 0) {};                 % and here
    \draw (x\i)--(b);
}
\draw (a)--(x3);
\end{tikzpicture}

\end{document}

答案2

您可以像这样定义标签选项:

[label={\ifnum\i\equals3 right\else above\fi :\i}]

将命令\newcommand\equals{=}添加到前导码中。因为这是当它试图拆分输入以识别键/值对时=解析错误的情况。这个想法借鉴了@egreg在tikz=类似问题

\documentclass[border=5pt,tikz]{standalone}
\newcommand\equals{=}
\begin{document}

  \begin{tikzpicture}[every node/.style={draw, circle}]
  \node[label=above:a] (a) at (3,1) {};
  \node[label=below:b] (b) at (3,-1) {};

  \foreach \i in {0,...,6} {
    \node [label={\ifnum\i\equals3 right\else above\fi :\i}] (x\i) at (\i, 0) {};
    \draw (x\i)--(b);
  }
  \draw (a)--(x3);
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案3

第一个解决方案(从列表中删除异常情况\foreach):

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={draw, circle}]
  \node[label=above:a] (a) at (3,1) {};
  \node[label=below:b] (b) at (3,-1) {};

  \foreach \i in {0,1,2,4,5,6} {
    \node[label=above:\i] (x\i) at (\i, 0) {};
    \draw (x\i)--(b);
  }
  % exception
  \node[label=right:3] (x3) at (3, 0) {};
  \draw (x3)--(b);

  \draw (a)--(x3);
\end{tikzpicture}
\end{document}

第二种解决方案(使用两个变量也不例外\foreach):

\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[every node/.style={draw, circle}]
  \node[label=above:a] (a) at (3,1) {};
  \node[label=below:b] (b) at (3,-1) {};

  \foreach \i/\mypos in {0/above,1/above,2/above,3/right,4/above,5/above,6/above} {
    \node[label=\mypos:\i] (x\i) at (\i, 0) {};
    \draw (x\i)--(b);
  }

  \draw (a)--(x3);
\end{tikzpicture}
\end{document}

相关内容