# align also ylabels in a TikZ matrix

I am aligning many pgfplots with the TikZ \matrix. The axes are automatically aligned correctly. How can I also align the ylabels automatically (because I am changing plotted data very often)?

\usepackage[latin1]{inputenc}
\usepackage[USenglish]{babel}

\usepackage{tikz,pgfplots}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure} [H]
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1}]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2}]
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}

\end{document}


Not desired output:

Desired output:

Again I had a close look at:

• Manual for pgfplots by Christian Feuersaenger
• Manual for TikZ & PGF by Till Tantau

## edit

thanks! Especially the the finer control is really good.

Unfortunately now -- with this ylabel absolute -- linebreaks with \\ or \newline aren't possible anymore :-(

Is there a trick?

not working code:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel  1 first line\\second line},myylabel absolute=-60]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},,myylabel absolute=-60]
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}

\end{document}

-
I added a new option to my updated answer. –  Gonzalo Medina Jul 8 '13 at 2:37
I updated my answer with a solution for the new requirement. –  Gonzalo Medina Jul 8 '13 at 15:10

The ylabel absolute option (pages 173 and 174 of the pgfplots manual) produces the desired result:

\documentclass{article}

\usepackage{tikz,pgfplots}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},ylabel absolute]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},ylabel absolute]
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}

\end{document}


If you want even finer control, you can define a style in a similar fashion to ylabel absolute, but allowing you to specify, through an argument, the desired shifting (ylabel absolute uses -35pt):

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-30pt]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-30pt]
\end{axis}
\\
};
\end{tikzpicture}

\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 1},myylabel absolute=-40pt]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2},,myylabel absolute=-40pt]
\end{axis}
\\
};
\end{tikzpicture}
\end{figure}

\end{document}


After the edit to the original question: the problem is that you are trying to use labels with more than one line of text; in order to do so, you can, for example, use the align=<value> option for the label:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{
/pgfplots/myylabel absolute/.style={%
/pgfplots/every axis y label/.style={at={(0,0.5)},xshift=#1,rotate=90,align=center},
/pgfplots/every y tick scale label/.style={
at={(0,1)},above right,inner sep=0pt,yshift=0.3em
}
}
}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}
\matrix{
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel  1 first line\\second line},myylabel absolute=-50]
\end{axis}
\\
\begin{axis}[width=0.45\textwidth,height=0.45\textwidth,ylabel={ylabel 2 first line\\second line},myylabel absolute=-50]

@BenjaminMassow by default, the whole picture (including the labels) is centered with respect to the caption; if you want to exclude the labels, you can use trim axis left for tikzpicture, as in \begin{tikzpicture}[trim axis left]...\end{tikzpicture}` –  Gonzalo Medina Jul 9 '13 at 23:18