[Tex/LaTex] New line in pgfplots/tikz not displaying in TeX

legendline-breakingmatlab2tikzpgfplotstikz-pgf

I have a multiple-line legend entry to preserve the width of my figure. I can use
\newline in Matlab and the legend entry is split between two lines. When I produce a .tikz of the figure however, I can't figure out how to get a new line… It appears there was a similar issue or the same issue github of matlab2tikz but when I use this syntax the same issue arises where Matlab shows the multi-line legend entry but LaTeX doesn't. Am I missing a package or writing the syntax wrong ?

LaTeX code generated by matlab2tikz to display the multiline legend (\newline works in matlab, tried "\n" and tried removing all of the \text{}'s which didn't help – the \newline command is just ignored by LaTeX):

\addlegendentry{$\text{R}_{\text{Darcy,pwr}}\text{ = [1.26x10}^{\text{10}}\text{, 3.76x10}^{\text{9}}\text{] (1/m)**\newline** MAE=1.14x10}^{\text{-2}}\text{(g/min)}$};

Unfortunately I can't post images because I don't have 10 repuation… sorry about the
links:

Matlab output:

enter image description here

LaTeX output:

enter image description here

Best Answer

Updated

According to @Jake's comment, adding the key [align=left] will allow one to type multiple lines in the legend (I guess this is the same for entering multi-line texts in a TikZ node):

\addlegendentry[align=left]{
  $\text{R}_\text{Darcy,pwr}=[1.26 \times 10^{10},\; 3.76\times 10^9]$ \\
  $\text{(1/m)MAE}=1.14\times 10^{-2} \text{ (g/min)}$
}

A working example:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{amsmath}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  legend style={at={(1.1,-0.1)},
  cells={anchor=west}, % left-align cell content
}]

\addplot coordinates {(0,0) (1,1)};
\addlegendentry{$\downarrow\Delta P$, 1$^{st}$ cycle}

\addplot coordinates {(0,1) (1,2)};
\addlegendentry{DGM $\delta'_{\text{air}}=4.8\times 10^{-8}$ m-kPa}

\addplot coordinates {(0,2) (1,3)};
\addlegendentry[align=left]{
  $\text{R}_\text{Darcy,pwr}=[1.26 \times 10^{10},\; 3.76\times 10^9]$ \\
  $\text{(1/m)MAE}=1.14\times 10^{-2} \text{ (g/min)}$
}

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Related Question