1

I'm trying to create number tracks for my students. They should look something like this.

enter image description here

I'd like to create a macro that makes an arbitrary number of them of an arbitrary length (x-max).

Note: The command \numbertrack{20} compiles to make the above image. The macro I made to make a whole bunch of those, \mytracks, does not compile.

\documentclass[landscape]{article}

\usepackage[margin=2cm]{geometry}
\usepackage{tikz}

\pagestyle{empty}

\def\myscale{1.15}

\newcommand{\numbertrack}[1]
{
\begin{tikzpicture}[scale=\myscale]
\draw (0,0)--(#1,0);
\draw (0,1)--(#1,1);
\foreach \x in {0,1,2,...,#1}
\draw (\x,0)--(\x,1);
\foreach \x in {1,2,3,...,#1}
\node[anchor=center] at (\x-0.5,0.5) {\huge\x};
\end{tikzpicture}
\\
\vspace{1cm}
}

\newcommand{\mytracks}
{
\foreach \y in {1,2,3,...,20}
\numbertrack{\y}
}

\begin{document}

\mytracks

\end{document}
6
  • 1
    with \foreach\y in {1,2,3,...,20}{ \numbertrack{\y} } , the example compiles for me
    – daleif
    Commented Nov 8, 2016 at 9:01
  • Yes, that works! Thanks. I didn't know I need the curly braces if there was only one command after a \foreach... I don't remember using them in TikZ unless there were at least two within the forloop. Commented Nov 8, 2016 at 9:06
  • Not sure if I should start a new thread for this, but the first number track (only 1 square) looks odd when it compiles. Why does this happen? Commented Nov 8, 2016 at 9:08
  • probably your lists, 1,2,3,...,2. Why don't you just draw them as fixed size nodes with their edges draw? Then no need to draw the boxes manually
    – daleif
    Commented Nov 8, 2016 at 9:11
  • @daleif I'm a bit of a programming newb. What did you mean by "fixed size nodes"? And what manual drawing is occurring? Doesn't the macro do it for me? Commented Nov 8, 2016 at 9:13

2 Answers 2

2

Something like this?

\documentclass{article}
\usepackage{tikz}
\usepackage[margin=10mm]{geometry}
\newcommand\numbertrack[2][1.15]{%
  \begin{tikzpicture}[scale=#1,
          mybox/.style={rectangle,draw,
                        minimum height=#1*10mm,
                        minimum width=#1*10mm}
    ]
    \foreach \num [count=\x] in {#2} {
      \node[mybox] at (\x,0){\num};
    }
  \end{tikzpicture}
}
\begin{document}

  \numbertrack{1,2,3,...,16}

  \numbertrack{12,4,3,5,3,5,2,3,4}

  \numbertrack[0.5]{a,d,3,c,s,g,d,a}

\end{document}

This produces:

enter image description here

The nodes are drawn with a rectangular boundary with the minimum width and height set according to the scale -- this is the only tricky part and is done by the #1*10mm in the style for mybox. For an explanation of the minimum height and minimum width settings see: displaying-a-node-inside-a-foreach-loop. Except for the scaling, these two questions are very similar.

Notice that the \numbertrack macro takes an optional argument for the scale. So the last example, \numbertrack[0.5]{a,d,3,c,s,g,d,a}, sets the scale to 0.5. By default, the scale is set to 1.15 as in the OP.

EDIT

If I have misunderstood and instead you just want the numbers from 1,2,...,x then a slight variation on the code above works:

\documentclass{article}
\usepackage{tikz}
\usepackage[margin=10mm]{geometry}
\newcommand\numbertrack[2][1.15]{%
  \begin{tikzpicture}[scale=#1,
          mybox/.style={rectangle,draw,
                        minimum height=#1*10mm,
                        minimum width=#1*10mm}
    ]
    \foreach \num [count=\x] in {1,...,#2} {
      \node[mybox] at (\x,0){\num};
    }
  \end{tikzpicture}
}
\begin{document}

  \numbertrack{16}

  \numbertrack{12}

  \numbertrack[0.5]{3}

  \numbertrack[2]{1}

  \numbertrack[2]{0}

  \numbertrack[2]{-5}

\end{document}

This produces:

enter image description here

In particular, 1 is not a special case. Negative numbers work too, but probably not as you'd want:(

1
  • \foreach \num [count=\x] in {#2} is just clever!
    – ebosi
    Commented Nov 8, 2016 at 9:59
1

Here is a method using just nodes. Because of the \foreach, 1 is a special case

\documentclass[landscape]{article}

\usepackage[margin=2cm]{geometry}
\usepackage{tikz,etoolbox}
\usetikzlibrary{calc}

\pagestyle{empty}

\def\myscale{1.15}

\newcommand\drawnumber[1]{
  \node[draw,anchor=west,
        minimum width=5mm,
        minimum height=5mm] (B) at (A) {#1};
  \coordinate (A) at ($(B.east)+(-\pgflinewidth,0)$);
}

\newcommand{\numbertrack}[1]
{
  \noindent
\begin{tikzpicture}[scale=\myscale]
  \coordinate (A) at (0,0); % initialize
  \ifnum#1=1\drawnumber{1}\fi
  \ifnum#1>1\relax
  \foreach\x in {1,2,...,#1}{
    \drawnumber{\x}
  }
  \fi
\end{tikzpicture}
\par
\bigskip
}

\newcommand{\mytracks}
{
    \foreach \y in {1,2,3,...,20} {
      \numbertrack{\y}
    }
 }

\begin{document}

\mytracks

\end{document}
2
  • 1
    I think the \ifnum#1=2 condition is unnecessary since \numbertrack{2} would work (\foreach \x in {1,2,...,2}{\x } prints 1 2 ).
    – ebosi
    Commented Nov 8, 2016 at 9:46
  • @ebo you're right, I've updated the example
    – daleif
    Commented Nov 8, 2016 at 9:56

You must log in to answer this question.

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