
Estou tentando usar o \pgfmathifthenelse
comando para colocar um nó no meu gráfico com tikz
, mas continuo tendo este erro:
"Incompleto \iffalse"
Aqui está meu código:
\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}
Alguém sabe onde está o problema?
Responder1
Alguns problemas:
- Estava faltando um colchete na linha com
pgfmathifthenelse
. - Todos os seus comandos podem ser reescritos para definir uma variável embutida em vez de usar
\pgfmathresult
.\pgfmathparse
faz isso, mas você pode usar\pgfmathsetmacro<VARIABLE>{<CODE>}
Então, você pode corrigir a \pgfmathifthenelse
reescrita assim:
\pgfmathsetmacro\xfinal{(\xtmp==0?(\xfactor):\xtmp)}
o que faz
\pgfmathsetmacro\xfinal{(<TEST>?(<TRUE>):<FALSE>)}
Saída
Código
\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}