1
\documentclass{article}
\usepackage{tikz}

\begin{document}

% See also https://tex.stackexchange.com/questions/187356/
\begin{tikzpicture}
    \node[align = left] at (0,6) {Line One\\Line Two\\ [Line Three]}; % <-- Problem
    \node[align = left] at (0,4) {Line One\\Line Two\\ Line Three}; % <-- No Problem
    \node[align = left] at (0,2) {Line One\\Line Two\\\relax [Line Three]}; % <-- No Problem
    \node[align = left] at (0,0) {Line One\\Line Two\newline [Line Three]}; % <-- Problem
\end{tikzpicture}
% \linebreak\relax also does not work.

\end{document}
  • I randomly found a problem in tikz today.
  • I want to have a line break within a node.
  • When the line break \\ is before a square bracket [ then I got an error (! Missing number, treated as zero.).
  • Searching the web brought me to Linebreak followed by bracket.
  • \\\relax works but \newline does not work (\linebreak\relax also does not work).
  • Question: Is there a "best practice" to address this problem in a tikz node?

PS: I also hope that this question helps others with the same problem to find this post.

  • 4
    This has nothing to do with tikz but also happens outside. \\ has an optional argument, a distance, e.g. \\[1em]. If you fill it with something different than a distance, this gives an error. You can use \\ \mbox{}[] or similar to avoid the problem. –  Mar 27 '21 at 02:41
  • @user238301 Thanks for the suggestion (\mbox) and the explanation that is has nothing to do with tikz in particular. – Dr. Manuel Kuehner Mar 27 '21 at 02:42
  • 3
    Another option is to use a brace group: e.g. Two\\ {[Line Three]}; instead of Two\\ [Line Three];. This prevents the \\\ treating the square bracket as the start of an optional parameter. – Peter Grill Mar 27 '21 at 03:06
  • 1
    @peter_gill Ah, thanks. This is also a good solution. Feel free to add an answer. – Dr. Manuel Kuehner Mar 27 '21 at 03:08
  • @Dr.ManuelKuehner: I see comments here! – mahmood Jun 13 '21 at 13:54
  • @mahmood You are right, this question could be closed because it is answered in the comments. I wanted to give the helpful user a chance to provide a proper answer – Dr. Manuel Kuehner Jun 13 '21 at 14:44
  • 4
    I’m voting to close this question because solved in comments. – Dr. Manuel Kuehner Jun 13 '21 at 14:46

0 Answers0