# Remove colon in the caption of a figure without using caption package

I know that I can Remove colon in the caption of a figure if the caption is empty when I simply use the caption package. But let's say I am using a journal-specific class (e.g. elsarticle) that has it's own definition of caption layout. So adding the caption package gives me an unsupported document class (or package) warning.

So the question it - can I remove the colon from the caption of a figure if the caption is empty WITHOUT using the caption package?

-
I also use the elsarticle class, slightly redefined, because it's pretty and works well, not necessarily to submit to Elsevier journals. –  PatrickT Feb 22 at 10:03

A preliminary remark: If a journal you're submitting an article to requires you to use the elsarticle document class, it may be a poor idea to modify the caption style defined by the class. Your journal's style guidelines probably also state something to the effect that every float should have a (nonempty) caption, in which case some kind of separator (such as a colon) between the float's number and caption is generally needed.

Here's the definition of the \@makecaption command contained in the file elsarticle.cls

\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip\footnotesize
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}


The two arguments of the \@makecaption macro are [Float] <num> and the caption's text, respectively. [Float] can be "Figure", "Table", or the name of any other floating environment recognized by LaTeX.

There are two ways to suppress the insertion of a colon following the float's name and number.

Method 1 : Remove the colon globally because the mandatory arguments (the stuff inside curly braces) of all \caption commands will always be empty. To achieve this, you could load the etoolbox package and issue the following commands:

\makeatletter
\patchcmd{\@makecaption}{#1: #2}{#1}{}{}
\makeatother


Method 2: Remove the colon whenever (and only if) the mandatory argument of the \caption command is empty. In this case, you may want to employ the following redefinition of the \@makecaption command:

\makeatletter
\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip\footnotesize
\sbox\@tempboxa{#2}         % place contents of #2 into a scratch TeX box
\ifdim \wd\@tempboxa = 0pt  % test if scratch box has zero width
\centering #1 \par       % if yes, typeset only #1 (the float's name and number)
\else                       % if no, proceed with default definition
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\fi
\vskip\belowcaptionskip}
\makeatother

-
This will remove the colon also when the caption is not empty, which is not what the OP is asking for. However, I agree that if the style has always a colon and this is a submission, then every figure should have a non empty caption. –  egreg Jan 19 '12 at 15:52
OK, I had interpreted the OP's question as wanting to remove the colon in all instances. Let me rework the answer to that the colon removal occurs only if the argument of the \caption command is truly empty. –  Mico Jan 19 '12 at 15:55
@Mico It's my first LaTeX submission so thank you for highlighting the issue with modifying journal's default style commands. However, the journal requires me to give figure captions as a list separately from the figures. So I am not sure how to deal with that, without modifying \@makecaption as you, @egreg and @Holle suggested... –  Geek On Acid Jan 19 '12 at 16:07
@GeekOnAcid: To provide a list of figures (on a separate page, right?), just type \clearpage\listoffigures\clearpage. This generates the equivalent of a table of contents, but containing only the figures. Likewise, the command \listoftables generates a list of all tables. –  Mico Jan 19 '12 at 16:51
@Mico Yes, I know that, but the point is that if I generate the \listoffigures without giving any name in curly brackets for \caption (i.e. \caption[text]{}) it will create the list but also create a colon for the figure label - which is what I am trying to avoid:) –  Geek On Acid Jan 19 '12 at 17:27

First of all, let me remark that if you're preparing a paper for a submission to a journal, such a change will be probably removed (and will annoy the people in charge of production). If the publisher's style wants a colon there, it means that no caption should be empty.

Anyway, here's a patch for elsarticle:

\documentclass{elsarticle}
\usepackage{etoolbox}
\makeatletter
\patchcmd{\@makecaption}{#1: #2}
{#1\sbox8{#2}\ifdim\wd8=\z@\else: #2\fi}
{}{\ddt}
\makeatother

\begin{document}
\begin{figure}
\caption{Here}
\end{figure}

\begin{figure}
\caption{}
\end{figure}
\end{document}


The original definition is

\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip\footnotesize
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}


so I patch the command with a check that the argument #2 (the caption text) is empty. However the usual \if\relax\detokenize{#2}\relax fails, because the caption text always begins with \ignorespaces. Thus it's better to check if the caption text typesets something.

-
Is the second patch command necessary? The \else part of the \ifdim clause will only be entered if the width of the box containing #1: #2 exceeds \hsize. This, in turn, will only ever happen (unless \hsize is truly minuscule!) if #2 is nonempty, won't it? –  Mico Jan 19 '12 at 17:02
@Mico You're right. –  egreg Jan 19 '12 at 17:06

If you use the class elsarticle than you can (re)format the figure caption with the following lines:

\makeatletter

\long\def\@makecaption#1#2{%
\vskip\abovecaptionskip\footnotesize
\sbox\@tempboxa{#1: #2}%
\ifdim \wd\@tempboxa >\hsize
#1: #2\par
\else
\global \@minipagefalse
\hb@xt@\hsize{\hfil\box\@tempboxa\hfil}%
\fi
\vskip\belowcaptionskip}

\makeatother

-