8

In TikZ, I know that the relevant properties to control the shape and text-alignment of a node are:

  • minimum width and minimum height (outer shape)
  • AFAIU baseline is used to align the nodes themselves, not their contents?
  • align (section 17.4.3)
  • text width and text height (section 17.4.4), although the documentation mentions that:

I recommend using minimum size instead of text height except for special situations.

I also think that this post gives a very good example of how to use these properties with text.


But what if what if the contents I am trying to align are not text? My example below is using a minipage, but if that doesn't change the answer completely, I would assume this could be a tabular, or includegraphics, or any other non-text contents.

As you can see the alignment of the contents is vertically centred. Given that I have specified minimum height/width, how can I specify an alignment to the top of the node, or to the bottom?

\documentclass[a4paper]{article}
\usepackage{calc}
\usepackage{tikz}

\newlength{\boxH} \setlength{\boxH}{2cm}
\newlength{\boxW} \setlength{\boxW}{5cm}
\newlength{\boxM} \setlength{\boxM}{2mm}

\begin{document}
\begin{tikzpicture}
    \node[
        shape=rectangle, 
        fill=red, 
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west
    ] {%
        \begin{minipage}{\boxW-2\boxM}%
        One line
        \end{minipage}%
    };
    \node[
        shape=rectangle, 
        fill=blue, 
        xshift=\boxW+\boxM,
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west
    ] {%
        \begin{minipage}{\boxW-2\boxM}%
        Two\\ lines
        \end{minipage}%
    };
\end{tikzpicture}
\end{document}

Output of example drawing

Bernard
  • 271,350
Jonathan H
  • 1,000

3 Answers3

4

Since you are already using minipages, you could use their alignment mechanisms. For example, it you want the text in the red box to be top aligned, use

\documentclass[a4paper]{article}
\usepackage{calc}
\usepackage{tikz}

\newlength{\boxH} \setlength{\boxH}{2cm}
\newlength{\boxW} \setlength{\boxW}{5cm}
\newlength{\boxM} \setlength{\boxM}{2mm}

\begin{document}
\begin{tikzpicture}
    \node[
        shape=rectangle, 
        fill=red, 
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west
    ] {%
        \begin{minipage}[t][\the\dimexpr\boxH-1.6em]{\boxW-2\boxM}%
        One line
        \end{minipage}%
    };
    \node[
        shape=rectangle, 
        fill=blue, 
        xshift=\boxW+\boxM,
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west
    ] {%
        \begin{minipage}{\boxW-2\boxM}%
        Two\\ lines
        \end{minipage}%
    };
\end{tikzpicture}
\end{document}

enter image description here

4

Since node's size is fixed and you know it's large enough to contain the text, you can use a label to place the text inside node's border selecting the convenient anchor:

\documentclass[a4paper]{article}
\usepackage{calc}
\usepackage{tikz}

\newlength{\boxH} \setlength{\boxH}{2cm}
\newlength{\boxW} \setlength{\boxW}{5cm}
\newlength{\boxM} \setlength{\boxM}{2mm}

\begin{document}
\begin{tikzpicture}
    \node[
        shape=rectangle, 
        fill=red, 
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west,
        label={[anchor=north west]north west:One line}
    ] {};
    \node[
        shape=rectangle, 
        fill=blue, 
        xshift=\boxW+\boxM,
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west,
        label={[anchor=south, align=left]south:{Two\\ longer lines}}
    ] {};
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
0

This answer gives an alternate way of doing it.

enter image description here

\documentclass[a4paper]{article}
\usepackage{calc}
\usepackage{tikz}

\newlength{\boxH} \setlength{\boxH}{2cm}
\newlength{\boxW} \setlength{\boxW}{5cm}
\newlength{\boxM} \setlength{\boxM}{2mm}

\begin{document}
\begin{tikzpicture}
    \node[
        shape=rectangle, 
        fill=red, 
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west,
        append after command={node[anchor=north west] at  (\tikzlastnode.north west) 
        {\begin{minipage}{\boxW-2\boxM}%
          One line
        \end{minipage}}},
    ] {};
    \node[
        shape=rectangle, 
        fill=blue, 
        xshift=\boxW+\boxM,
        inner sep=\boxM, 
        minimum width=\boxW, 
        minimum height=\boxH, 
        anchor=north west,
        append after command={
        node[anchor=south west] at  (\tikzlastnode.south west) 
        {\begin{minipage}{\boxW-2\boxM}%
          Two\\ lines
        \end{minipage}}},
    ] {};
\end{tikzpicture}
\end{document}
nidhin
  • 7,932