0

\subcaptionphantom is nice when the subfigures are marked on a single figure, but the subcaption package is not allowed at memoir's house today.

Thus a solution was made:

memoir label and ref subfigures inside bigger image

This however leaves a large amount of whitespace around the figure:

enter image description here

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
% =============== defining the \subcaptionphantom ===================================
\makeatletter
% \begin{macro}{\subcaptionphantom}
% \cs{subcaptionphantom}\oarg{list-entry}\marg{caption} is a hidden
% non-printed subcaption. Designed for the case if "(a)", "(b)" are
% already embedded in the figure itself. 
% Roughtly equivalent to \cs{phantomsubcaption} from the \Lpack{subcaption} package.
%
%    \begin{macrocode}
\newcommand{\subcaptionphantom}{%
  \bgroup    
    \let\label=\memsub@label
    \ifdonemaincaption\else
      \advance\csname c@\@captype\endcsname\@ne
    \fi
    \refstepcounter{sub\@captype}\@contkeep
    \@ifnextchar [%
      {\@memsubcapphantom{sub\@captype}}%
      {\@memsubcapphantom{sub\@captype}[\@empty]}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@memsubcapphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{memsubcap}, adapted
% for use in \cs{subcaptionphantom}.
%    \begin{macrocode}
\long\def\@memsubcapphantom#1[#2]#3{%
  \@tempdima=\hsize
  \vskip\subfloatcapskip
  \ifx \@empty #2
    \@memsubcaptionphantom{#1}{#3}{#3}%
  \else
    \@memsubcaptionphantom{#1}{#2}{#3}%
  \fi
  \vskip\subfloatcapskip
  \egroup}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@memsubcaptionphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{memsubcaption}, adapted
% for use in \cs{memsubcapphantom}.
%    \begin{macrocode}
\newcommand{\@memsubcaptionphantom}[3]{%

  \ifx \relax#2\relax \else
    \bgroup
      \let\label\@gobble
      \let\protect\string
      \def\@memsubcaplabel{\@nameuse{@@the#1}}%
      \xdef\@memsubfigcaptionlist{%
        \@memsubfigcaptionlist,%
  {\protect\numberline{\@memsubcaplabel}\noexpand{\ignorespaces #2}}}%
    \egroup
  \fi
  \@makesubfloatcaptionphantom{\@nameuse{@the#1}}{#3}%
  }

%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@makesubfloatcaptionphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{makesubfloatcaption}, adapted
% for use in \cs{memsubcaptionphantom}.
%    \begin{macrocode}
\newcommand{\@makesubfloatcaptionphantom}[2]{%
  \setbox\@tempboxa\hbox{%
    \@subcapsize
    {\phantom{\@subcaplabelfont#1}}{\ignorespaces #2}\unskip}%
  \@tempdimb=-\subfloatcapmargin
  \multiply\@tempdimb\tw@
  \advance\@tempdimb\@tempdima
  \hb@xt@\@tempdima{%
    \hss
    \ifdim \wd\@tempboxa >\@tempdimb
      \phantom{\memsubfig@caption{#1}}{#2}%
    \else
      \if@shortsubcap
        \phantom{\memsubfig@caption{#1}}{#2}%
      \else
        \box\@tempboxa
      \fi
    \fi
    \hss}}
%    \end{macrocode}
% \end{macro}
\makeatother
\begin{document}
\chapter{duck}
\lipsum[1-4]
\begin{figure}[h]
    \centering
%   \vspace{-4cm}
    \subcaptionphantom{\label{a}}~%
    \subcaptionphantom{\label{b}}~%
    \subcaptionphantom{\label{c}}~%
    \subcaptionphantom{\label{d}}~%
    \subcaptionphantom{\label{f}}~%
    \subcaptionphantom{\label{g}}~%
    \includegraphics{example-image-duck}
    \caption{Some figures.
        A figure named \subcaptionref{a}.
        A figure named \subcaptionref{b}.
        A figure named \subcaptionref{c}.
        A figure named \subcaptionref{d}.
        A figure named \subcaptionref{f}.
        A figure named \subcaptionref{g}.
    }
    \label{duck}
\end{figure}
\lipsum[5-10]
\end{document}

So far my solution has been the outcommented \vspace{-4cm} but that's imprecise and impractical. Is there a better way of removing the whitespace

  • Just wondering why you arentt just using subcaption, memoir should not have any issues with it. Also why those ~ before the %'s – daleif May 29 at 9:11
  • @daleif I did not make the figure, it's from an article I'm referencing. The \subcaptionphantom broke when adding ~% after it. – Thorbjørn E. K. Christensen May 29 at 9:42
  • @daleif I'm also using memoirs subfloats a lot (for my own figures). I thought those where incompatible with subcaption – Thorbjørn E. K. Christensen May 29 at 9:45
1

This seems to work

  1. in \@memsubcapphantom remove the skips and add \leavevmode
  2. in \@makesubfloatcaptionphantom make the box width zero instead of \@tempdima

There are probably better ways.

Code:

\documentclass{memoir}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{duckuments}
\newsubfloat{figure}
% =============== defining the \subcaptionphantom ===================================
\makeatletter
% \begin{macro}{\subcaptionphantom}
% \cs{subcaptionphantom}\oarg{list-entry}\marg{caption} is a hidden
% non-printed subcaption. Designed for the case if "(a)", "(b)" are
% already embedded in the figure itself. 
% Roughtly equivalent to \cs{phantomsubcaption} from the \Lpack{subcaption} package.
%
%    \begin{macrocode}
\newcommand{\subcaptionphantom}{%
  \bgroup%    
    \let\label=\memsub@label%
    \ifdonemaincaption\else%
      \advance\csname c@\@captype\endcsname\@ne%
    \fi%
    \refstepcounter{sub\@captype}\@contkeep%
    \@ifnextchar[%
      {\@memsubcapphantom{sub\@captype}}%
      {\@memsubcapphantom{sub\@captype}[\@empty]}}
%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@memsubcapphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{memsubcap}, adapted
% for use in \cs{subcaptionphantom}.
%    \begin{macrocode}
\long\def\@memsubcapphantom#1[#2]#3{%
  \@tempdima=\hsize%
%  \vskip\subfloatcapskip % <- removed
  \ifx\@empty#2%
    \leavevmode% <- added
    \@memsubcaptionphantom{#1}{#3}{#3}%
  \else%
    \leavevmode% <- added
    \@memsubcaptionphantom{#1}{#2}{#3}%
  \fi%
%  \vskip\subfloatcapskip % <- removed
  \egroup}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@memsubcaptionphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{memsubcaption}, adapted
% for use in \cs{memsubcapphantom}.
%    \begin{macrocode}
\newcommand{\@memsubcaptionphantom}[3]{%
  \ifx\relax#2\relax\else%
    \bgroup%
      \let\label\@gobble%
      \let\protect\string%
      \def\@memsubcaplabel{\@nameuse{@@the#1}}%
      \xdef\@memsubfigcaptionlist{%
        \@memsubfigcaptionlist,%
  {\protect\numberline{\@memsubcaplabel}\noexpand{\ignorespaces #2}}}%
    \egroup%
  \fi%
  \@makesubfloatcaptionphantom{\@nameuse{@the#1}}{#3}%
  }

%    \end{macrocode}
% \end{macro}

% \begin{macro}{\@makesubfloatcaptionphantom}
% Quick-and-dirty analog of \Lpack{memoir} \cs{makesubfloatcaption}, adapted
% for use in \cs{memsubcaptionphantom}.
%    \begin{macrocode}
\newcommand{\@makesubfloatcaptionphantom}[2]{%
  \setbox\@tempboxa\hbox{%
    \@subcapsize%
    {\vphantom{\@subcaplabelfont#1}}{\ignorespaces #2}\unskip}%
  \@tempdimb=-\subfloatcapmargin%
  \multiply\@tempdimb\tw@%
  \advance\@tempdimb\@tempdima%
  %\hb@xt@\@tempdima{% % <- changed
  \hb@xt@ 0pt{%
    \hss%
    \ifdim \wd\@tempboxa >\@tempdimb%
      \vphantom{\memsubfig@caption{#1}}{#2}%
    \else%
      \if@shortsubcap%
        \vphantom{\memsubfig@caption{#1}}{#2}%
      \else%
        \box\@tempboxa%
      \fi%
    \fi%
    \hss}}
%    \end{macrocode}
% \end{macro}
\makeatother
\begin{document}
\chapter{duck}
\lipsum[1-4]
\begin{figure}[h]
    \centering
    % \vspace{-4cm}
    \def\LABELS{%
    \subcaptionphantom{\label{a}}%
    \subcaptionphantom{\label{b}}%
    \subcaptionphantom{\label{c}}%
    \subcaptionphantom{\label{d}}%
    \subcaptionphantom{\label{f}}%
    \subcaptionphantom{\label{g}}}%
    \LABELS%
    \includegraphics{example-image-duck}
    \caption{Some figures.
        A figure named \subcaptionref{a}.
        A figure named \subcaptionref{b}.
        A figure named \subcaptionref{c}.
        A figure named \subcaptionref{d}.
        A figure named \subcaptionref{f}.
        A figure named \subcaptionref{g}.
    }
    \label{duck}
\end{figure}
\lipsum[5-10]
\end{document}
  • This still moves the picture slightly down, but it will be the same amount no matter how many phantoms are used (1 \baselineskip). thanks. – Thorbjørn E. K. Christensen May 29 at 9:48
  • The single baselineskip only happens the first time the command is used, that's a bit wierd to me – Thorbjørn E. K. Christensen May 29 at 10:14
  • I don't see any v movement, only h movement. We should probably use \vphantom not \phantom. Exactly which LaTeX version are you on? I'm using TL19. – daleif May 29 at 10:16
  • It was sporadic spaces, plus I changed to \vphantom (I don't think that change is necessary) – daleif May 29 at 10:31
  • 1
    the vspace came from me having an empty line after the phantom labels – Thorbjørn E. K. Christensen May 29 at 11:06
1

You can use \baselineskip to get the lineheight.

This can be inserted into the \vspace. So you can use \vspace{-n\baselineskip} where n is the number of \subcaptionphantom{} commands present.

This solution is still far from perfect, as it is not automatic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.