[Tex/LaTex] Subfloat: Label beside, caption below

floatssubfloats

I'm looking for a possibility to put the label (a),(b),(c)… of a subfigure beside the subfigure and the caption below. So I'm tried sidesubfigure (Subfig label positioning), which puts the label exactly as I want to, but I didn't find a solution to put the caption below the image.

\documentclass{article}
    \usepackage{floatrow}
    \usepackage{subfig}
    \usepackage{caption}
    \usepackage{rule}

\begin{document}

\begin{figure}
\sidesubfloat[description]{\rule{2cm}{2cm}}
\hfill
\sidesubfloat[description]{\rule{2cm}{2cm}}
\end{figure}

\end{document}

Is there a possiblity to do that?

enter image description here

Edit: Just got another problem, when I'm adding 4 same size picture in 2 rows like this, the pictures are shifted:

\begin{figure}[H]
    \begin{mysubfigure}{desc}{imageA}
        \includegraphics[width=4cm, height=4cm]{imageA}
    \end{mysubfigure}
    \hfill
    \begin{mysubfigure}{shorter desc}{imageB}
        \includegraphics[width=4cm, height=4cm]{imageB}
    \end{mysubfigure}
    \newline
        \begin{mysubfigure}{desc}{imageC}
        \includegraphics[width=4cm, height=4cm]{imageC}
    \end{mysubfigure}
    \hfill
    \begin{mysubfigure}{shorter desc}{imageD}
        \includegraphics[width=4cm, height=4cm]{imageD}
    \end{mysubfigure}
\caption{Two images: \subref{imageA} is imageA, \subref{imageB} is imageB}
\end{figure}

enter image description here

Best Answer

Here is a solution using the stackengine (and the varwidth) package.

enter image description here

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{subcaption}
\usepackage{varwidth}
\usepackage{stackengine}

\usepackage{lipsum}
\usepackage{float}

\DeclareCaptionLabelFormat{none}{}
\captionsetup[subfigure]{justification=centering, labelformat=none}

\newsavebox{\mybox}
\newlength{\mylength}

\makeatletter
\newenvironment{mysubfigure}[2]{%
    \def\arg@caption{#1}%
    \def\arg@label{#2}%
    \def\arg@ref{(\subref{\arg@label})}%
    \sbox{\mybox}{\begin{varwidth}{\linewidth}{\arg@ref}\end{varwidth}}%
    \edef\arg@reflen{\the\wd\mybox}%
    \edef\arg@refline{\the\dimexpr\ht\mybox+\dp\mybox}%
    \begin{lrbox}{\mybox}%
    \begin{varwidth}{\linewidth}%
}{%
    \end{varwidth}%
    \end{lrbox}%
    \setlength{\mylength}{\the\dimexpr\ht\mybox+\dp\mybox-\arg@refline}%
    \setlength{\mylength}{0.5\mylength}%
    \hspace{\arg@reflen}\subfigure[b]{\the\wd\mybox}%
        \centering%
        \toplap[\mylength]{l}{\arg@ref}\usebox{\mybox}%
        \caption{\arg@caption\label{\arg@label}}%
    \endsubfigure%
}
\makeatother

\begin{document}

\lipsum[1]

\begin{figure}[H]
    \centering
    \begin{mysubfigure}{longer description than the other}{imageA}
        \includegraphics[width=3cm, height=6cm]{imageA}
    \end{mysubfigure}
    \begin{mysubfigure}{shorter desc}{imageB}
        \includegraphics[width=4cm, height=3cm]{imageB}
    \end{mysubfigure}
\caption{Two images: \subref{imageA} is imageA, \subref{imageB} is imageB}
\end{figure}

\lipsum[2]

\end{document}
Related Question