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.

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}
share|improve this question
2  
Please provide a complete minimal working example showing your current code. –  Marco Daniel Feb 2 at 9:17

7 Answers 7

up vote 4 down vote accepted

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}
share|improve this answer
    
Using tabular alone without TikZ should be enough, unless we want to slow down the compilation. –  Please don't touch Feb 3 at 0:50
2  
Yes you are right but the OP needs to use Tikz with other parts. –  Alain Matthes Feb 3 at 8:02
    
@AlainMatthes: it seems I've lost a green tick, but welcome back Alain! –  Claudio Fiandrino Feb 3 at 18:27
\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

share|improve this answer
2  
If compilation speed is the most important aspect, avoid using TikZ. :-) –  Please don't touch Feb 2 at 9:48
    
But I already have many other parts in TikZ. –  pushpen.paul Feb 2 at 14:13
    
No this is not a fine solution because you need to fix the width. A tabular is better –  Alain Matthes Feb 2 at 22:31

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

share|improve this answer
    
you don't need the double backslash in the first example. Whitespace is enough since the width is quite narrow. –  percusse Feb 2 at 10:27
    
@percusse: sure, I incidentally merged two options into one. –  Claudio Fiandrino Feb 2 at 10:53

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

share|improve this answer

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}
share|improve this answer

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}
share|improve this answer

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

share|improve this answer

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.