3

I am trying to create a simple (hopefully) macro for myself that will make drawing number lines with TikZ more efficient. I can make such lines manually, but since I will need so many similar pictures, a macro would be super useful.

One other note, though: I actually don't want the closing brace for \tikz{} in the macro because I would like to be able to add other paths inside the macro when I actually use it in the body.

Is any of this possible?

In my MWE, I manually make a number line from 0 to 3 with tick marks at every quarter. That worked. The macro meant to create exactly the same thing did not work. What am I missing here?

Thanks!

\documentclass{article}

\usepackage{tikz}

% THE MACRO THAT DIDN'T WORK
\newcommand{\NL}[4]
{\tikz[xscale=[#1],yscale=[#2]]
 {
 \draw(0,0)--([#3],0);
 \foreach \x in {0,...,[#3]}
  \node[below] at (\x,-0.2) {\x};
 \foreach \x in {0,...,[#3]*[#4]}
  \draw (\x,-0.2)--(\x,0.2);
 }
}

\begin{document}

Number line from 0 to 3 with tick marks at every quarter.

MANUAL WAY
\tikz[xscale=4,yscale=1.2]
{\draw (0,0)--(3,0);
 \foreach \x in {0,...,3}
  \node[below] at (\x,-0.3) {\x};
 \foreach \x in {0,...,12}
  \draw (\x/4,-0.2)--(\x/4,0.2);
}


ATTEMPT TO MAKE EXACTLY THE SAME NUMBER LINE WITH THE MACRO
\NL{4}{1.2}{3}{4}
% xscale of 4, yscale of 1.2, x-axis from 0 to 3, denominator of quarters

\end{document}

1 Answer 1

2

The basic elements are there. However, you need to remove the square brackets around the arguments and do the calculation #3*#4 beforehand:

\newcommand{\NL}[4]
{\tikz[xscale=#1,yscale=#2]
 {
 \draw(0,0)--(#3,0);
 \foreach \x in {0,...,#3}
  \node[below] at (\x,-0.2) {\x};
  \pgfmathparse{#3*#4}
 \foreach \x in {0,...,\pgfmathresult}
  \draw (\x/#4,-0.2)--(\x/#4,0.2);
 }%
}

The complete code:

\documentclass{article}
\usepackage{tikz}

\newcommand{\NL}[4]
{\tikz[xscale=#1,yscale=#2]
 {
 \draw(0,0)--(#3,0);
 \foreach \x in {0,...,#3}
  \node[below] at (\x,-0.2) {\x};
  \pgfmathparse{#3*#4}
 \foreach \x in {0,...,\pgfmathresult}
  \draw (\x/#4,-0.2)--(\x/#4,0.2);
 }%
}

\begin{document}

Number line from 0 to 3 with tick marks at every quarter.

MANUAL WAY

\tikz[xscale=4,yscale=1.2]
{\draw (0,0)--(3,0);
 \foreach \x in {0,...,3}
  \node[below] at (\x,-0.3) {\x};
 \foreach \x in {0,...,12}
  \draw (\x/4,-0.2)--(\x/4,0.2);
}


ATTEMPT TO MAKE EXACTLY THE SAME NUMBER LINE WITH THE MACRO

\NL{4}{1.2}{3}{4}

\end{document}

The result:

enter image description here

If you are interested in adding some additional stuff to the tikzpicture, having an umatched closing brace in the definition is not a sensible approach; I'd suggest you to use something like

\newcommand{\NL}[2]{
 \draw(0,0)--(#1,0);
 \foreach \x in {0,...,#1}
  \node[below] at (\x,-0.2) {\x};
  \pgfmathparse{#1*#2}
  \foreach \x in {0,...,\pgfmathresult}
  \draw (\x/#2,-0.2)--(\x/#2,0.2);
}

and then

\begin{tikzpicture}[xscale=4,yscale=1.2]
\NL{3}{4}
% other tikz stuff
\end{tikzpicture}
5
  • Awesome, thanks! My question about the additional stuff in the tikzpictures: Why is it not good to have an unmatched closing brace? Wouldn't it save me quite a bit of typing? Sep 15, 2015 at 21:22
  • @WeCanLearnAnything That would be syntactically and semantically incorrect. Let's assume we try \newcommand{\NL}[2]{ \tikz{ \draw(0,0)--(#1,0); \foreach \x in {0,...,#1} \node[below] at (\x,-0.2) {\x}; \pgfmathparse{#1*#2} \foreach \x in {0,...,\pgfmathresult} \draw (\x/#2,-0.2)--(\x/#2,0.2); } (supposedly the final brace is missing) now we need \NL{3}{4}} (with an extra brace) and besides it's useless and won't work. This, Sep 15, 2015 at 22:59
  • \newcommand{\NL}[2]{ \tikz\bgroup \draw(0,0)--(#1,0); \foreach \x in {0,...,#1} \node[below] at (\x,-0.2) {\x}; \pgfmathparse{#1*#2} \foreach \x in {0,...,\pgfmathresult} \draw (\x/#2,-0.2)--(\x/#2,0.2); } allows to do things like \NL{3}{4}\draw (0,0) -- (3,5);\egroup but again, might fail. All in all, leaving a brace open in the definition here might be more problematic than beneficial. Sep 15, 2015 at 23:00
  • Looking at the last bit of code that you wrote: Is there any way to save those optional arguments {3}{4} so that I can use them for the % other tikz stuff? Or are they no longer accessible because they are effectively within the macro's { }? Sep 19, 2015 at 20:11
  • @WeCanLearnAnything They are mandatory, not optional. You could say something like \newcommand{\NL}[2]{ \gdef\parami{#1} \gdef\paramii{#2} \draw(0,0)--(#1,0); \foreach \x in {0,...,#1} \node[below] at (\x,-0.2) {\x}; \pgfmathparse{#1*#2} \foreach \x in {0,...,\pgfmathresult} \draw (\x/#2,-0.2)--(\x/#2,0.2); } and then you can use \parami and \paramii in % other tikz stuff. Sep 19, 2015 at 23:38

You must log in to answer this question.

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