Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
If you use \node [text width=<dimension>] then TikZ adds the minipage for you. –  Martin Scharrer Jul 7 '11 at 8:47
1  
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 7 '11 at 9:00

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
    
Thanks Jake.!!! –  Regis da Silva Jul 8 '11 at 2:00

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

share|improve this answer
    
That's a really clever approach! –  Jake Jul 7 '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 7 '11 at 13:14
    
Thanks Martin, your idea is good. –  Regis da Silva Jul 8 '11 at 2:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.