[Tex/LaTex] Remove colon in the caption of a figure without using caption package

captionspunctuation

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?

Best Answer

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