13

I am transferring a professor's .doc files to LaTeX so that we can do more sophisticated formatting and I ran across a problem.

I would like to recreate this number line in LaTeX.

number line

Help with which package to use and which commands to study would be a great help.

lockstep
  • 250,273
Daniel
  • 513
  • see this: http://tex.stackexchange.com/questions/45006/how-to-plot-intervals-and-points-in-the-real-line – alfC Aug 20 '12 at 22:59

2 Answers2

22

You don't really need any extra packages for that kind of diagram, LaTeX can do that unaided:

enter image description here

\documentclass{article}

\begin{document}
\newcounter{nn}
\setlength\unitlength{2pt}

\begin{picture}(100,100)

\put(10,60){$L_1=114.06$}
\put(70,60){$L_2=119.94$}
\put(50,30){$\mu$}
\put(0,50){\line(1,0){100}}
\multiput(10,50)(10,0){9}{\line(0,1){5}}
\setcounter{nn}{113}%
\multiput(10,40)(10,0){9}{\makebox(0,0){\thenn\stepcounter{nn}}}

\thicklines
\put(20,50){\circle{3}}
\put(80,50){\circle{3}}

\linethickness{2pt}
\put(20,50){\line(1,0){60}}
\end{picture}

\end{document}
azetina
  • 28,884
David Carlisle
  • 757,742
20

You can use TikZ; the manual is great and contains numerous examples; in the following example, the basic constructs are \node and \draw:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
% a straight line segment
\draw (0.5,0) -- (10.5,0);
% the ticks and their labels
\foreach \x  in {1,...,10}
  \draw[xshift=\x cm] (0pt,2pt) -- (0pt,-1pt) node[below,fill=white] {\the\numexpr\x +112\relax};
% the thicker segment
\draw[ultra thick] (2.06,0) -- (8.94,0);
% the labels
\node[fill=white,draw=black,circle,inner sep=2pt,label=above:{$L_1=114.06$}] at (2.12,0) {};
\node[fill=white,draw=black,circle,inner sep=2pt,label=above:{$L_1=119.94$}] at (8.9,0) {};
\node at (5.5,-0.8) {$\mu$};
\end{tikzpicture}

\end{document}

enter image description here

And here's a variation using pgfplots (which internally uses TikZ and is very useful for plots):

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  axis y line=none,
  axis lines=left,
  axis line style={-},
  xmin=112.5,
  xmax=121.5,
  ymin=0,
  ymax=1,
  xlabel=$\mu$,
  scatter/classes={o={mark=*}},
  restrict y to domain=0:1,
  xtick={113,114,...,121}
]
\addplot table [y expr=0,meta index=1, header=false] {
114.06 o
119.94 o
};
\node[coordinate,label=above:{$L_1=114.06$}] at (axis cs:114.06,0.05) {};
\node[coordinate,label=above:{$L_2=119.94$}] at (axis cs:119.94,0.05) {};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Gonzalo Medina
  • 505,128
  • What do the ymin and ymax do? – soandos Aug 21 '12 at 04:06
  • @soandos >.>, the produced graph has a lower bound of 112.5 and an upper bound of 121.5, let's play "pin the tail on the observable." – Jeff Langemeier Aug 21 '12 at 04:30
  • @JeffLangemeier As far as I can tell it slightly changes the height of L1 and L2 (tried 10,100,1000 for ymax) – soandos Aug 21 '12 at 05:12
  • @soandos, And now I get red on my face, yes, the y bound handles the vertical, but since a line graph has no vertical it doesn't really do squat. Tired-dyslexia causes y and x confusion tonight, stellar. – Jeff Langemeier Aug 21 '12 at 05:36
  • @JeffLangemeier don't sweat it – soandos Aug 21 '12 at 05:38
  • What is the function of this snippet?
    {\the\numexpr\x +112\relax}
    
    

    I get that it is adding 112 to the values of x so that the labels reflect the example I provided, but what is the function of "\the", "\numexpr", and "\relax"?

    – Daniel Aug 22 '12 at 16:06
  • @Daniel \relax is a nonexpandable TeX primitive that causes TeX to stop scanning (in this case, for a number); \the is a TeX primitive that basically "gives the value" of some macros. Comments are too short for a detailed explanation, but you can find it in TeX by Topic or in The TeXbook. – Gonzalo Medina Aug 22 '12 at 16:27
  • @Daniel I forgot to mention \numexpr: it is a e-TeX macro for evaluation of numeric expressions. – Gonzalo Medina Aug 23 '12 at 03:02
  • This solution is very useful for me as newbie in latex but I have question how can we replace the label with x1, x2,... instead of number. Thank you –  Oct 30 '12 at 13:23
  • @yant Welcome to TeX.sx! Your question won't be seen by many people here, additionally, comments are not very suitable to place code, so it would be best to repost your question as a fresh one. Follow-up questions like this are more than welcome! Please use the "Ask Question" link for your new question; there you can link to this question to provide the background. – Gonzalo Medina Oct 30 '12 at 22:52