8

I am looking to create simple number lines. I have asked a similar question: Resetting numerical labels in \foreach commands on a numberline. But now I am in a different situation.

I know how to start the counting at a different number, but how can I count from a specific number by fives. For example: I want the number line to begin at 140, but count by 5's every x coordinate.

\documentclass[letterpaper,12pt]{book}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=1pt,line cap=round,x=.5cm,y=.5cm]
                    \clip (0,-0.8) rectangle (10,1);
                    %   Inequality Line
                    \draw   [<->,thick]     (0,0) -- (10,0);
                    %   Tick Marks
                    \foreach \x in {1,2,...,9}
                    \draw (\x,-2pt) -- (\x,2pt) node [anchor=north,below=3pt] 
                    {$\scriptstyle \x$};
                    %   Displayed Solution
\end{tikzpicture}               
\end{document}
  • 4
    You can calculate the labels from your loop variable: \pgfmathparse{int(140+5*(\x-1))}$\scriptstyle \pgfmathresult$ – Jake Jun 6 '13 at 22:41
  • Also you can tell the loop to do the computation for you with another variable \foreach \x[evaluate=\x as \labelx using int(140+5*(\x-1))] in {1,2,...,9}{..... {$scriptstyle \labelx$};} – percusse Jun 7 '13 at 8:16
13

There are many ways for numerical calculations. The following example uses e-TeX's \numexpr:

\the\numexpr 135 + 5 * \x \relax

Full example:

\documentclass[letterpaper,12pt]{book}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[line width=1pt,line cap=round,x=.5cm,y=.5cm]
  \clip (0,-0.8) rectangle (10,1);
  %   Inequality Line
  \draw   [<->,thick]     (0,0) -- (10,0);
  %   Tick Marks
  \foreach \x in {1,2,...,9}
  \draw (\x,-2pt) -- (\x,2pt) node [anchor=north,below=3pt]
  {$\scriptstyle \the\numexpr135+5*\x\relax$};
  %   Displayed Solution
\end{tikzpicture}
\end{document}

Result

  • 1
    Would it be possible to get a slight explanation of that command? \the\numexpr135+5*\x\relax. For instance what is \relax? – Calhistorian Jun 6 '13 at 23:12
  • 2
    @Calhistorian: \relax is optional and tells \numexpr that the expression is finished. \the (also \number can be used instead) forces \numexpr to trigger the calculation and output the number. More details, see The e-TeX Manual. – Heiko Oberdiek Jun 6 '13 at 23:24
  • Excellent! I will get the manual. Didn't know about that one. – Calhistorian Jun 7 '13 at 0:09
2

There are many ways for numerical calculations inside \foreach loop. Here are two possibilities, one use count, the other evaluate :

\documentclass[preview, border=7mm]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}[line width=1pt,line cap=round,x=.5cm,y=.5cm]
    % axes
    \draw   [<->,thick]     (0,0) -- (10,0);
    % tick marks and numbers below
    \foreach[count=\x] \l in {140,145,...,180}
      \draw (\x,2pt) -- (\x,-2pt) node[blue, below,scale=.5]{$\l$};
    % numbers above
    \foreach[evaluate={\l=int(5*\x+135)}] \x in {1,...,9}
      \node[above,scale=.5,red] at (\x,2pt) {$\l$};
  \end{tikzpicture}
\end{document}

enter image description here

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.