20

The MWE:

\documentclass{standalone}

%======================================
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{arrows}
\usetikzlibrary{graphs}
\usegdlibrary{force, layered, trees}
%======================================

\begin{document}

\tikz 
\graph [spring layout, nodes = draw]
{
    a/"dispersive\newline collision";
    b/"non-dispersive\newline  collision";

    a -> b
};

\end{document}

The output:

enter image description here

The question:

As you can see above, the \newline command does nothing. Trying to use \\ instead of \newline, like how the pgfmanual does in some examples (see pg. 386) has the same effect. Why? What can I do in order to have the words appear on separate "lines"?

  • By the way, this has nothing to do with the graphdrawing library either. It happens as soon as you put text in nodes. – cfr May 20 '14 at 23:15
  • @cfr I'll change the question again to reflect that. – user89 May 20 '14 at 23:22
25

By default nodes are set in a box that doesn't allow line breaks. Adding an alignment (e.g. align=center) or text width to the node sets it in a paragraph box instead, where line breaks can occur. For some reason \newline still doesn't work (no idea why), but \\ does.

\documentclass{standalone}

%======================================
\usepackage{tikz}
\usetikzlibrary{graphdrawing}
\usetikzlibrary{arrows}
\usetikzlibrary{graphs}
\usegdlibrary{force, layered, trees}
%======================================

\begin{document}

%simpler example
\begin{tikzpicture}
\node {a\\b};
\node [align=center] at (1,0) {a\\b};
\end{tikzpicture}

% your code, modified
\tikz 
\graph [spring layout, nodes = {draw,align=center}]
{
    a/"dispersive\\ collision";
    b/"non-dispersive\\  collision";

    a -> b
};

\end{document}

enter image description here

  • In my case `\` makes new line but also puts a line in-between. @Torbjørn T. – alper Feb 21 '18 at 10:15
  • @Alper What do you mean by "line in-between"? – Torbjørn T. Feb 21 '18 at 10:19
  • My code: gist.github.com/avatar-lavventura/… . Instead of putting new-line, text inside the node, it starts drawing new node on the next line. @Torbjørn T. – alper Feb 21 '18 at 10:33
  • 1
    @Alper Of course, think about the context. In a matrix a \\ means "start a new row". See tex.stackexchange.com/questions/112724, you need to wrap the cell content in { }, and also specify the text width , and finally, it doesn't seem to work with math nodes, try matrix of nodes. – Torbjørn T. Feb 21 '18 at 10:56
  • You are right, I confused the terms. \shortstack{value1\\ value} solved my problem. – alper Feb 21 '18 at 11:05

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.