2

In this legend, I'd like the second words ("measured", "measured", "regression", "regression") to line up, such that all four start at the same horizontal position. How to accomplish that?

enter image description here

If there was a horizontal \smash command, I'd try to solve it by changing e.g. the first legend entry to \hphantom{\textsc{qt}}\hsmash{\textsc{pt}}, measured.

\documentclass{memoir}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend entries={
    {\textsc{pt}, measured},
    {\textsc{qt}, measured},
    {\textsc{pt}, regression},
    {\textsc{qt}, regression}
    },
    legend cell align={left},
  ]
  \addplot {x}; \addplot {x^2}; \addplot {x^3}; \addplot {x^4};
  \end{axis}
\end{tikzpicture}
\end{document}
4
  • Are you familiar with \llap and \rlap?
    – Mico
    Feb 22 at 11:36
  • @Mico I hadn't heard of them. It seems they do exactly what I wanted hsmash to do?
    – Anna
    Feb 22 at 11:41
  • 1
    An other way would be to add an extra column in the legend, but it would probably not be worth it as you would need extra \addlegendimage{empty legend} and adjustments of the column spacing. Feb 22 at 13:00
  • @hpekristiansen Clever, thank you!
    – Anna
    Feb 27 at 9:20

1 Answer 1

5

You could use \makebox to ensure that all your pt and qt have the same width:

\documentclass{memoir}
\usepackage{tikz,pgfplots}

\newlength{\mylen}
\settowidth{\mylen}{\textsc{qt},}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[
    legend entries={
    {\makebox[\mylen][l]{\textsc{pt},} measured},
    {\makebox[\mylen][l]{\textsc{qt},} measured},
    {\makebox[\mylen][l]{\textsc{pt},} regression},
    {\makebox[\mylen][l]{\textsc{qt},} regression}
    },
    legend cell align={left},
  ]
  \addplot {x}; \addplot {x^2}; \addplot {x^3}; \addplot {x^4};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

0

You must log in to answer this question.

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