2

Tikz allow to make multiline nodes using align=alignement where alignement can be left, center or right and \\ (see this previous question)

I am wondering how one can have a multi line node with a different alignement depending on the line ?

Example:

   Node title

this the body of  the
node spanning several
lines.
Manuel Selva
  • 1,839
  • Can you give an example of such text? Not much hope for word breaking though. – percusse Oct 02 '14 at 09:39
  • I just added a simple text example. i don't need word breaking in this case – Manuel Selva Oct 02 '14 at 09:42
  • See for example `\node[text width=3.5cm,draw,align=justify]{ \centering Node title

    this the body of the node spanning several lines. };You need an empty line afterNode titleotherwise\centering` won't work.

    – percusse Oct 02 '14 at 09:48
  • Can you show a working example, I am not able to compile my document when using \centering. – Manuel Selva Oct 02 '14 at 09:56
  • Complete example: \documentclass{article}\usepackage{tikz,lipsum}\begin{document}\begin{tikzpicture}\node [draw,text width=5cm,align=justify] {{\centering Stuff \par}\lipsum[1]};\end{tikzpicture}\end{document} This isn't really different from normal text, a node with text width set is something like a minipage. – Torbjørn T. Oct 02 '14 at 10:07
  • Thanks ! I was just missing the text width parameter thinking it was not important ! – Manuel Selva Oct 02 '14 at 10:40
  • 1
    Heck, just stick a \parbox inside the node. – John Kormylo Oct 02 '14 at 16:08
  • @TorbjørnT. that looks like an answer to me. Any chance you want to convert it into one? – Adam Liter Oct 04 '14 at 05:14

1 Answers1

2

When adding the text width parameter to a node, this is similar to the node contents being within a minipage. As such, with text width set you can use any normal way of centering text, such as \centering or the center environment. If you want the remaining text to be justfied, add align=justify to the node options as well, the default is to set the node text ragged right.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\begin{tikzpicture}
\node [draw,text width=5cm]
{{\centering Stuff \par}
\lipsum[1]};

\node at (6,0) [draw,text width=5cm,align=justify]
{\begin{center}
Stuff
\end{center}
\lipsum[1]};
\end{tikzpicture}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
  • Sorry to put this as an answer but I'm new and can't comment the previous answer. If, like me, you only have two lines and the first is not a title, you can add a negative \vspace inside your center environment to centre the whole text within the box. – gabri Oct 10 '18 at 00:45