6

I use lines and text to clarify a TikZ figure. However if I use the same y-coordinate (in tthe example below 3) and offset (in this case 'above'), the text looks messy.

How can I get the alignment right of different nodes with the same (y) coordinates?

This:

\documentclass{standalone}
\usepackage{tikz}    
\begin{document} 
\begin{tikzpicture} 
\draw[color=red](0,3)--(11,3); 
\draw (0.5,2.5)--(0.5,3) node[above] {For Example:};
\draw (3,2.5)--(2.5,3) node[above] {this looks};
\draw (5,2.5)--(5.5,3) node[above] {not nice vertically aligned};
\end{tikzpicture}
\end{document} 

Results in Miktex/TexLive PDFLatex into:

This looks not nice

  • 3
    Welcome! This happens because some nodes have descenders in their texts while others do not. Use a \strut in your node texts or add suitable values of text height and text depth to the node options (best done with a style). – Paul Gessler Mar 25 '15 at 19:01
5

You can also anchor nodes on its baseline:

\documentclass{standalone}
\usepackage{tikz}    
\begin{document} 
\begin{tikzpicture}[every node/.style={above=2mm, anchor=base}]
\draw[color=red](0,3)--(11,3); 
\draw (0.5,2.5)--(0.5,3) node {For Example:};
\draw (3,2.5)--(2.5,3) node {this looks};
\draw (5,2.5)--(5.5,3) node {not nice vertically aligned};
\end{tikzpicture}
\end{document} 

enter image description here

3

Thanks Paul, this did the trick:

\draw (0.5,2.5)--(0.5,3) node[above] {\strut For Example:};
\draw (3,2.5)--(2.5,3) node[above] {\strut this looks};
\draw (5,2.5)--(5.5,3) node[above] {\strut not nice vertically aligned};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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