# Put Subcaptions on the right of vertically aligned subfigures

I'm struggling to find a solution for my problem. I want something like this

but for VERTICALLY aligned subfigures, as an attempt to save some vertical space (I can't reduce the size more than this)

I'm using \subfig package to have my subfigures aligned. here is my code:

\begin{figure}[ht!]

\centering
\begin{minipage}{\textwidth}
\centering
\subfloat[]
{
\includegraphics[width=0.6\textwidth, keepaspectratio]{fig_a}
\label{fig:a}
}
\\
\centering
\subfloat[]
{
\includegraphics[width=0.6\textwidth, keepaspectratio]{fig_b}
\label{fig:b}
}
\\
\centering
\subfloat[]
{
\includegraphics[width=0.6\textwidth, keepaspectratio]{fig_c}
\label{fig:c}
}
\caption{Three figures, take too much vertical space}
\label{fig:unified}
\end{minipage}
\end{figure}


Thank you for any help!

• As I pointed out, I can't reduce the size, otherwise labels in the image are not readable once printed. Plus, your comment is in no way a solution to my answer! – Fed Dec 16 '17 at 12:00
• @koleygr Wouldn't that be horizontal alignment instead of vertical? – TeXnician Dec 16 '17 at 12:00
• @Fed How do your subcaptions look like? Are that only one-letter-captions or may they need e.g. line breaking (if they are longer than the remaining space)? – TeXnician Dec 16 '17 at 12:01
• @TeXnician... You are right... I just read fast the post and thought that got the point... Deleting... Fed.. sorry! – koleygr Dec 16 '17 at 12:05
• @TeXnician Ideally they would be two or three words names (like "Logistic regression", "Naive Bayes" etc), but I'm also ok with letters like in the pictures, then I can explain to which algorithm they correspond in the "general" caption – Fed Dec 16 '17 at 12:09

Note that only the image is centered, not the caption.

Ideally one should put the \label immediately after the \caption, but in this case that isn't possible. Instead one should put the \label inside the caption. Otherwise some of the local macros used by \label may be lost.

\documentclass{article}
\usepackage{graphicx}
\usepackage{subcaption}
\captionsetup[sub]{justification=raggedright,singlelinecheck=false}
\usepackage{showframe}% debuggingh tool

\makeatletter
\newcommand{\mysub}[2][]% #1=caption (optional), #2=graphics
{\bgroup
\sbox0{#2}\usebox0
\dimen0=\dimexpr \textwidth-\wd0\relax
\ifx\\\@centercr \divide\dimen0 by 2\fi
\sbox1{\begin{minipage}[t]{\dimen0}
\subcaption{#1}%
\end{minipage}}%
\rlap{\raisebox{\dimexpr \ht0-\ht1}[0pt][0pt]{\usebox1}}\allowbreak
\egroup}
\makeatother

\begin{document}
\begin{figure}[p]
\centering
\mysub[A very long caption, just to test the limits. \label{fig:a}]
{\includegraphics[width=0.6\textwidth, keepaspectratio]{example-image-a}}

\mysub[\label{fig:b}]
{\includegraphics[width=0.6\textwidth, keepaspectratio]{example-image-b}}

\mysub[\label{fig:c}]
{\includegraphics[width=0.6\textwidth, keepaspectratio]{example-image-c}}

\caption{Three figures, take too much vertical space}
\label{fig:unified}
\end{figure}
\end{document}