[Tex/LaTex] How to remove an empty caption line

captionsfloatsmemoirspacing

I'm generating LaTex automagically from a lengthy document that is written in the multimarkdown language, compiled by a software package called Scrivener. It's a workflow that works well, but this is one of the issues I'm hitting. I don't want to have to replace / delete numerous caption command each time I compile to LaTex.

The MWE below illustrates the issue.

This remove Figure n: and caption from included graphic. However, it leaves a blank line, which makes the images spacing appear gauche. Is there a simple way to remove the blank line , without inserting space adjustments, each time a figure is used Thanks.

\documentclass{memoir}

\usepackage{mwe}                    %   for dummy images
\usepackage[labelformat=empty]{caption} %   remove figure captions

\begin{document}

 \begin{figure}[!ht]
  \centering
  \includegraphics[width=0.5\textwidth]{image}                   
  \caption{}
  \end{figure}
   This is first text after caption.

\end{document}

Best Answer

memoir provides its own interface for caption management, and there may be some clashes when using it in conjunction with caption (see section 10.13 The class versus the caption package (and its friends), p 206 of the memoir user manual).

However, you can update \caption to grab its contents, evaluate whether the argument is empty/not, and condition accordingly:

enter image description here

\documentclass{memoir}

\usepackage{mwe}                    %   for dummy images
\usepackage[labelformat=empty]{caption} %   remove figure captions

% http://tex.stackexchange.com/a/58638/5764
\makeatletter
\def\ifemptyarg#1{%
  \if\relax\detokenize{#1}\relax % H. Oberdiek
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
\makeatother

\let\oldcaption\caption
\AtBeginDocument{%
  \renewcommand{\caption}[2][]{%
    \ifemptyarg{#2}{}{\oldcaption[#1]{#2}}%
  }%
}

\begin{document}

\begin{figure}[!ht]
  \centering
  \includegraphics[width=0.5\textwidth]{image}                   
  \caption{}
\end{figure}
This is first text after caption.

\newpage

\begin{figure}[!ht]
  \centering
  \includegraphics[width=0.5\textwidth]{image}                   
  \caption{Some caption}
\end{figure}
This is first text after caption.

\end{document}

If you're not using the optional argument of \caption in your workflow, then the redefinition could be a bit simpler:

\AtBeginDocument{%
  \renewcommand{\caption}[1]{%
    \ifemptyarg{#1}{}{\oldcaption{#1}}%
  }%
}
Related Question