1

I know that the TeX macro language, unlike most programming languages, allows nothing but letters in macro names. But what about the names within tikz?

MWE 1

Something like graph1 and graph2 seem to work.

\documentclass[tikz]{standalone}

\tikzset{
   graph1/.pic={
        \draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
    }
}

\tikzset{
   graph2/.pic={
        \draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
    }
}

\begin{document}

\begin{tikzpicture}
\draw[help lines] (-0.99,-0.99) grid (8.99,8.99);
\pic{graph1};
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-0.99,-0.99) grid (8.99,8.99);
\pic{graph2};
\end{tikzpicture}

\end{document}

MWE 2

Even 1 and 2 work.

\documentclass[tikz]{standalone}

\tikzset{
   1/.pic={
        \draw[very thick,color=black,domain=-1:9] plot (\x,{\x});
    }
}

\tikzset{
   2/.pic={
        \draw[very thick,color=black,domain=-1:9] plot (\x,{0.5*\x});
    }
}

\begin{document}

\begin{tikzpicture}
\draw[help lines] (-0.99,-0.99) grid (8.99,8.99);
\pic{1};
\end{tikzpicture}

\begin{tikzpicture}
\draw[help lines] (-0.99,-0.99) grid (8.99,8.99);
\pic{2};
\end{tikzpicture}

\end{document}

Does it have any disadvantage of using numbers in tikz style names? I'm just asking because TeX doesn't allow numbers in macro names.

  • It does not have any real disadvantages and you can use numbers in macro names in TeX, e.g. \expandafter\def\csname mymacro1\endcsname{Im a 1}, but it is true that you cannot use numbers in \newcommand or \def without the \csname trick (easily). (If you use declare function, you can also use numbers, but not at the beginning of the function.) –  Dec 19 '19 at 19:17
  • TikZ uses pgfkeys to store most things. I would assume that pgfkeys in fact uses \csname with some naming protocol prefix, since they have similar restrictions (no formatted text). – John Kormylo Dec 19 '19 at 21:22

1 Answers1

1

It is not really true that you cannot use numbers in macro names. For instance,

\documentclass{article}
\begin{document}
\expandafter\def\csname mymacro1\endcsname{Im a 1}

\csname mymacro1\endcsname
\end{document}

works. TikZ/pgf uses basically this trick to allow for key names that contain spaces and numbers (and so on).

To answer your question, it is per se not dangerous to use numbers in style name. pgf does this all time, e.g. there is column 1/.style=.... However, I do not recommend to use just a number for a pic name. Even though there is nothing wrong with that per se, it is foreseeable that you will get confused.