9

I would like to to get this:

enter image description here

I tried using ifthenelse inside foreach but I get an error: Missing number, treated as zero. <to be read again> = l.9 }. Here esdd says "\ifthenelse is "normal" LaTeX code. Therefore you can not use this command inside a TikZ path specification." However, I do not know how to fix this the problem. Here is my code:

\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}
4
  • Two things are clear: the test uses just =, but it can only compare integers.
    – egreg
    Feb 18 '16 at 18:34
  • I tried changing the last element of foreach from 1.6 to 1; then I tried using \ifthenelse{\y=1} but it does not work.
    – Marco
    Feb 18 '16 at 18:40
  • you need \equal the = test in ifthen (not ==) is n \ifnum so only for integers Feb 18 '16 at 18:47
  • 2
    A third problem: when TikZ comes to 1.6, it evaluates it as 1.59998
    – egreg
    Feb 18 '16 at 18:48
10

You can surely use \ifthenelse, but

  1. the test compares only integer
  2. it uses a single =
  3. when TikZ comes to 1.6 it actually sees it as 1.59998

Use integers, then:

\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}

enter image description here

3
  • Could you explain to me what "when TikZ comes to 1.6 it actually sees it as 1.59998" means?
    – Marco
    Feb 18 '16 at 18:58
  • 2
    @Marco The joys of floating point arithmetic in TeX! ;-)
    – egreg
    Feb 18 '16 at 19:05
  • @Marco see tex.stackexchange.com/questions/275056/… for a similar problem
    – egreg
    Feb 18 '16 at 19:10
11

An alternative approach would be the standard \ifnum construct combined with \pgfmathparse. Note that since 1.6 is a float, you must provide a tolerance. A simple \pgfmathparse{\y == 1.6 ? int(1) : int(0)} would not work.

Here is the complete solution:

\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}
1
  • I forgot to add int(). That resulted in a weird Missing = inserted for \ifnum. Feb 9 '19 at 15:53
4

The mandatory xintexpr solution. This time I spare you folks the \xintFor, as \foreach is too venerable.

I don't know how to tell \foreach to expand its list argument first, hence I have to resort to the device from the TikZ manual with a \mylist definition first.

The method here is for more complicated situation where fixed point operations must be exact.

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.