TikZ 내부의 ifthenelse: 작동하지 않음

TikZ 내부의 ifthenelse: 작동하지 않음

나는 이것을 얻고 싶습니다 :

여기에 이미지 설명을 입력하세요

ifthenelse내부를 사용해 보았 foreach으나 오류가 발생했습니다: Missing number, treated as zero. <to be read again> = l.9 }.여기esdd는 " \ifthenelse는 "정상적인" LaTeX 코드입니다. 따라서 TikZ 경로 사양 내에서는 이 명령을 사용할 수 없습니다. 그러나 이 문제를 해결하는 방법을 모르겠습니다. 내 코드는 다음과 같습니다.

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}

\begin{document}
\begin{tikzpicture}
\foreach \y in {0,0.2,0.4,...,1.6}{
    \ifthenelse{\y==1.6}{\draw [thin,-latex] (-0.8,1.6) -- (-0.3,1.6) node [above,midway] {U};}{\draw [thin,-latex] (-0.8,\y) -- (-0.3,\y);}
}
\end{tikzpicture}
\end{document}

답변1

확실히 사용해도 되지만 \ifthenelse,

  1. 테스트에서는 정수만 비교합니다.
  2. 그것은 하나를 사용합니다=
  3. TikZ가 1.6이 되면 실제로는 1.59998로 표시됩니다.

정수를 사용하고 다음을 수행하십시오.

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usepackage{ifthen}

\begin{document}
\begin{tikzpicture}
\foreach \y in {0,2,4,...,16}{
  \ifthenelse{\y = 16}
    {\draw [thin,-latex] (-0.8,1.6) -- (-0.3,1.6) node [above,midway] {U}}
    {\draw [thin,-latex] (-0.8,\y/10) -- (-0.3,\y/10)}
  ;
}
\end{tikzpicture}
\end{document}

여기에 이미지 설명을 입력하세요

답변2

\ifnum대안적인 접근 방식은 와 결합된 표준 구성이 될 것입니다 \pgfmathparse. 1.6은 부동 소수점이므로 허용 오차를 제공해야 합니다. 간단한 것은 \pgfmathparse{\y == 1.6 ? int(1) : int(0)}작동하지 않습니다.

완전한 솔루션은 다음과 같습니다.

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\foreach \y in {0,0.2,0.4,...,1.6}{
    \pgfmathparse{abs(\y - 1.6) < 0.001 ? int(1) : int(0)}
    \ifnum\pgfmathresult=1 
        \draw [thin,-latex] (-0.8,\y) -- (-0.3,\y) node [above,midway] {U};
    \else
        \draw [thin,-latex] (-0.8,\y) -- (-0.3,\y);
    \fi
}
\end{tikzpicture}
\end{document}

답변3

필수 xintexpr솔루션. 이번에는 너무 존경스럽기 \xintFor때문에 여러분께 양해를 구합니다.\foreach

목록 인수를 먼저 확장하는 방법을 모르므 로 먼저 정의 가 포함된 설명서 \foreach의 장치에 의존해야 합니다 .TikZ\mylist

여기서의 방법은 고정 소수점 연산이 정확해야 하는 보다 복잡한 상황을 위한 것입니다.

\documentclass[border=0.2cm]{standalone}
\usepackage{tikz}
\usepackage{xintexpr}
\begin{document}
\begin{tikzpicture}
\edef\mylist{\xinttheiexpr [1] 0..[+0.2]..1.6\relax}% 
% (The [1] is to tell it to use fixed point notation 
% with one digit after decimal mark, and this expands to 
% 0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6 )
% 
\foreach \y in \mylist
{%
  \xintifboolexpr{\y = 1.6}
    {\draw [thin,-latex] (-0.8,1.6) -- (-0.3,1.6) node [above,midway] {U}}
    {\draw [thin,-latex] (-0.8,\y) -- (-0.3,\y)}
  ;
}
\end{tikzpicture}
\end{document}

관련 정보