[Tex/LaTex] Nested subfigures alignment

floatssubfloats

I'm having trouble creating a 2×2 grid of subfigures. With this tex below, I get a 1×4 grid instead. I'm probably not understanding how the 0.5/textwidth worked, I presumed it used the parent's textwidth?

\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.5\textwidth}
          \centering
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-crop.jpg}
                  \caption{Left cropped}
          \end{subfigure}
          ~
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-shift.jpg}
                  \caption{Right shifted}
          \end{subfigure}
    \end{subfigure}
    ~
    \begin{subfigure}[b]{0.5\textwidth}
          \centering
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-stereo.jpg}
                  \caption{Anaglypah of Stereo Pair}
          \end{subfigure}
          ~
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-diff.jpg}
                  \caption{Difference of Stereo Pair}
          \end{subfigure}
    \end{subfigure}
    \caption{\texttt{phone0018.pgm} with $D_h = 10$}\label{fig:contrast}
\end{figure}

Best Answer

The subfigure environment from the subcaption package takes all the same arguments as a minipage.

The minipage environment adds a small amount of horizontal space at the end of the environment, which you can remove by using a % after \end{minipage}, or in this case \end{subfigure}.

All I've done in the MWE below is replace ~ with %; you are correct about your description of \textwidth, it uses the parent value.

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subcaption}

\begin{document}
\begin{figure}[h!]
    \centering
    \begin{subfigure}[b]{0.5\textwidth}
          \centering
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-crop.jpg}
                  \caption{Left cropped}
          \end{subfigure}%
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-shift.jpg}
                  \caption{Right shifted}
          \end{subfigure}
    \end{subfigure}
    \begin{subfigure}[b]{0.5\textwidth}
          \centering
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-stereo.jpg}
                  \caption{Anaglypah of Stereo Pair}
          \end{subfigure}%
          \begin{subfigure}[b]{0.5\textwidth}
                  \centering
                  \includegraphics[width=\textwidth]{phone0018-A-diff.jpg}
                  \caption{Difference of Stereo Pair}
          \end{subfigure}
    \end{subfigure}
    \caption{\texttt{phone0018.pgm} with $D_h = 10$}\label{fig:contrast}
\end{figure}
\end{document}