# comma in node name [TikZ]

In my database some objects have name in A,B format, i.e there is comma in between. I wanted to do something like this

\node (A,B) {Comma};
\node (x) {normal};
\draw (A,B) -- (x);


However, this is not permitted. How can I use comma in in the node names, or in general other special characters in node name?

-
Page 217 of the pgf manual (version 3.0.0) says "Your node name should not contain any punctuation like a dot, a comma, or a colon since these are used to detect what kind of coordinate you mean when you reference a node." – Gonzalo Medina Mar 20 '14 at 3:30
That information is helpful, but is there any way to 'escape' it. – DurgaDatta Mar 20 '14 at 3:37
Unlikely, unfortunately. You could try wrapping it in braces to form a group {A,B}, but I honestly doubt that will work. PGF does some really crazy stuff underneath the hood. – Sean Allred Mar 20 '14 at 4:09
@SeanAllred as you suspected, the extra pair of braces doesn't work. – Gonzalo Medina Mar 20 '14 at 4:11
Although I would definitely not advise using commas in node names, you can create nodes with commas in their names \node (A,B) at (2,2) {A,B}; and refer to them using the node cs: \draw (0,0) -- (node cs:name={A,B});. But as others have pointed out it isn't a good idea at all. – Mark Wibrow Mar 20 '14 at 7:03

You can't because TikZ performs tests such as Does the coordinate expression involve a colon? Then go to angle:radius polar syntax. Does it involve a dot? Then switch to (node name.angle) syntax etc.

A code snippet from such parsing is below;

\pgfutil@in@:{#2}%
\ifpgfutil@in@
\let\@next\tikz@parse@polar%
\else%
\pgfutil@in@,{#2}%
\ifpgfutil@in@%
\let\@next\tikz@parse@regular%
\else%
\let\@next\tikz@parse@node%


It doesn't matter what it does but you can feel that there is some decision making going on. Hence it would be too much of a hassle to escape comma or other punctuation and would make your code very fragile.

-

(Highly unrecommended)

Since you have asked and you probably need it, we can start changing catcodes, in this case:

\catcode\,=11


But we are running into a series of problems and even then you will find comma-related situations which would need a special attention. The reason is that commas are used as a list of parameters and point coordinates separator. Therefore, we restrict its use as much as it is possible by using a group.

We can limit a command or several commands with { } or with \begingroup and \endgroup as in this example:

\begingroup
\catcode\,=11
\node (A,B) {Comma};
\endgroup


We can limit one particular tikzpicture environment:

\begingroup
\catcode\,=11
\begin{tikzpicture}
\node (A,B) {Comma};
\end{tikzpicture}
\endgroup


Or even all tikzpicture environments, e.g. by:

\tikzset{every picture/.style={execute at begin picture={\catcode\,=11}}


This is an example limiting one TikZ environment:

\documentclass{article}
\usepackage{tikz}
\begin{document}
% All environments wrapper...
%\tikzset{every picture/.style={execute at begin picture={\catcode\,=11}}
% One environment wrapper...
\begingroup
\catcode\,=11
\begin{tikzpicture}
% One line wrapper...
%\begingroup
%\catcode\,=11
%\node (A,B) {Comma};
%\endgroup
\node (A,B) {Comma};
\node[xshift=3cm] (x) {normal};
% It is not working when we add ", draw".
%\draw (x) -- (1cm,2cm);
%\draw (A,B) -- (x); % We cannot use "-- (1cm,2cm)".
\end{tikzpicture}
\endgroup
\end{document}


We can also restrict a particular area and avoid using commas directly in it with TikZ tools, because we are fixing a list of parameters, e.g. [xshift=3cm,draw], as well as a pair of point coordinates, e.g. (1cm,2cm).

In the first case, we can define a style outside the limiting group, for instance:

\tikzset{mystyle/.style={xshift=3cm,draw}}


In the second case, we can use \coordinate command, for instance:

\coordinate (myright) at (1cm,2cm);


You will probably run into another situations as in \pgfmathparse{pow(2,4)}, I am afraid, where your attention would be needed. This is an example of this method:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikzset{mystyle/.style={xshift=3cm,draw}}
\begin{tikzpicture}
\coordinate (myright) at (1cm,2cm);
\begingroup
\catcode\,=11
\node (A,B) {Comma};
\node[mystyle] (x) {normal};
\draw (A,B) -- (x) -- (myright);
\endgroup
\end{tikzpicture}
\end{document}


I would highly recommend you to rename object names, it doesn't have to be at the level of database, you can use the generated TeX code itself for this purpose.

In your minimal working example there would be a need of change from A,B to AB or to A-B etc. We are getting to our final and an appropriate example, if I may put it this way:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node (A-B) {Comma};
\node[xshift=3cm] (x) {normal};
\draw (A-B) -- (x);
\end{tikzpicture}
\end{document}

-
This will almost always break the typical TikZ pictures. category code change is used for example in babel and there has been a lot of questions because of that. – percusse Mar 20 '14 at 8:37