14

How to position text on top of a node?

\documentclass{report}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\tikzset{every node/.style={rounded corners,minimum height=5cm}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {
    \begin{minipage}{5cm}
        alignment of nodes

        with \verb|minipage|

        long text

        fourth line
    \end{minipage}
};
\draw[<-,>=latex,red,thick] (-2.5,2.4) to[bend left] (-2.5,3) node[right,blue] {i need text here.};
\end{tikzpicture}
\end{document}

enter image description here

Torbjørn T.
  • 206,688
Regis Santos
  • 14,463
  • If you use \node [text width=<dimension>] then TikZ adds the minipage for you. – Martin Scharrer Jul 07 '11 at 08:47
  • 2
    I just had a look at the source code (pgfmoduleshapes.code.tex) where the minimum height handling is implemented for rectangle nodes. The vertical centering of text boxes in the node is hard coded there, so there aren't any TikZ style to adjust this. Maybe this is worth a feature request!? – Martin Scharrer Jul 07 '11 at 09:00
  • @MartinScharrer Did you ever add a feature request about this? I agree, it would be a nice addition. – Torbjørn T. Dec 09 '17 at 12:03
  • @TorbjørnT.: No I actually didn't – Martin Scharrer Dec 10 '17 at 19:10

2 Answers2

16

The minipage environment has a couple of optional arguments:

  • [pos] takes one of t, b, c, which will position the minipage at the top, bottom or center of the surrounding text line, respectively
  • [height] specifies the height of the minipage
  • [inner pos] again takes one of t,b,c, but this time it specifies the position of the content inside the minipage

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.

node with minipage

\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
  • 232,450
7

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}

Result

Martin Scharrer
  • 262,582
  • That's a really clever approach! – Jake Jul 07 '11 at 13:04
  • @Jake: actually it took me a while to get there. First I was doing it more complicated until I figured that it can be that simple. Also I learned about the practical differences of \box and \unhbox (which removes the added depth again). – Martin Scharrer Jul 07 '11 at 13:14