1

Suppose we have the following:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
\begin{axis}[%legend cell align={&+},?
             legend entries={a+b, aaaa+bbb},]
    \addplot {x};
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

Is it possible to align text in a legend with respect to a certain character? e.g. like we might do in an align environment

\begin{align}
    a &+ b\\
    aaaa &+ bbb
\end{align}

I know we can do legend cell align=left etc. but this will only work for the above case when the legend entries themselves meet a certain criteria.

As an aside, where do I find a comprehensive documentation page for pgfplots? I have been referring to this although it is not all inclusive.

algae
  • 285
  • 1
    Welcome to TeX.SX! To answer the second part of the question, you can find the documentation of pgfplots on CTAN, on this page. – Vincent Jan 23 '20 at 03:24
  • You do realize that all externalize does is run TikZ using standalone in a separate process and use \includegraphics for the image file created. – John Kormylo Jan 23 '20 at 15:53
  • Right... In the end I just need something like this and your solution works with externalize anyway, despite your chip. – algae Jan 24 '20 at 01:19

2 Answers2

1

Here is one way to achieve this alignment.

\documentclass{standalone}
\usepackage{eqparbox}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
% \usepgfplotslibrary{external}
% \tikzexternalize
\begin{document}
\begin{tikzpicture}
\begin{axis}[%legend cell align={&+},?
             legend entries={\eqmakebox[r1][r]{$b$}\eqmakebox[l1][l]{${}+b$},
              \eqmakebox[r1][r]{$aaaa$}\eqmakebox[l1][l]{${}+bbb$}}]
    \addplot {x};
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

One may simplify the usage. Whether there is a stable way to marry this to externalize I do not know.

  • Thanks, I'll accept soon if there are no suggestions on what to do with externalize. I'm very new to pgf but it seems all but the most trivial cases require the externalize option to have anything compile due to size limitations. i.e. imported data needs to be further discretized into smaller sets which is difficult for curves with fine structure. – algae Jan 23 '20 at 04:29
1

Here is another way to do it. See also \mathrlap and \mathllap from mathtools.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{external}
%\tikzexternalize% sorry, not on my system
\begin{document}
\begin{tikzpicture}
\begin{axis}[%legend cell align={&+},?
             legend entries={\phantom{aaaa}\llap{a}+\rlap{b}\phantom{bbb}, aaaa+bbb},]
    \addplot {x};
    \addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}
John Kormylo
  • 79,712
  • 3
  • 50
  • 120