我正在尝试使用\pgfmathifthenelse
命令将节点放置在图表中tikz
,但仍然出现此错误:
“不完整 \iffalse”
这是我的代码:
\begin{tikzpicture}
\def \n {4}
\def \hradius {2cm}
\def \vradius {1.2cm}
\def \nradius {0cm}
\def \lengths {3,4,6}
\def \degree {3}
\node (u) at (0,{2*\vradius}) {$\bullet$};
\node (v) at (0,{-2*\vradius}) {$\bullet$};
\node at (0,{2.2*\vradius}) {$u$};
\node at (0,{-2.2*\vradius}) {$v$};
\draw[>=latex] (v.center) -- (u.center) node [midway,left]{{$1$}};
\foreach [count=\i, evaluate=\i as \k] \s in \lengths {
\pgfmathparse{\s-1}
\edef \l {\pgfmathresult}
\pgfmathparse{\k-2}
\def \xtmp {\pgfmathresult}
\pgfmathparse{\xtmp+1}
\edef \xfactor {\pgfmathresult}
\pgfmathparse{\pgfmathifthenelse{\xtmp=0}{\xfactor {\xtmp}}\pgfmathresult
\edef \xfinal {\pgfmathresult}
\foreach \i in {1,...,\l}{
\pgfmathparse{1/\s}
\def \inverse {\pgfmathresult}
\node (\s\i) at ({(\k-2)*\hradius},{((4*\vradius)*\inverse*\i)-2*\vradius}) {\k};
}
}
\end{tikzpicture}
有人知道问题出在哪里吗?
答案1
一些问题:
- 您在带有 的行处缺少一个括号
pgfmathifthenelse
。 - 可以重写所有命令以内联设置变量,而不是使用
\pgfmathresult
.\pgfmathparse
这样做,但您可以使用\pgfmathsetmacro<VARIABLE>{<CODE>}
因此,你可以\pgfmathifthenelse
像这样修复重写:
\pgfmathsetmacro\xfinal{(\xtmp==0?(\xfactor):\xtmp)}
确实
\pgfmathsetmacro\xfinal{(<TEST>?(<TRUE>):<FALSE>)}
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\def\n{4}
\def\hradius{2cm}
\def\vradius{1.2cm}
\def\nradius{0cm}
\def\lengths{3,4,6}
\def\degree{3}
\node (u) at (0,{2*\vradius}) {$\bullet$};
\node (v) at (0,{-2*\vradius}) {$\bullet$};
\node at (0,{2.2*\vradius}) {$u$};
\node at (0,{-2.2*\vradius}) {$v$};
\draw[>=latex] (v.center) -- (u.center) node [midway,left]{{$1$}};
\foreach [count=\i, evaluate=\i as \k] \s in \lengths {%
\pgfmathsetmacro\l{\s-1}
\pgfmathsetmacro\xtmp{\k-2}
\pgfmathsetmacro\xfactor{\xtmp+1}
\pgfmathsetmacro\xfinal{(\xtmp==0?(\xfactor):\xtmp)}
\foreach \i in {1,...,\l}{%
\pgfmathsetmacro\inverse{1/\s}
\node (\s\i) at ({(\k-2)*\hradius},{((4*\vradius)*\inverse*\i)-2*\vradius}) {\k};
}
}
\end{tikzpicture}
\end{document}