10

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 
]

\addplot+[xbar] coordinates {(1,one)};
\addlegendentry{one}

\addplot+[xbar] coordinates {(2,two)};
\addlegendentry{two}

\addplot+[xbar] coordinates {(8,eight)};
\addlegendentry{eight}

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

enter image description here

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?

2
  • FWIW, my back-up solution is: legend style={font=\scshape}. ... any excuse to use small caps. ^.^
    – badroit
    Nov 25, 2012 at 4:01
  • 1
    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? Nov 25, 2012 at 4:19

1 Answer 1

9

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 
]

\addplot+[xbar] coordinates {(1,one)};
\addlegendentry{one}

\addplot+[xbar] coordinates {(2,two)};
\addlegendentry{two}

\addplot+[xbar] coordinates {(8,eight)};
\addlegendentry{eight}

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

Output

enter image description here

2
  • Thanks! I thought that phantom text would add redundant horizontal white-space, but it appears not so this works well!
    – badroit
    Nov 25, 2012 at 16:51
  • 1
    @badroit \phantom adds vertical and horizontal, \hphantom only horizontal, \vphantom only vertical space. Nov 25, 2012 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .