9

I was trying to draw a rectangle like this (there would be only one letter per row in the rectangle). But none of \\, \linebreak, \newline seems to work. How can I do this?

Well, I tried this:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[node distance=1cm, auto]  
\tikzset{
    man/.style={rectangle}
}  
\node[man] (man) {A \\ B \linebreak C \newline D};
\end{tikzpicture} 

\end{document}
hola
  • 4,026
  • 3
  • 35
  • 72

7 Answers7

13
\documentclass[preview,border=12pt]{standalone}
\usepackage{xcolor}

\begin{document}
\colorbox{blue!50!green}{\parbox{12pt}{\centering A\\B\\C}}
\end{document}

enter image description here

or

\documentclass[preview,border=3pt]{standalone}
\usepackage{xcolor}
\definecolor{mycolor}{RGB}{91,155,213}
\begin{document}
\colorbox{mycolor}{\parbox{12pt}{\color{white}\centering A\\B\\C}}
\end{document}

enter image description here

9

TikZ version.

The simplest manner is to use a node, fixing its text width and center align the content; alternatively, you can break the content, provided that you are setting some alignment. The third option uses the dedicated rectangle split shape.

\documentclass[tikz]{standalone}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}
\node[text=white,fill=cyan!60!blue,text width=1em,align=center]{A B C};
\end{tikzpicture}

\begin{tikzpicture}
\node[text=white,fill=cyan!60!blue,align=center]{A\\ B\\ C};
\end{tikzpicture}

\begin{tikzpicture}
\node[text=white,fill=cyan!60!blue,
 rectangle split,rectangle split parts=3]{A\nodepart{two}B\nodepart{three}C};
\end{tikzpicture}

\end{document}

The result:

enter image description here

6

I think the same as the other tikz solutions but a few minutes later:

\documentclass[tikz,border=0.125cm]{standalone}
\begin{document}
\tikz\node[fill=blue!70!green!60, text=white, inner xsep=1.5ex, align=center, font=\sf]
  {A\\B\\C};
\end{document}

enter image description here

Mark Wibrow
  • 70,437
4

Firstly, in your question it would have been interesting to say that there were other parts with Tikz. Without other parts Code Mocker is right with the use of \parbox but you need to fix the width.

Perhaps it's possible to consider your question like a duplicate one : manual-automatic-line-breaks and you can also read the pgfmanual 16.4.3 Text Parameters: Alignment and Width for Multi-Line Text

I prefer to give another answer with some explanations.

If you put a simple text in a node, this text is written inside a \hbox. Inside this box, \\ is ineffective. If you want to be able to use linebreak you need to use complex objects like boxes, \parbox, minipage etc...

1) First idea like in the pgfmanual : tabular

\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\begin{document}

\tikz \node[draw,fill=blue!20] {
\begin{tabular}{@{}c@{}}
A\\
B
\end{tabular}
};
\end{document}

enter image description here

2) a box with \parbox but it's not perfect. You need to know the width of the rectangle

\documentclass{article}
\usepackage{colortbl}
\usepackage{tikz}
\begin{document}

\tikz \node[draw,fill=blue!20] {
  \parbox{12pt}{\centering A\\B\\C}
};
\end{document}

It's also possible to use a minipage or a \vbox

3) text width is a node option but you need to fix the width like Claudio's code

4) the better one is the use of align=center like Claudio's code

\documentclass{article}
\usepackage{tikz}
\begin{document}

\tikz \node[draw,fill=blue!20,align=center] {A\\B\\C};
\end{document}
Alain Matthes
  • 95,075
3

Here is how. use align=center.

enter image description here

\documentclass[11pt]{article}
\usepackage[margin=1cm]{geometry}
\usepackage[demo]{graphicx}
\usepackage{tikz,xcolor}
\usetikzlibrary{positioning,calc,arrows}

\begin{document}

\tikzset{
mynode/.style={minimum height=2cm, minimum width=1cm, fill=blue, text=white,align=center}
}


\begin{tikzpicture}
\node [mynode] at (0,0) (a){A\\B\\C};
\end{tikzpicture}

\end{document}
Jesse
  • 29,686
2

Using TikZ node (though this looks overkill in this case):

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[fill=blue!40!cyan, text=white, align=center, font=\sffamily]{A\\B\\C};
\end{document}

output
You have to specify align option to use \\ in TikZ node.

If you want to increase the spaces between the lines, add an optional argument to \\:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\tikz\node[fill=blue!40!cyan, text=white, align=center, font=\sffamily]{A\\[1ex]B\\[1ex]C};
\end{document}
a user
  • 1,502
1

Another TikZ solution. This one uses a matrix node. Each letter will be on a different cell/node which can be individually referenced.

\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{matrix, fit}
\begin{document}
\begin{tikzpicture}[mynode/.style={text=white, 
               fill=cyan!60!blue, 
               anchor=center, 
               inner sep=3pt}]

\matrix (A) [matrix of nodes, inner sep=0pt, 
             nodes={mynode}, column sep=0pt, row sep=0pt] 
             {A\\B\\C\\};

\draw (A-1-1) to [out=30, in=-30] (A-3-1);
\node[fit={(A-2-1)}, circle, draw, red, inner sep=-1pt] (r) {};
\draw[<-, red] (r) -- ++(15:7.5mm);
\end{tikzpicture}

\end{document}

enter image description here

Ignasi
  • 136,588