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}
Marco
  • 1,373

3 Answers3

12

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}
Ondrian
  • 1,536
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

egreg
  • 1,121,712
  • 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 http://tex.stackexchange.com/questions/275056/why-is-the-last-pgfplots-tick-label-not-shown-in-one-of-my-groupplots for a similar problem – egreg Feb 18 '16 at 19:10
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}