14

The output from the following script

\documentclass{article}
\usepackage{tikz}
\usepackage{graphicx}
\begin{document}
\begin{tikzpicture}
  \node(title)[text width = 0.25\textwidth, text height = 3 cm, draw]{title};
  \end{tikzpicture}
\end{document}

is

enter image description here

How can I vertically align the text in the node?

Specifying the text depth as in

\node(title)[text width = 0.25\textwidth, text height = 1cm, text depth = 1 cm, draw]{
    title line 1\\
    title line 2\\
    title line 3};

Does not result in the text being vertically centred.

enter image description here

3
  • 1
    text height specifies the height of the text, so adds space above the text. You could use text depth to specify a certain depth, which adds space below your text. But I guess you don't want that, either. You're most likely looking for minimum size instead of text height.
    – Skillmon
    Commented May 4, 2018 at 9:01
  • @Skillmon, could I centre text following your suggestion of using text depth? See my updated question. – Viesturs
    – Viesturs
    Commented May 4, 2018 at 9:13
  • 2
    No, use minimum size or minimum height, the latter if it's just about vertical dimensions (you already specified horizontal dimensions with text width).
    – Skillmon
    Commented May 4, 2018 at 9:17

1 Answer 1

21

text height and text depth specify the vertical dimensions of the text you give explicitly. As a result TikZ thinks the node's text has exactly that height and draws its box like that.

You can use minimum height to set a minimum vertical size of the node, and minimum width for the width (while text width inserts line wrapping at the specified width minimum width does not). The result will be a box in which your text is centred, but the box must have at least those dimensions. Note that minimum height is both height and depth around your text (evenly). If you want line wrapping text which is horizontally centred you can use text width and text centered.

Compare the following:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[node distance=2cm]
  \node(1)[text centered,text width = 0.25\textwidth, minimum height = 3 cm, draw]
    {title is very long and long};
  \node(2)[minimum width = 0.25\textwidth, minimum height = 3 cm,
    draw,below of=1,anchor=north]{title is very long and long};
  \node(3)[text centered,text width = 0.25\textwidth, text height = 3 cm,
    draw,below of=2,anchor=north]{title is very long and long};
  \node(4)[text centered,text width = 0.25\textwidth, text depth = 3 cm,
    draw,below of=3,anchor=north]{title is very long and long};
  \node(5)[text centered,text width = 0.25\textwidth, text depth = 1.5 cm, text
    height = 1.5cm, draw,below of=4,anchor=north]{title is very long and long};
\end{tikzpicture}
\end{document}

enter image description here

5
  • Is it possible to fix the position of the baseline independently of the content of the node? Indeed, if I fix text height=.3em,text depth=0em then the baseline is always at the same position but if the text is particularly heigh then it will go out of the node. MWE: \tikzset{my/.style={font={\fontsize{8}{10}\selectfont\boldmath},minimum size=5mm, inner sep=0pt, anchor=center,circle,fill=red,text height=.3em,text depth=0em}} \tikz \node[my]{$a$}; \tikz \node[my]{$b$}; \tikz \node[my]{$b\pi'$};
    – tobiasBora
    Commented Oct 27, 2021 at 16:48
  • @tobiasBora first: That might be better off as a new question. However, take a look at the baseline key. E.g., use \tikz[baseline=<something>]\node[my]{$b$}; (with <something> some length TikZ understands, might reference the current bounding box as well)
    – Skillmon
    Commented Oct 27, 2021 at 17:27
  • @Skillmon, I'd like as node 4 but with constant total height. For example, 4cm with text pushed to top. The text could have 1, 2 or even 3 lines but the text should go to top always. Is it possible?
    – Sigur
    Commented Nov 8, 2022 at 16:41
  • @Sigur I'm not sure whether there are built-in options to achieve that, but note that nothing hinders you to use \parbox inside of a node: \node[draw]{\parbox[t][4cm][t]{5cm}{Your node text goes here}; creates a 4cm high and 5cm wide node (plus inner sep) with the contents at the top.
    – Skillmon
    Commented Nov 8, 2022 at 17:32
  • @Skillmon, thanks. I tried it and also another possibility is to subtract a little bit: text depth=4cm - 14pt worked in my case. Thanks.
    – Sigur
    Commented Nov 8, 2022 at 18:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .