I would like to set text in a TikZ node of maximum width and minimum height, i.e. the node cannot grow horizontally (but shrink for short text) but it can grow vertically (if the text exceeds the minimum height).
Using the varwidth
package this looks not too difficult, but the problem is, that I would like to top-align the enclosed text. The alignment should work for single lines of text as well as for more than one text line.
Tik/ doesn't seem to know any text top-align function but instead suggests the use of the text depth=
key. Unfortunately, that doesn't seem to work in a multi-line vs. single-line comparison.
You can use the following code for an example:
\documentclass{scrreprt}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{varwidth}
\begin{document}
\begin{tikzpicture}
\node (left) [minimum height=2cm,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Short text%
\end{varwidth}%
};
\node at (left.north east) [anchor=north west,minimum height=2cm,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Somehow a longer text for testing%
\end{varwidth}%
};
\end{tikzpicture}
\vspace{1cm}
\begin{tikzpicture}
\node (left) [text depth=2cm,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Short text%
\end{varwidth}%
};
\node at (left.north east) [anchor=north west,text depth=2cm,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Somehow a longer text for testing%
\end{varwidth}%
};
\end{tikzpicture}
\vspace{1cm}
\begin{tikzpicture}
\node (left) [text depth=2cm,text height=0,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Short text%
\end{varwidth}%
};
\node at (left.north east) [anchor=north west,text depth=2cm,text height=0,inner sep=0,shape=rectangle,draw=black] {%
\begin{varwidth}{3cm}%
Somehow a longer text for testing%
\end{varwidth}%
};
\end{tikzpicture}
\end{document}
The output looks like this:
The first two boxes show how
varwidth
does its job by growing the second box up to 3cm width and then breaking the line.The second two boxes are my attempt to solve it by setting
text depth=2cm
but as you can see, this grows one box vs. the other.The third illustration is an experiment to find out why
text depth
is not working as I would expect. Settingtext height=0
centers the text vertically on the textbox center and the boxes are of correct height. But now the text overflows on the top.
Desired outcome would be a box of height of 2cm with the text aligned on top. Maybe I'm missing something obvious?
Edit: I found a crude way to bottom-align
Reading In TikZ, is it possible to nest nodes? I got the idea of nesting tikzpicture
environments. This led to a crude way of bottom-aligning, but no top aligning.
The code looks like this (works with my example document from above):
\begin{tikzpicture}
\node (left) [minimum height=2cm,inner sep=0,text height=2cm,text depth=0,shape=rectangle,draw=black] {%
\begin{tikzpicture}
\node [minimum height=0,inner sep=0,text height=,text depth=,shape=rectangle,draw=red] {%
\begin{varwidth}{3cm}%
Short text%
\end{varwidth}%
};
\end{tikzpicture}%
};
\node at (left.north east) [minimum height=2cm,inner sep=0,text height=2cm,text depth=0,anchor=north west,shape=rectangle,draw=black] {%
\begin{tikzpicture}
\node [minimum height=0,inner sep=0,text height=,text depth=,shape=rectangle,draw=red] {%
\begin{varwidth}{3cm}%
Somehow a longer text for testing%
\end{varwidth}%
};
\end{tikzpicture}%
};
\end{tikzpicture}
It is crude because it doesn't take the baseline into account (could maybe be fixed with \strut
) and because it nests the environment, which actually requires to reset all parameters on the inside as they are inherited from the outside.