0

I'm using \foreach in Tikz to make a line with vertical marks at some real numbers. But I want to put \pi and \e (Euler number) in the code, but it doesnt work. Please, can anyone help me?

My Code:

\begin{tikzpicture}[scale=1.8]
\draw [->] (-4,0) -- (4,0);
\foreach \x in  {-3,-2,-1,0,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
\foreach \y in  {1/2,4/3,-2/3}
\draw[shift={(\y,0)},color=black] (0pt,-3pt) -- (0pt,3pt) node[above] {$\y$};
\end{tikzpicture}
\end{center}

I want:

\begin{tikzpicture}[scale=1.8]
\draw [->] (-4,0) -- (4,0);
\foreach \x in  {-3,-2,-1,0,1,2,3}
\draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt) node[below] {$\x$};
\foreach \y in  {1/2,4/3,-2/3, \pi, \e}
\draw[shift={(\y,0)},color=black] (0pt,-3pt) -- (0pt,3pt) node[above] {$\y$};
\end{tikzpicture}
\end{center}
  • 2
    Welcome to TeX.se. Please don't post code fragments. Instead, put the fragments into a complete compilable (even with error) document. In your second example, you should use e and pi not \e and \pi. – Alan Munn Jun 26 '19 at 1:40
  • Try \documentclass[tikz,border=3.14mm]{standalone} \begin{document} \begin{tikzpicture} \draw [-stealth] (-4,0) -- (4,0); \foreach \X in {-3,-2,-1,0,1,2,3} {\draw (\X,3pt) -- (\X,-3pt) node[below] {$\X$};} \foreach \Y/\Z in {1/2/\frac{1}{2},4/3/\frac{4}{3},-2/3/\frac{-2}{3}, pi/\pi, e/\mathrm{e}} {\draw (\Y,-3pt) -- (\Y,3pt) node[above] {$\Z$};} \end{tikzpicture} \end{document} if you want, in addition to @AlanMunn's comment, to typeset the pi as \pi. – user121799 Jun 26 '19 at 2:54
  • Thank you, guys! – Eric Jun 26 '19 at 19:33
  • @AlanMunn: There's another thing: when I type pi instead of \pi, the result is pi instead of the symbol of pi. Any clue? – Eric Jun 26 '19 at 20:07
  • Yes, see @marmot 's answer. In the coordinates you want the value of pi, i.e., 3.14159... which is pi. But when you put the node label, TikZ just puts whatever is in the list. It can't know that you want to represent pi as \pi (which prints the Greek letter). So you need a separate list for the labels, which is what his answer does. – Alan Munn Jun 26 '19 at 20:07
2

Full credit to Alan Munn, whose comment solves the problem, and which is why this is a community wiki. Apart from that I would not use the shifts (and it is better not to use \x and \y if you consider also loading calc and using it). So here I repost my above comment.

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw [-stealth] (-4,0) -- (4,0);
\foreach \X in  {-3,-2,-1,0,1,2,3}
{\draw (\X,3pt) -- (\X,-3pt) node[below] {$\X$};}
\foreach \Y/\Z in  {{1/2}/\frac{1}{2},{4/3}/\frac{4}{3},{-2/3}/\frac{-2}{3}, pi/\pi,
e/\mathrm{e}}
{\draw (\Y,-3pt) -- (\Y,3pt) node[above] {$\Z$};}
\end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||
  • Worked very well! Thank you! – Eric Jun 26 '19 at 20:41
  • @Eric You're welcome. I was just spelling out Alan's comment. – user121799 Jun 26 '19 at 20:53
1
\documentclass[tikz, margin=3mm]{standalone}

\begin{document}
\begin{tikzpicture}[scale=1.8]
\draw [->] (-4,0) -- (4,0);
    \foreach \x in  {-3,-2,-1,0,1,2,3}
\draw[shift={(\x,0)}]   (0,3pt) -- (0,-3pt) node[below] {$\x$};
    \foreach \y [count=\i] in  {1/2,4/3,-2/3, e, pi}
{
\draw[shift={(\y,0)}]   (0,-3pt) -- (0,3pt)
    \ifnum\i=5
node[above] {$\pi$};
    \else
node[above] {\y};
    \fi
}
\end{tikzpicture}
\end{document}

enter image description here

|improve this answer|||||

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.