4

Assume a tikzpicture consisting of three nodes with node text.

\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, shapes, fit, positioning}

\begin{document}
\tikzset{
    block/.style = {rectangle, draw, rounded corners, text width=6.0cm,
       text depth=1.50cm, text height=0.4cm, line width=0.75pt},
    block_half/.style = {block, text width=2.75cm},
}
    \begin{tikzpicture}[node distance=1.5cm, auto]
        \node [block, anchor=north east] (row0_col1) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux
        };
        \node [block_half, right=1cm of row0_col1] (row0_col2) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux
        };
        \node [block_half, right=1cm of row0_col2] (row0_col3) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux
        };
    \end{tikzpicture}
\end{document}

How can I align the text in each node such that it is vertically centered and horizontally left?

  • Use minimum height instead of text depth and text height, see e.g. Vertical alignmend tikz node – Torbjørn T. May 17 '16 at 16:24
  • @TorbjørnT. But I would like to maintain a constant height and length of each node across multiple rows (not shown in the above example). – Michael Gruenstaeudl May 17 '16 at 16:27
  • OK, but I don't see the problem. Won't you get that with minimum height? – Torbjørn T. May 17 '16 at 16:30
  • @TorbjørnT. You are right. I can achieve the desired outcome via minimum height. – Michael Gruenstaeudl May 17 '16 at 16:47
1

If you don't specify text height and text depth, that is the default behaviour. To have them all the same height, set a minimum height.

enter image description here

\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc, shapes, fit, positioning}

\begin{document}
\tikzset{
    block/.style = {rectangle, draw, rounded corners, text width=6.0cm,
        line width=0.75pt,minimum height=2.5cm},
    block_half/.style = {block, text width=2.75cm},
}
    \begin{tikzpicture}[node distance=1.5cm, auto]
        \node [block, anchor=north east] (row0_col1) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux \\
            \textit{Baz:} Qux \\
            \textit{Baz:} Qux \\
        };
        \node [block_half, right=1cm of row0_col1] (row0_col2) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux
            \textit{Baz:} Qux \\
        };
        \node [block_half, right=1cm of row0_col2] (row0_col3) {
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux
        };
    \end{tikzpicture}
\end{document}
0

I used a \Centerstack on the text, and adjusted the text height and text depth.

\documentclass[border=5mm]{standalone}
\usepackage[latin1]{inputenc}
\usepackage{tikz,stackengine}
\setstackEOL{\\}
\usetikzlibrary{calc, shapes, fit, positioning}

\begin{document}
\tikzset{
    block/.style = {rectangle, draw, rounded corners, text width=6.0cm,
       text depth=.9cm, text height=1.00cm, line width=0.75pt},
    block_half/.style = {block, text width=2.75cm},
}
    \begin{tikzpicture}[node distance=1.5cm, auto]
        \node [block, anchor=north east] (row0_col1) {\Centerstack{%
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux\\
            \textit{Baz:} Qux}
        };
        \node [block_half, right=1cm of row0_col1] (row0_col2) {\Centerstack{%
            \textbf{Foo Bar}\\
            \textit{Baz:} Qux}
        };
        \node [block_half, right=1cm of row0_col2] (row0_col3) {\Centerstack{%
            \textbf{Foo Bar}}
        };
    \end{tikzpicture}
\end{document}

enter image description here

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.