161

This is my code, which doesn't work:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz \node {start\linebreak{}stop};
\end{document}

The \linebreak command doesn't work. In order to make it working I have to say \node[text width=5em]... Is there any other method to make it working, without explicit specification of the node text width?

5
  • 13
    Please vote to close duplicates, do not downvote.
    – Svante
    Commented Jul 20, 2010 at 13:38
  • 4
    This is not a duplicate. I'm not going to use \nodepart, I just want to use \linebreak. I have many lines in the node, much more than \nodepart allows (up to 4).
    – yegor256
    Commented Jul 20, 2010 at 14:30
  • Related: Manual/automatic line breaks and text alignment in TikZ nodes Commented Nov 21, 2013 at 21:22
  • 1
    The question that has been marked as duplicate is three years older than this one. In good spirit of this site, I suggest to mark the newer one a duplicate of this one, which gives a simple answer to a simple problem.
    – Ingo
    Commented Jun 15, 2015 at 19:06
  • I'm not so sure about the re-open vote, so I will refrain from voting. But note that the question this question is currently linked to as a duplicate has a very thorough answer that very well includes the answer given here. It might not be as short or concise as the one from here, but it gives different approaches and some more explanation.
    – moewe
    Commented Jun 15, 2015 at 20:30

5 Answers 5

225

There is a much simpler and more elegant solution! From the TikZ manual § 17.4.3 Text Parameters: Alignment and Width for Multi-Line Text:

\node[draw, align=left] {This is a\\demonstration.};

Key here is that you must use the align option, with the parameter that you want.

3
  • 13
    So many "random" details like this is the main drawback of latex. The language should be intelligent/flexible enough not to need an instruction of how to align extra lines.
    – luchonacho
    Commented Nov 4, 2020 at 17:56
  • 3
    @luchonacho - completely agree! There was a hack with text width to force line breaking, but it's so inflexible (and it was the first advice I've got). Why couldn't it work by default? And I read it's still a "side effect" of some change, that it even works at all. Commented May 3, 2021 at 9:37
  • \node needs a default value for align option, so this feature can work without need to google this post.
    – luchonacho
    Commented Apr 1 at 19:50
31

You could use a single-column tabular to achieve line breaks:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz \node {\begin{tabular}{l}
    start \\
    another \\ 
    stop
\end{tabular}};
\end{document}

Also macros like \shortstack{start\\another\\stop} are useful. I also always add a \strut in every line to ensure a constant line skip.

1
  • 6
    +1 for \shortstack{start\\another\\stop}. Works fine.
    – dcb
    Commented Feb 12, 2014 at 3:38
7

You can get several lines in a node by using rectangle split (of the TikZ library shapes) which draws a split rectangle. Set rectangle split parts to the number of lines you want and use \nodepart to switch to the next line as in the following example:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes}

\begin{document}

\begin{tikzpicture}
\draw ellipse (2cm and 1cm) node [rectangle split,rectangle split parts=2]{%
  First line
  \nodepart{second}
  second line%
  };
\end{tikzpicture}

\end{document}

The output of the code

7

You can also put a \parbox around your node text:

\documentclass{article}
\usepackage{calc,tikz}
\begin{document}
\tikz \node {\parbox{\widthof{another}}{
    start \\
    another \\ 
    stop}};
\end{document}
0
0
\def\mynode#{\vtop \bgroup \hsize 0pt \parindent 0pt 
        \rightskip = 0pt minus \maxdimen \let\next=}
\tikz \node {\mynode{start\linebreak stop}};
1
  • Worked only partially for me. By using \mynode I get a node with the correct height but the node width seems to be 0. Commented May 19, 2011 at 9:34

Not the answer you're looking for? Browse other questions tagged .