2

I am a LyX user and I would like to use a line plot in one of my papers. This is simply a line with few identified points. For me the points of interest are 24, 32 and 38. Could you please tell what is the easiest way to accomplish this? I have heard about the packaged pgfplots but I do not know how to use it. It also seems an overkill for my case.

All suggestions are welcome, thank you in advance.

2
  • Yes, pgfplots is probably the best way to do that. But not sure what 24, 32 and 38 represent. Are those points on a number line? Nov 17, 2014 at 20:38
  • @PeterGrill Imagine points on a line. There is no need for a vertical axis. The point is to show how far they all lie from each other in just 1 dimension.
    – JohnK
    Nov 17, 2014 at 20:39

1 Answer 1

2

You can use tikz for simple drawings such as this:

enter image description here

If you are doing these types of figure often, then it makes sense to wrap them in a macro where you can define

  • #1 = the start value,
  • #2 = end value, and the
  • #3 = list of points to mark.

Then,

\DrawNumberLine{18}{42}{24,32,38}
\DrawNumberLine{0}{50}{24,32,38}
\DrawNumberLine{0}{25}{2,7,12,20,22}

yields:

enter image description here

Code: Basic Version

\documentclass{article}
\usepackage{tikz}

\tikzset{tick style/.style={thick, black}}
\begin{document}
\begin{tikzpicture}[scale=0.5]
\draw [thin, gray, -latex] (18,0) -- (42,0);
\foreach \Tick in {20,25,...,40} {
    \draw [tick style] (\Tick,1.5ex) -- (\Tick,-1.5ex) node [below] {$\Tick$} ;
}
\foreach \X in {24, 32, 38} {
    \draw [fill=red] (\X,0) circle (4pt) node [above, blue] {$\X$};
}
\end{tikzpicture}
\end{document}

Code: Macro Version

\documentclass{article}
\usepackage{graphicx}
\usepackage{tikz}
%\usepackage{showframe}

\tikzset{number line style/.style={thin, gray, -latex}}
\tikzset{tick style/.style={thick, black}}

%% http://tex.stackexchange.com/questions/175507/scale-tikzpicture-to-enclosing-minipage
\newcommand*{\MyScale}{1}%
\newcommand*{\MyResizeBox}[2]{%
    % #1 = width
    % #2 = tikzpicture
    \renewcommand*{\MyScale}{1}%
    \sbox0{#2}%
    \pgfmathsetmacro{\MyScale}{0.98*#1/\wd0}%
    #2%
}%

\newcommand*{\TickStep}{5}
\newcommand*{\DrawNumberLine}[3]{%
    % #1 = x min
    % #2 = x max
    % #3 = comma separated list of points to show
    \MyResizeBox{\linewidth}{%
        \noindent
        \begin{tikzpicture}[x=\MyScale cm, y=1cm]
            \draw [number line style] (#1,0) -- (#2,0);
            \pgfmathtruncatemacro{\TickStart}{\TickStep*int(#1/\TickStep)}%
            \pgfmathtruncatemacro{\TickEnd}{\TickStep*int(#2/\TickStep -1)}%
            \pgfmathtruncatemacro{\NumOfTicks}{1+(\TickEnd-\TickStart)/\TickStep}%
            \foreach \Tick in {1,...,\NumOfTicks} {
                \pgfmathtruncatemacro{\CurrentTick}{\TickStart+\Tick*\TickStep}%
                \draw [tick style] (\CurrentTick,0.5ex) -- (\CurrentTick,-0.5ex) 
                    node [below] {\tiny$\CurrentTick$} ;
            }
            \foreach \X in {#3} {
                \draw [fill=red] (\X,0) circle (2pt) 
                    node [above, blue] {\tiny$\X$};
            }
        \end{tikzpicture}%
    }%
}

\begin{document}
\DrawNumberLine{18}{42}{24,32,38}%
\medskip\par
\DrawNumberLine{0}{50}{24,32,38}%
\medskip\par
\DrawNumberLine{0}{25}{2,7,12,20,22}%
\end{document}
1
  • Works wonderfully, thank you very much. There is plenty for me to learn, this is an amazing package.
    – JohnK
    Nov 17, 2014 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .