9

I would like to create a command that allows me to create a number line and circle two given values.

enter image description here

I have the code for the number line:

\documentclass{article} \usepackage{tikz}


\newcommand{\NL}[2]{\begin{center} \begin{tikzpicture}[scale =
1.07] \draw[latex-latex] (#1-0.5,0) -- (#2+0.5,0) ;  \foreach \x in  {#1,...,#2}  \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt); \foreach \x in {#1,...,#2}  \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below]  {$\x$}; \end{tikzpicture} \end{center}}

\begin{document} \NLTwo{1}{10}

\end{document}

And this code makes nice looking circles:

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\begin{document}
Numbers aligned with the text:  \circled{1} \circled{2} \circled{3} end.
\end{document}

...and I'm hopeful these ideas can be combined.

For example, I would like to use a command like \NLTwo{1}{10}{3}{5} that produces a number line with numbers from 1 to 10 (inputs 1 and 2) and circles numbers 3 and 5 (inputs 3 and 4) looking something like

enter image description here

Thanks so much, Wendy

2

3 Answers 3

7

Vanilla solution:

\documentclass{article} 
\usepackage{tikz}

\begin{document} 
    \begin{tikzpicture}
        \draw[-latex] (-1,0)--(10.5,0);
        \foreach \x in {0,1,...,10}
        \draw (\x,.1)--(\x,-.1) node[below] () {\small \x};
        \node[below,shape=circle,draw,inner sep=2pt] at (3,-.1) {3};
        \node[below,shape=circle,draw,inner sep=2pt] at (5,-.1) {5};
    \end{tikzpicture}       
\end{document}

Output:

enter image description here

5
  • Thank you, adding the code \node[below,shape=circle,draw,inner sep=2pt] at (3,-.1) {3}; below my NL code works really well! Mar 1 at 1:21
  • I am not quite advanced enough to move beyond vanilla, but greatly appreciate your effort. One day I hope to progress! Mar 1 at 1:22
  • @WendyTaylor You are wellcomed . My 'vanilla' code works to solve Your problem. Mar 1 at 1:26
  • Here's the code I ended up going with. \newcommand{\NLTwo}[4]{\begin{center} \begin{tikzpicture}[scale = 1.07] \draw[latex-latex] (#1-0.5,0) -- (#2+0.5,0) ; \foreach \x in {#1,...,#2} \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt); \foreach \x in {#1,...,#2} \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] {$\x$}; \node[below,shape=circle,draw,inner sep=9pt] at (#3,-.01) {}; \node[below,shape=circle,draw,inner sep=9pt] at (#4,-.01) {}; \end{tikzpicture} \end{center}} Mar 1 at 1:30
  • I specified the circles to be larger so I didn't need to repeat the number (removing the shaddow) and I've made the circles large enough to work with two digit numbers. Thanks again @raffaele santor Mar 1 at 1:30
9

Although you could use the syntax you suggested, I'd use pgfkeys and a key-value interface. That's easier to extend and keep track of. I also make the argument to \NL optional. Without an optional argument, a simple numberline from 1 to 10 is drawn with nothing circled. With an optional argument, the start end and circle numbers can be specified, as well as regular pgf/tikz options.

For example,

\NL
  
\NL[circle={3,5}]

\NL[every node/.append style={fill=blue!25!black!5},circle={2,4,6,8},start=-1,end=12,thick,draw=blue!25!black,text=blue!25!black]

produces three number lines with increasing degrees of customisation.

three number lines

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
% ateb: https://tex.stackexchange.com/a/711912/ addaswyd o gwestiwn Wendy Taylor: https://tex.stackexchange.com/q/711907/
\tikzset{%
  numberline/.search also={/tikz,/pgf},
  numberline/.cd,
  start/.store in=\numberlinestart,
  end/.store in=\numberlineend,
  circle/.store in=\numberlinecircle,
  start=1,
  end=10,
  circle={},
}

\NewDocumentCommand \NL { O{} }
{%
  \begin{center}
    \begin{tikzpicture}[x=1.07cm,y=1.07cm,numberline/.cd,#1]
      \draw [Latex-Latex] (\numberlinestart-0.5,0pt) -- (\numberlineend+0.5,0pt);
      \foreach \i in {\numberlinestart,...,\numberlineend} 
      \draw (\i,3pt) -- (\i,-3pt) node (n-\i) [below] {$\i$}; 
      \foreach \i in \numberlinecircle 
      \node [circle,draw,inner sep=1.5pt] at (n-\i) {\phantom{$\i$}};
    \end{tikzpicture}
  \end{center}%
}


\begin{document}

\NL
  
\NL[circle={3,5}]

\NL[every node/.append style={fill=blue!25!black!5},circle={2,4,6,8},start=-1,end=12,thick,draw=blue!25!black,text=blue!25!black]

\end{document}
7

A solution of another flavor; Just by cleaning the code, I added an additional variable to, after drawing the arrow, do its style as required for each requested number within the range, of course.

RESULT:

enter image description here

MWE:

\documentclass{article}
\usepackage{tikz}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{\node[shape=circle,draw,inner sep=2pt] (char) {#1};}}
\newcommand{\NL}[3]{
    \begin{center}
        \begin{tikzpicture}[scale =1.07]
            \draw[latex-latex](#1-0.5,0) -- (#2+0.5,0);
            \foreach \x in  {#1,...,#2}{
                \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] (N-\x) {$\x$};
            }
            \foreach \y/\z in  {#3}{
                \node[draw=\z,circle,minimum size=13pt] at (N-\y.center){};
            }
        \end{tikzpicture}
    \end{center}}
\begin{document}
    Numbers aligned with the text:  \circled{2} \circled{4} \circled{6} \circled{9} end.
    \NL{1}{10}{2/red,4/blue,6/orange,9/green!50!black}
    
    Another result:
    \NL{-3}{7}{-2/red,0/blue,4/orange,7/green!50!black}
    
\end{document}

You must log in to answer this question.

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