2

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}
johnbrc
  • 575
  • 1
    Don't write \caption{}..? – jon Apr 10 '14 at 17:19
  • @jon see my edited explanation of how my workflow creates this issue – johnbrc Apr 10 '14 at 17:23
  • Will any figures need a caption? If not, isn't it easy to do something like \let\caption\relax? (Probably need to do that after \usepackage{caption}.) Note: I don't know if Scrivener allows you to define arbitrary commands or not, or fiddle with the preamble... – jon Apr 10 '14 at 17:24
  • Thanks. I don't envisage any figures requiring a caption, no. I tried \let\caption\relax in the preamble, but that returns error: \caption undefined – johnbrc Apr 10 '14 at 17:28
  • Are you always putting \includegraphics[..]{...} \caption{..}? If so, one can use this "pattern" to perform certain replacements. Do you sometimes use \caption with a non-empty argument? – Werner Apr 10 '14 at 17:37
  • @jon : the error is returned w/r/to my MWE, not my document… yet you say it's working for you. I have \let\caption\relax on the ensuing line after \usepackage[labelformat=empty]{caption} – johnbrc Apr 10 '14 at 17:44
  • @jon: yes, I have full control over the preamble – johnbrc Apr 10 '14 at 17:45
  • @Werner I don't want to achieve this effect by a secondary find and replace: because of (described) workflow, I (re)compile / regenerate the LaTex file repeatedly, often several times a day. – johnbrc Apr 10 '14 at 17:52
  • @jon : where would I \listfiles? Terminal (OSX)? – johnbrc Apr 10 '14 at 17:53
  • Put \listfiles in the preamble; then check the .log after compilation. I have, e.g.: memoir.cls 2013/05/30 v3.7b and caption.sty 2013/05/02 v3.3-89. – jon Apr 10 '14 at 17:55
  • I have just run TexLive, which is how I obtained my version of Tex and it instructed me to upgrade to TexLive2013, so complying now… (takes >20mins) – johnbrc Apr 10 '14 at 17:55
  • log returns: caption.sty 2012/02/19 v3.2f and memoir.cls 2011/03/06 v3.6j configurable book, report, article document cl – johnbrc Apr 10 '14 at 17:58
  • Well those are older versions that may not interact so well. A variant on the \let command would be to do \def\caption{}, which will tyrannically overwrite any previous (or not) definitions of \caption... – jon Apr 10 '14 at 18:01
  • @johnbrc: My suggestion is not to use a secondary search-and-replace, but let LaTeX perform that. I just want to know what the general situation is like. You can also modify \caption to pick up whether it's argument is empty, and condition accordingly. More information in terms of the general situation is required though... – Werner Apr 10 '14 at 18:01
  • @Werner general situation is as you suggest: the pattern is regularly, I believe, exhaustively as you describe, but yes there are sometimes when I would prefer the option to use \caption with a non-empty argument. – johnbrc Apr 10 '14 at 18:18

2 Answers2

2

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}}%
  }%
}
Werner
  • 603,163
1

If you really don't use captions anywhere, redefine \caption to do nothing.

\renewcommand{\caption}{}

Which I think will do what you want.

bombcar
  • 1,512
  • 10
  • 20
  • that looks promising…will test on whole document before upmarking – johnbrc Apr 10 '14 at 17:31
  • @johnbrc -- If you are getting \caption undefined with my suggestions, you won't be able to \renewcommand it either since there is no command (yet) to REnew. – jon Apr 10 '14 at 17:33
  • This works on the MWE…but not on my document. Is that what your comment @jon implies I should expect? – johnbrc Apr 10 '14 at 17:48
  • Try the renewcommand right before begin document? – bombcar Apr 10 '14 at 18:43