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.

I have a problem in TikZ with the vertical alignment of text inside nodes.

With a very minimal example like this:

\usetikzlibrary{arrows,positioning,shapes.geometric}

\begin{tikzpicture}[%
  ->,
  thick, shorten >=1pt,
  >=latex,
  node distance=7mm,
  noname/.style={%
    rectangle, rounded corners=9pt,
    text width=11em,
    text centered,
    minimum height=3em,
    draw=black!50,fill=black!20
  }
]

\node[noname] (configload) {Loads config};
\node[noname] (varsdec) [right=of configload] {New variables};

\path (configload) edge node {} (varsdec);

\end{tikzpicture}

I get this diagram:

simple diagram generated

which has the text wrongly aligned because of the letter g in the first box. It can be seen better if we cut this diagram, showing only text:

only text

Which is the parameter I have to set in order to align the text correctly?

share|improve this question
add comment

3 Answers 3

up vote 10 down vote accepted

This situation is described in Section 5.1 Styling the Nodes of the pgfmanual; one possible solution is to explicitly declare height and depth for the nodes:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes.geometric}

\begin{document}

\begin{tikzpicture}[%
  ->,
  thick, shorten >=1pt,
  >=latex,
  node distance=7mm,
  noname/.style={%
    rectangle, rounded corners=9pt,
    text height=1.5ex,
    text depth=.25ex,
    text width=11em,
    text centered,
    minimum height=3em,
    draw=black!50,fill=black!20
  }
]

\node[noname] (configload) {Loads config};
\node[noname] (varsdec) [right=of configload] {New variables};

\path (configload) edge node {} (varsdec);

\end{tikzpicture}

\end{document}

Here I add an image showing that now the baseline of the text is aligned:

The red line showing the alignment was obtained by adding

\draw[help lines,red,<->] let \p1 = (configload.base), \p2 = (varsdec.base) in (-1.5,\y1) -- (6.5,\y1)
(-1.5,\y2) -- (6.5,\y2);

to the example code above.

share|improve this answer
    
Thanks Gonzalo! That solved perfectly the problem. And also thanks for pointing out the section in the pgfmanual where this issue is covered. –  Nicolás Jun 3 '11 at 0:01
add comment

And why not :

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc,positioning,shapes.geometric}

\begin{document}
\begin{tikzpicture}[%
  ->,
  thick, shorten >=1pt,
  >=latex,
  node distance=7mm,
  noname/.style={%
    rectangle, rounded corners=9pt,
    text width=11em,
    text centered,
    minimum height=3em,
    draw=black!50,fill=black!20
  }
]

\node[noname,anchor=mid] (configload) {Loads config};
\node[noname,anchor=mid] (varsdec) [right=of configload] {New variables};

\draw[help lines,red,<->] ($(configload.base)-(2,0)$)--($(varsdec.base)+(2,0)$);
\path (configload) edge node {} (varsdec);

\end{tikzpicture}    
\end{document} 

enter image description here

share|improve this answer
    
This is exactly what the anchors base and mid are meant for, isn't it? –  Christoph Jun 3 '11 at 9:00
add comment

add some phantom text with the same height as the other one. For example

\node[noname,anchor=mid] (configload) {Loads config};
\node[noname,anchor=mid] (varsdec) [right=of configload] {\vphantom{g} New variables};
share|improve this answer
add comment

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.