3

I'm trying to write a macro that draws a number line given a number of tick marks to use. However, there are a couple of spots in the code where I try to do arithmetic using the argument of the command, specifically 1/(#1-1+3) and #1-1, that don't seem to want to work. I get an error message saying the following:

Illegal unit of measure (pt inserted).
<to be read again> 
                   -
l.96 \numline{5}

It seems to be treating the minus sign - as a unit of measure, but I'm not sure how that could happen. My full code follow below.

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{ifthen}
\usepackage{amsmath}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\numline}[1]{
    \pgfmathsetmacro{\spacing}{1/(#1-1+3)}%
    \begin{tikzpicture}[scale=\textwidth/1cm]
    \draw[latex-latex,thick] (0,0) -- (1,0);
    \foreach \i in {0,...,#1-1}
        \draw[shift={(1.5*\spacing+\i,0)},color=black,thick] (0pt,5/6pt) -- (0pt,-5/6pt);
    \end{tikzpicture}
}

\numline{5}

\end{document}

1 Answer 1

4

Welcome to TeX.SE! TikZ is smart, but not that smart. TikZ don't perform math operations inside the \foreach expressions but you need to help TikZ a bit. That's why I added \pgfmathtruncatemacro{\imax}{#1-1} and used \imax in the loop.

\documentclass{article}
\usepackage{pgf,tikz}
\usepackage{ifthen}
\usepackage{amsmath}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{calc}

\begin{document}

\newcommand{\numline}[1]{
    \pgfmathsetmacro{\spacing}{1/(#1-1+3)}%
    \begin{tikzpicture}[scale=\textwidth/1cm]
    \draw[latex-latex,thick] (0,0) -- (1,0);
    \pgfmathtruncatemacro{\imax}{#1-1}
    \foreach \i in {0,...,\imax}
        \draw[shift={(1.5*\spacing+\i,0)},color=black,thick] (0pt,5/6pt) -- (0pt,-5/6pt);
    \end{tikzpicture}
}
\numline{5}
\end{document}

Alternatively you can force to perform the integer arithmetic via \numexpr

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows.meta,calc}

\begin{document}

\newcommand{\numline}[1]{
    \pgfmathsetmacro{\spacing}{1/(#1-1+3)}%
    \begin{tikzpicture}[scale=\textwidth/1cm]
    \draw[latex-latex,thick] (0,0) -- (1,0);
    \foreach \i in {0,...,\numexpr#1-1\relax}
        \draw[shift={(1.5*\spacing+\i,0)},color=black,thick] (0pt,5/6pt) -- (0pt,-5/6pt);
    \end{tikzpicture}
}

\numline{5}

\end{document}

My suspicion is that you may want something like this, though.

\documentclass{article}
\usepackage{tikz}
\newcommand{\numline}[1]{
    \pgfmathsetmacro{\spacing}{1/(#1-1+3)}%
    \typeout{\spacing}
\centerline{\begin{tikzpicture}[scale=\textwidth/1cm]
    \draw[latex-latex,thick] (0,0) -- (1,0);
    \pgfmathtruncatemacro{\imax}{#1-1}
    \foreach \i in {0,...,\imax}
        \draw[thick] 
        ({(1.5+\i)*\spacing},5/6pt) -- ({(1.5+\i)*\spacing},-5/6pt);
    \end{tikzpicture}}
}
\begin{document}
\numline{5}
\end{document}
3
  • 3
    You could even use relative coordinates for the second coordinate pair (++(0,-10/6pt)). Also note that 5/6pt only works as expected because the 5 is also interpreted as 5pt (it's the default unit), but when you try 5/6mm it will be interpreted as 5pt/6mm which is approximately 0.29mm.
    – Max
    Aug 8, 2018 at 6:30
  • Thank you; the third solution is indeed exactly what I was looking for! I notice now that I didn't apply the \spacing factor to everything in the original code. What was the problem with using the shift attribute in the \draw command?
    – Arrow M
    Aug 9, 2018 at 19:32
  • @ArrowM There is no real problem but I personally find the other syntax more intuitive.
    – user121799
    Aug 9, 2018 at 19:35

You must log in to answer this question.

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