Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Oct 10 '11 at 10:16

This question came from our site for professional and enthusiast programmers.

2  
Duplicate stackoverflow.com/questions/3253769/… –  Alexey Malistov Jul 20 '10 at 12:23
5  
Please vote to close duplicates, do not downvote. –  Svante Jul 20 '10 at 13:38
1  
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 Jul 20 '10 at 14:30
1  
This question belongs on tex.sx! –  Ingo Oct 9 '11 at 13:03
    

5 Answers 5

up vote 71 down vote accepted

There is a much simpler and more elegant solution! From the TikZ manual v. 2.1, page 180:

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

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

share|improve this answer

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.

share|improve this answer
    
+1 for \shortstack{start\\another\\stop}. Works fine. –  my_greets Feb 12 at 3:38

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

share|improve this answer

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{tabular}};
\end{document}
share|improve this answer
\def\mynode#{\vtop \bgroup \hsize 0pt \parindent 0pt 
        \rightskip = 0pt minus \maxdimen \let\next=}
\tikz \node {\mynode{start\linebreak stop}};
share|improve this answer
    
Worked only partially for me. By using \mynode I get a node with the correct height but the node width seems to be 0. –  Stefan Endrullis May 19 '11 at 9:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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