使用 \pgfmathifthenelse 放置節點

使用 \pgfmathifthenelse 放置節點

我正在嘗試使用該\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

一些問題:

  1. 您在該行缺少一個括號pgfmathifthenelse
  2. 您可以重寫所有命令以設定內聯變量,而不是使用\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}

相關內容