1

I have 4 subfigures in a 2x2 grid. The figures are made in Matlab by using matlab2tikz, and hence the TikZ files are inputted. The problem is, that the y-ticks are different and therefore the subfigures (see (a) and (c)) have different widths. As you can see from the figure below, I would like to vertically align the right-hand side of subfigures (a) and (c) without manually adding a \hspace before subfigure (a). How is this done?

Here is a MWE:

\documentclass{memoir}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\newsubfloat{figure}
\usepackage{siunitx}

\begin{document}

\begin{figure}
    \centering
    \newlength\figureheight 
    \newlength\figurewidth 
    \setlength\figureheight{0.4\textwidth}
    \setlength\figurewidth{0.35\textwidth}
    \subbottom[Subcaption1]{\input{flowUniformityOutletSimple_n.tikz}}
    \hfill
    \subbottom[Subcaption2]{\input{flowUniformityOutletFail_n.tikz}}
    %
    \subbottom[Subcaption3]{\input{flowUniformityOutletSimple_t.tikz}}
    \hfill
    \subbottom[Subcaption4]{\input{flowUniformityOutletFail_t.tikz}}
    \caption{Main caption}
\end{figure}

\end{document}

subfigures

2
  • 2
    You could possibly set the width of the ticklabels to the same in all cases with something like 'extraAxisOptions','yticklabel style={text width={width("$-0.6$")},align=right}' for matlab2tikz, where $-0.6$ is the widest ticklabel. May 19, 2016 at 9:02
  • @TorbjørnT. This seems to work well. Would you mind making it an answer for me to accept?
    – Thomas
    May 19, 2016 at 9:14

1 Answer 1

3

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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .