[Tex/LaTex] Vertical space inside a TikZ node

nodestikz-pgf

I am trying to insert some vertical space inside a node (So it will be filled later).

\node [draw,rounded corners,minimum height=5cm] (node1) {\underline{Case 1}};

This is giving this:

enter image description here

However, I want Case 1 to be at the top of the node.

Can any one help please.
Thanks in advance.

Best Answer

You can achieve the effect you want by using an anchor and a second node:

\documentclass{article}
\usepackage{tikz}
%%\usetikzlibrary{calc}%% not necessary, but potentially useful
\pagestyle{empty}
\begin{document}

\begin{tikzpicture}
  \node [draw,rounded corners,minimum height=5cm,minimum width=2cm] (node1) {};
  \node [anchor=north west] at (node1.north west) {\underline{Case 1}};
\end{tikzpicture}

\end{document}

enter image description here

What's happening here is that node1.north west tells \node where you want the content to be placed. The anchor=north west tell the \node to use the upper left hand corner of the text as the anchor position to be placed.

It's a bit easier to see what's happening if we do something completely different from what you actually want (but perhaps this will make clear what the two north west directives are about):

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \node [draw,rounded corners,minimum height=5cm,minimum width=4cm] (node1) {};
  \node [anchor=north west] at (node1.north west) {\fbox{Case: anchor is NW}};
  \node [anchor=south west] at (node1.north west) {\fbox{Case: anchor is SW}};
  \node [anchor=south east] at (node1.north west) {\fbox{Case: anchor is SE}};
  \node [anchor=north east] at (node1.north west) {\fbox{Case: anchor is NE}};
  \foreach \myanchor in {north west,south west,north east,south east}
    { 
      \node[fill,circle,inner sep=1pt,outer sep=1ex,anchor=\myanchor] at (node1.north west){};
    }
\end{tikzpicture}

\end{document}

enter image description here

Notice that the anchoring is about the corners to the text being place with respect to the node being used (which in this case is node1.north west.

Additionally using the TikZ library calc you can exert more control over the positioning:

  \node [anchor=north west] at ($(node1.north west)+(1ex,-2ex)$) {\underline{Case 1}};

The positioning here is a bit more extreme to help illustrate what's happening.

UPDATE

If you'll be doing this a lot, then (following @Qrrbrbirlbel 's suggestion), you can define a style and do everything within one node:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[my case/.style={draw,
                                    rounded corners,
                                    minimum height=5cm,
                                    minimum width=4cm,
                                    append after command={node[anchor=north west]
                                                          at  (\tikzlastnode.north west)
                                                          {#1}}}]
  \node [my case={\textbf{Case A:}}] (node1) {};
\end{tikzpicture}

\end{document}

And yes, you can define a command for this:

\documentclass{article}
\usepackage{tikz}
\def\mycasenode(#1)#2;{\node[my case={#2}] (#1) {};}
\begin{document}

\begin{tikzpicture}[my case/.style={draw,
                                    rounded corners,
                                    minimum height=5cm,
                                    minimum width=4cm,
                                    append after command={node[anchor=north west]
                                                          at  (\tikzlastnode.north west)
                                                          {#1}}}]
  %\node [my case={\textbf{Case A:}}] (node1) {};
  \mycasenode (node1) {\textbf{Case A:}};
\end{tikzpicture}

\end{document}

Notice how I created a definition to mimic TikZ command and node styles.

Related Question