4

The documentation of tikz gives following example containing the \verb command in the node text (Section 2.21 Adding Text, v3):

\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\draw (0.5,0.5) node [fill=yellow!80!black]
                       {Text at \verb!node 1!}
     -- (1.5,1.5) node {Text at \verb!node 2!};
\end{tikzpicture}

If I want to use the label or pin functionality for nodes, it does not work as expected:

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\draw (0.5,0.5) node [fill=yellow!80!black]
                       {Text at \verb!a_b!}
                     -- (1.5,1.5) node[label={\verb!a_b!}] {Text at \verb!a_b!};
\end{tikzpicture}
\end{document}

The error message given is ! Missing $ inserted. due to the _ I want to use.

2
\documentclass[tikz]{standalone}
\usepackage{fancyvrb} \DefineShortVerb{\|}
\begin{document}
\SaveVerb{Verb}|a_b|
\begin{tikzpicture}
    \draw (0,0) rectangle (2,2);
    \draw (0.5,0.5) node [fill=yellow!80!black]{Text at |a_b|} -- (1.5,1.5) 
                    node[label=\UseVerb{Verb}] {Text at |a_b|};
\end{tikzpicture}
\end{document}
1

I really do not fully understand what's going on but I can offer a workaround. To this end, let me first mention that creating a new \savebox and trying it to fill with \savebox\verbox{\verb!a_b!} fails. However, a lrbox works, so I propose to store the content of the label in a \savebox first and then use it.

\documentclass[tikz]{standalone}
\newsavebox\verbox
\begin{document}
\begin{lrbox}{\verbox}
\verb!a_b!
\end{lrbox}
% fails:
%\savebox\verbox{\verb!a_b!}
\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\draw (0.5,0.5) node [fill=yellow!80!black]
                       {Text at \verb!a_b!}
                     -- (1.5,1.5) node[label={\usebox\verbox}] {Text at \verb!a_b!};
\end{tikzpicture}
\end{document}

enter image description here

1

Another workaround producing same output as in @marmot answer.

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw (0,0) rectangle (2,2);
\draw (0.5,0.5) node [fill=yellow!80!black]
                       {Text at \verb!a_b!}
                     -- (1.5,1.5) node[label=\texttt{a\string_b}] {Text at \verb!a_b!};
\end{tikzpicture}
\end{document}
  • \texttt{a\_b} should also work – user2478 Nov 12 '18 at 17:48
  • 1
    @Herbert this is what I had done initially but notice that it does not give same result in OT1 default encoding. – user4686 Nov 12 '18 at 18:23
  • I cannot see a problem with \_$\_$ – user2478 Nov 12 '18 at 18:32
  • 1
    @Herbert try it out with tt font. I told you it gives different result, and I don't see why I would have said so if not. – user4686 Nov 12 '18 at 18:39
  • 1
    @Herbert I think we have a quiproquo here. I am talking about comparing \string_ with \_. Notice that the OP is asking for \verb|a_b|. You can reproduce same with \texttt{a\string_b} not with \texttt{a\_b} in OT1 encoding. – user4686 Nov 12 '18 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.