TiKz 和 \pgfmathifthenelse

TiKz 和 \pgfmathifthenelse

有人知道為什麼,使用下面的程式碼,我沒有讓我的藍線從 (0,0) 到 (0.5,0)?

對我來說,橫座標 1 應該乘以 0.5,由\pgfmathresult條件結果給出,\pgfmathifthenelse但事實並非如此...

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\newcommand{\origin}{
\node (O) at (0,0){$\times$};
\node at (1,0){$\times$};
\node at (O){O};}

\begin{document}
\begin{tikzpicture}
\origin
\pgfmathifthenelse{1}{"0.5*"}{}
\draw[blue] (0,0)--++(\pgfmathresult1,0);
\end{tikzpicture}
\end{document}

答案1

我肯定會遠離這種用法,但問題是 \pgfmathresult定義不會存在那麼久。所以它的當前值需要快速使用。因為很多繪圖指令內部也使用它。

\pgfmathsetmacro\mytemp{ifthenelse(1,"0.5*",)};
\draw[blue] (0,0)--++(\mytemp1,0);

作品。相反,更直觀並且沒有任何擴展問題的程式碼是

\pgfmathsetmacro\mytemp{ifthenelse(1,0.5,1)};
\draw[blue] (0,0)--++(\mytemp*1,0);

這也相當於

 \draw[blue] (0,0) --++({ifthenelse(1,0.5,1)*(1cm)},0);

在此輸入影像描述

相關內容