# Hashtag label with tikzpicture

I'm trying to compile the tree below and its working. The problem is that whenever I change the label of DP or LP to \#P (i.e. \node(\#P) {\#P}; \#), I get the following error message:

! Missing \endcsname inserted.
\#
l.17 ...LP}; L [.YP Y [.FP ] ] ] ] ] ] ]
]
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
Missing character: There is no # in font nullfont!
Missing character: There is no P in font nullfont!

\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{tikz-qtree-compat}

\tikzset{every tree node/.style={baseline=(top.base), level distance=2em, sibling distance=4em, align=center, parent anchor=south, child anchor=north, anchor=north}, sibling distance=15pt}

\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}  [inv/.style={overlay, coordinate  }]

\Tree [.XP X [.ZP Z [.\node(DP) {DP}; D    [.\node(LP) {LP}; L      [.YP Y [.FP     ] ] ] ] ]  ] ] ]

\node [ right=1cm of DP,font=\itshape] (X) {Blah};

\draw[<-] (X.west) -- (DP);

\node [ right=1cm of LP,font=\itshape] (X) {Blah};

\draw[<-] (X.west) -- (LP);

\end{tikzpicture}

\end{document}


• Did you try \# instead of just #? – Jasper Habicht Feb 19 '18 at 13:22
• Ya I did still doesn't work – user135370 Feb 19 '18 at 13:29

# is a special character in TeX which you have to escape if you want it printed. So, you have to escape it in normal text. However, names of nodes do not allow macros, so you cannot use \# in the node name.

Hence, I suggest you to use another letter character like x to replace \# in the node name (which is only used internally in your code and won't be typeset anyways).

Note that even most “special characters [...] including commas, semicolons, hyphens, braces, dots, parentheses, slashes, dashes, and more” (cited the TikZ manual) cannot be used in node names.

On changing \catcode, see here.

EDIT Possible solutions: 1) just name the node differently from its text; 2) if node name and node text have to be identical, you could use a macro to replace xD to \#D:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usepackage{xstring}
\newcommand{\xtohash}[1]{\StrSubstitute[0]{#1}{x}{\#}}

\begin{document}

\begin{tikzpicture}

\node at (0,0) (xD) {\#D};

\node at (0,1) (xD) {\xtohash{xD}};

\end{tikzpicture}

\end{document}

• Thank you so much for your response. Do you suggest something like defining a new command that refers to \# without using \# in the code? I tried that before and still, it does not work. If you please can show me how it is done that would be great. – user135370 Feb 19 '18 at 14:22
• Is it a problem to use x in the node name and \# in the node text? Do both have to be the same necessarily, because they are, say, generated by some other code? – Jasper Habicht Feb 19 '18 at 16:39
• If node name and node text have to be identical, you could write a macro to change the node name xD to \#D to be used as node text. Would this be a feasible solution? – Jasper Habicht Feb 19 '18 at 16:56
• Both have to be identical. I'm kinda new to Latex, so I do not know how to do that (name xD to \#D) but thanks anyway. – user135370 Feb 19 '18 at 17:00
• @Demo See my edit. – Jasper Habicht Feb 19 '18 at 17:12