3

Following-up my previous question, I would like to left-align both text entries with their first ones below them, respectively. In other words, I need to move text to be left-aligned with yy=x^2 and another text to be left-aligned with zzyyz=x^3

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{mathtools,calc}
\newcommand{\LHS}[2][1.5em]{\hspace{#1}\mathllap{#2}}
\newcommand{\RHS}[2][1.5em]{\mathrlap{#2}\hspace{#1}} 


\begin{document}


    \begin{tikzpicture}
    \begin{axis}[
    legend cell align=left,
    legend columns=2,
    legend style={
        anchor=south,
        at={([yshift=2mm]current axis.north)},%,above=0cm,left=0mm,
        cells={align=center},
        /tikz/every even column/.append style={column sep=5mm}
}
    ]
    \addplot[red] {x};
    \addplot[blue,domain=0:2] {x^2};
    \addplot[orange] {-x};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \legend{
        text,
        another text,
        $\LHS{yy}=x^2$,
        $\LHS[3em]{zzyyz}=\RHS{x^3}$ reference,
        $\LHS{k}=xyz$,
        $\LHS[3em]{kk}=\RHS{xz}$ cite,
    }
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

Diaa
  • 9,599
  • Just add \hspace{0.5em} before text and another text. –  Jul 15 '18 at 12:13
  • @marmot Actually, I will have to manually adjust this spacing every single time I have a different equation left-hand-side, which I would like to avoid. – Diaa Jul 15 '18 at 12:22
  • 1
    Well, you sort of went this way when aligning the equality signs. In my answer below I show how to compute the distances. This also reveals that the distances between the red line and yy on the one hand and between the green line and zzyyz do not exactly coincide. You could make them coincide, of course, by choosing as first argument of \LHS some desired distance plus the width of yy or zzyyz. –  Jul 15 '18 at 12:29

1 Answers1

3

You are, in a way, manually adjusting the equations in order to get the equality sign aligned. You could then ask TikZ (or the calc package, not library) to compute the required horizontal space for you.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{mathtools,calc}
\newcommand{\LHS}[2][1.5em]{\hspace{#1}\mathllap{#2}}
\newcommand{\RHS}[2][1.5em]{\mathrlap{#2}\hspace{#1}} 


\begin{document}


    \begin{tikzpicture}
    \begin{axis}[
    legend cell align=left,
    legend columns=2,
    legend style={
        anchor=south,
        at={([yshift=2mm]current axis.north)},%,above=0cm,left=0mm,
        cells={align=center},
        /tikz/every even column/.append style={column sep=5mm}
}
    ]
    \addplot[red] {x};
    \addplot[blue,domain=0:2] {x^2};
    \addplot[orange] {-x};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \legend{\pgfmathsetmacro{\myw}{1.5em-width("$yy$")}%
        \hspace{\myw pt}text,\pgfmathsetmacro{\myw}{3em-width("$zzyyz$")}%
        \hspace{\myw pt}another text,
        $\LHS{yy}=x^2$,
        $\LHS[3em]{zzyyz}=\RHS{x^3}$ reference,
        $\LHS{k}=xyz$,
        $\LHS[3em]{kk}=\RHS{xz}$ cite,
    }
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

A perhaps more appealing output is achieved by fixing the indent, \myindent in the following MWE, and than shift the stuff accordingly. This might be the better choice even if you do not add text because then the indent will be universal.

\documentclass{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepackage{mathtools,calc}
\newcommand{\LHS}[2][1.5em]{\hspace{#1}\mathllap{#2}}
\newcommand{\RHS}[2][1.5em]{\mathrlap{#2}\hspace{#1}} 
\newlength\myindent
\myindent=0.5em

\begin{document}


    \begin{tikzpicture}
    \begin{axis}[
    legend cell align=left,
    legend columns=2,
    legend style={
        anchor=south,
        at={([yshift=2mm]current axis.north)},%,above=0cm,left=0mm,
        cells={align=center},
        /tikz/every even column/.append style={column sep=5mm}
}
    ]
    \addplot[red] {x};
    \addplot[blue,domain=0:2] {x^2};
    \addplot[orange] {-x};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \addplot[green] {x^3};
    \legend{\hspace{\myindent}text,
        \hspace{\myindent}another text,\pgfmathsetmacro{\mywleft}{\myindent+width("$yy$")}%
        $\LHS[\mywleft pt]{yy}=x^2$,\pgfmathsetmacro{\mywright}{\myindent+width("$zzyyz$")}%
        $\LHS[\mywright pt]{zzyyz}=\RHS{x^3}$ reference,\pgfmathsetmacro{\mywleft}{\myindent+width("$yy$")}%
        $\LHS[\mywleft pt]{k}=xyz$,\pgfmathsetmacro{\mywright}{\myindent+width("$zzyyz$")}%
        $\LHS[\mywright pt]{kk}=\RHS{xz}$ cite,
    }
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here