# Correctly align vertical text on a baseline in pgfplots

I'm proud to announce that I have a large number of PGFplots in my document.

But ... I have a small problem with the vertical alignment of text throughout my plots.

What's the easiest way to align text on a baseline in PGFPlots? I'm particularly interested in alignment of text in multi-column legends, but also on axes.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
y post scale=0.2,
legend style={at={(0.5,1.02)},anchor=south},
legend columns=3,
xmin=0,
ytick={one,two,eight},
symbolic y coords={one,two,eight},
bar width=7pt,
enlarge y limits=0.3
]

\end{axis}
\end{tikzpicture}
\end{document}


Here you see that in the legend, text is centred "mid" according to the height of the text block (see one floating higher than the baseline of two due to being shorter, and ditto two to eight), not according to a natural text baseline. This is also true of the y-axis (and would be true of the x-axis also).

I could fix this by appending phantom text like tg to equalise the text heights of my various labels, but this would introduce redundant horizontal space and I have a large number of plots and hope that particular hack is not necessary.

I would love a solution I could stick in the \pgfplotsset{ ... } in the head of my document that would apply across all plots, for legends and x & y axes, but am open to suggestions.

Anyone know how this text alignment could be best achieved universally?

-
FWIW, my back-up solution is: legend style={font=\scshape}. ... any excuse to use small caps. ^.^ –  badroit Nov 25 '12 at 4:01
For the legend adding \strut or (preferable) a custom \mystrut (e.g. \def\mystrut{\vphantom{hg}}) works (all text boxes have the same depth and height now). This probably would work for the y axis, too. Maybe there's a font= key for these things? –  Qrrbrbirlbel Nov 25 '12 at 4:19

For the legend adding \strut or (preferable) a custom \mystrut (e.g. \def\mystrut{\vphantom{hg}}) works (all text boxes have the same depth and height now). This probably would work for the y axis, too. Maybe there's a font= key for these things?

Oh, yes there is: legend style and y tick label style.

I used \vphantom{hg} here because h and g are the highest and deepest letters in your example. For a more general solution (actual text, different fonts) I'd use the very high \strut or a longer \vphantom argument.

## Code

\documentclass{standalone}
\usepackage{pgfplots}
\def\mystrut{\vphantom{hg}}
%\def\mystrut{\strut}
\pgfplotsset{
legend style={font=\mystrut},
y tick label style={font=\mystrut}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
y post scale=0.2,
legend style={at={(0.5,1.02)},anchor=south},
legend columns=3,
xmin=0,
ytick={one,two,eight},
symbolic y coords={one,two,eight},
bar width=7pt,
enlarge y limits=0.3
]


@badroit \phantom adds vertical and horizontal, \hphantom only horizontal, \vphantom only vertical space. –  Qrrbrbirlbel Nov 25 '12 at 21:41