How to position text on top of a node?
The minipage
environment has a couple of optional arguments:
[pos]
takes one oft
,b
,c
, which will position theminipage
at the top, bottom or center of the surrounding text line, respectively[height]
specifies the height of theminipage
[inner pos]
again takes one oft
,b
,c
, but this time it specifies the position of the content inside theminipage
In your case, you could use
\begin{minipage}[t][5cm]{5cm} ... \end{minipage}
to position the text. Your node will then have a width and height of 5cm + 2*inner sep
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{every node/.style={rounded corners}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {
\begin{minipage}[t][5cm]{5cm}
alignment of nodes
with \verb|minipage|
long text
fourth line
\end{minipage}
};
\end{tikzpicture}
\end{document}
Jake's answer is good, but if you really want to obey the minimum height
, i.e. allow the nodes also to be larger you would need to measure the height (actually the depth) by yourself and increase it if required. You can code an own environment for this which takes the width and totalheight as two arguments.
The height argument can also be replaced by reading out the minimum height
argument using \pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
.
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\newenvironment{minsizebox}[2]{%
\pgfmathsetlength\@tempdima{#2}%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
\advance\@tempdima by -2\pgf@yc
\begin{lrbox}{\@tempboxa}%
\begin{minipage}[t]{#1}%
\vspace{0pt}%
}{%
\end{minipage}%
\end{lrbox}%
\ifdim\@tempdima>\dp\@tempboxa
\dp\@tempboxa=\@tempdima
\fi
\box\@tempboxa
}
\makeatother
\begin{document}
\tikzset{every node/.style={rounded corners,minimum height=5cm}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
fourth line
\end{minsizebox}%
};
\end{tikzpicture}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
long text
long text
long text
long text
long text
long text
\end{minsizebox}%
};
\end{tikzpicture}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
\end{minsizebox}%
};
\end{tikzpicture}
\end{document}