5

Why doesn't tikz accept multi-line text within font commands like \texttt and \textbf?

For instance, compilation of the document

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
  % this node works
  \node[draw,align=left] (a) {Normal text\\works.};

  % this node gives an error
  \node[draw,align=left] [below of=a] {\texttt{Typewritter text is\\rejected.}};
\end{tikzpicture}

\end{document}

fails with the error

! Undefined control sequence.
\tikz@invoke@collected@onpath ...mmand \tikz@temp 
                                                  \pgf@stop \tikz@node@is@a@...
l.10 ... {\texttt{Typewritter text is\\rejected.}}

What is a good solution for this problem?

  • 1
    You are trying to use a paragraph as an argument to the \texttt. You can use font=\ttfamily instead as the node option. Also note that TikZ modifies double backslash so it's not directly TeX rules inside a node. In fact, for line breaking inside the node TikZ uses a minipage. – percusse Jun 16 '13 at 9:24
  • @percusse, would you write an answer from your comment? – Romildo Jun 16 '13 at 9:31
4

Specify the font in the node attributes:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
  \node[draw,align=left] (a) {Normal text\\works.};
  \node[draw,align=left,font=\ttfamily] [below of=a] {Typewriter text is\\not rejected.};
\end{tikzpicture}

\end{document}

enter image description here

  • That's the way to do it! (On your way to the tikz-pgf gold badge!) – Jake Jun 16 '13 at 14:08
  • 1
    @Jake I didn't even look at the manual. ;-) But the present status is "560 tikz-pgf × 98", a bit far from the gold badge. ;-) – egreg Jun 16 '13 at 14:16
1

Add text width to make the line breakable:

\documentclass[tikz]{standalone}

\begin{document}

\begin{tikzpicture}
  % this node works
  \node[draw,align=left] (a) {Normal text\\works.};

  % this node gives an error
  \node[draw,align=left, text width = 3cm] [below of=a] {\texttt{Typewriter text\\is not rejected.}};
\end{tikzpicture}

\end{document}
  • That workaround has the disadvantage of specifying an arbitrary text width. I would like to have the node width automatically calculated, as happens with the a node in the example. – Romildo Jun 16 '13 at 9:06

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.