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'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?

share|improve this question
    
FWIW, my back-up solution is: legend style={font=\scshape}. ... any excuse to use small caps. ^.^ –  badroit Nov 25 '12 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? –  Qrrbrbirlbel Nov 25 '12 at 4:19
add comment

1 Answer

up vote 6 down vote accepted

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

share|improve this answer
    
Thanks! I thought that phantom text would add redundant horizontal white-space, but it appears not so this works well! –  badroit Nov 25 '12 at 16:51
1  
@badroit \phantom adds vertical and horizontal, \hphantom only horizontal, \vphantom only vertical space. –  Qrrbrbirlbel Nov 25 '12 at 21:41
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.