1

I would like to align two plots containing legends on their outside:

\documentclass[a4paper]{article}

\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\begin{document}
\begin{figure}
  \begin{centering}
    \begin{subfigure}[t]{0.4\textwidth}
      \vskip 0pt
      \includegraphics[width=\textwidth]{plot1.tikz}
    \end{subfigure}
    ~
    \begin{subfigure}[t]{0.4\textwidth}
      \vskip 0pt
      \includegraphics[width=\textwidth]{plot2.tikz}
    \end{subfigure}
  \end{centering}
\end{figure}
\end{document}

Specifically, the two plot files contain the following:

% plot1.tikz
  \begin{tikzpicture}
    \begin{axis}[
        legend entries={Short caption},
        legend style = {at = {(0.5, -0.3)}, anchor = north},
        width=\textwidth,
        height=\textwidth]
      \addplot[color=red,mark=x] coordinates {
        (2,-2.8559703)
        (8,-7.9377747) };
    \end{axis}
  \end{tikzpicture}

% plot2.tikz
\begin{tikzpicture}
  \begin{axis}[
      legend entries={Long long long long long long caption},
      legend style = {at = {(0.5, -0.3)}, anchor = north},
      width=\textwidth,
      height=\textwidth]
    \addplot[color=red,mark=x] coordinates {
      (2,-2.8559703)
      (8,-7.9377747) };
  \end{axis}
\end{tikzpicture}

Unfortunately, the width of the caption of the second plot dominates the width of the plot itself. Therefore, the plot is shrunk and the sizes don't match up anymore:

enter image description here

I would like the layout of the legend to break this long line and to maintain a fixed width. How can I achieve that effect?

Is there a way to do so?

1

You can specify line breaks using \\, but in addition you must modify the legend style and specify a location to align, for example cells={align=left}.

As an aside, note that \centering is a switch command and isn't written as \begin{centering}...\end{centering}. I fixed that for you.

enter image description here

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{tikzscale}
\usepackage{subcaption}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}

\usepackage{filecontents} % <--- To make a self contained MWE

\begin{filecontents*}{plot1.tikz}
  \begin{tikzpicture}
    \begin{axis}[
        legend entries={Short caption},
        legend style = {at = {(0.5, -0.3)}, anchor = north},
        width=\textwidth,
        height=\textwidth]
      \addplot[color=red,mark=x] coordinates {
        (2,-2.8559703)
        (8,-7.9377747) };
    \end{axis}
  \end{tikzpicture}
\end{filecontents*}

\begin{filecontents*}{plot2.tikz}
\begin{tikzpicture}
  \begin{axis}[
      legend entries={Long long long \\ long long long \\ caption},
      legend style = {at = {(0.5, -0.3)}, anchor = north,cells={align=left}},
      width=\textwidth,
      height=\textwidth]
    \addplot[color=red,mark=x] coordinates {
      (2,-2.8559703)
      (8,-7.9377747) };
  \end{axis}
\end{tikzpicture}
\end{filecontents*}

\begin{document}
\begin{figure}
  \centering  % <--- Fixed
    \begin{subfigure}[t]{0.4\textwidth}
      \vskip 0pt
      \includegraphics[width=\textwidth]{plot1.tikz}
    \end{subfigure}
    ~
    \begin{subfigure}[t]{0.4\textwidth}
      \vskip 0pt
      \includegraphics[width=\textwidth]{plot2.tikz}
    \end{subfigure}
\end{figure}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.