In general, this is a little bit tricky, as there so many things influencing the size, but for this case, where the ticklabels on the y-axis are the only thing causing problems, you could try adding
'extraAxisOptions','yticklabel style={text width= width("$-0.6$")},align=right}'
the matlab2tikz
calls, where $-0.6$
is the widest ticklabel.
As a sidenote, I suggest moving
\newlength\figureheight
\newlength\figurewidth
\setlength\figureheight{0.4\textwidth}
\setlength\figurewidth{0.35\textwidth}
to the preamble (before \begin{document}
), and only redefine the lengths in those figure
environments where you need to.
For aligning multiple axes with pgfplots
like this I would usually recommend using a single tikzpucture
and the features of the groupplots
library (see pgfplots
manual), but that will lead to a quite a bit more work, and a lot of modifications to the LaTeX code.
Just for reference, in case anyone is interested, here is one way of using groupplots
in this context, also integrating numbered subcaptions. One obvious drawback is the need for some manual adjustment of the spacing between axes, but that fairly easily done by modifying the vertical sep
(and horizontal sep
) in the group style
.
\documentclass{memoir}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots,units}
\pgfplotsset{
compat=1.9,
unit code/.code 2 args={\si{#1#2}} % from manual, for using siunitx to typeset units
}
\newsubfloat{figure}
\usepackage{siunitx}
\newlength\figureheight
\newlength\figurewidth
\setlength\figureheight{0.4\textwidth}
\setlength\figurewidth{0.35\textwidth}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{groupplot}[
width=\figurewidth,
height=\figureheight,
scale only axis,
title style={
at={(xticklabel cs:0.5)},
below=4ex,
text width=\figurewidth},
group style={
group size=2 by 2,
vertical sep=2cm,
horizontal sep=1.5cm},
ylabel=$v_n$,xlabel=$y$,
y unit=\m\per\s,x unit=\cm,
domain=4:5,samples=10,
]
\nextgroupplot[title={\subcaption{Subcaption 1}}]
\addplot {x};
\nextgroupplot[title={\subcaption{Subcaption 2}}]
\addplot {x^2};
\nextgroupplot[title={\subcaption{Subcaption 3. A longer one that will break across lines.}}]
\addplot {-x};
\nextgroupplot[title={\subcaption{Subcaption 4}}]
\addplot {x^2 + 4};
\end{groupplot}
\end{tikzpicture}
\caption{Main caption}
\end{figure}
\end{document}
'extraAxisOptions','yticklabel style={text width={width("$-0.6$")},align=right}'
formatlab2tikz
, where$-0.6$
is the widest ticklabel.