TiKz と \pgfmathifthenelse

TiKz と \pgfmathifthenelse

次のコードでは、青い線が (0,0) から (0.5,0) に移動しないのはなぜか、誰か知っていますか?

\pgfmathresult私にとっては、横軸 1 は条件の結果として 0.5 で乗算されるはずです\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);

ここに画像の説明を入力してください

関連情報