
Estoy intentando usar el \pgfmathifthenelse
comando para colocar un nodo en mi gráfico tikz
, pero sigo teniendo este error:
"Incompleto \iffalse"
Aquí está mi 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}
¿Alguien sabe dónde está el problema?
Respuesta1
Algunos asuntos:
- Te faltaba un corchete en la línea con
pgfmathifthenelse
. - Todos sus comandos se pueden reescribir para establecer una variable en línea en lugar de usar
\pgfmathresult
.\pgfmathparse
hace eso, pero puedes usar\pgfmathsetmacro<VARIABLE>{<CODE>}
Entonces, puedes arreglar la \pgfmathifthenelse
reescritura de esta manera:
\pgfmathsetmacro\xfinal{(\xtmp==0?(\xfactor):\xtmp)}
que hace
\pgfmathsetmacro\xfinal{(<TEST>?(<TRUE>):<FALSE>)}
Producción
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}