Document Classeselsarticle | Modifications to Caption Labels

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
Mahsa
Posts: 4
Joined: Mon Mar 26, 2012 4:07 pm

elsarticle | Modifications to Caption Labels

Post by Mahsa »

Hi all,

I am not allowed to use the caption package as the journal asks not to do so. I am using the elsarticle class and I would like to replace the colon in the figure captions with the dot so that "Figure 3:" becomes "Fig. 3.".

Using the \renewcommand{\fnum@figure}{Fig.~\thefigure}, I change "Figure" to "Fig.". but I still cannot get rid of the colon. Any tips on that? I appreciate it.


Regards,
Mahsa.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

elsarticle | Modifications to Caption Labels

Post by localghost »

The elsarticle class relies on the standard »article« class, where you can find the formatting for the float captions. Try the following lines in the preamble of your document (untested).

Code: Select all

\renewcommand{\figurename}{Fig.}
\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \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

Best regards and welcome to the board
Thorsten
Mahsa
Posts: 4
Joined: Mon Mar 26, 2012 4:07 pm

Re: elsarticle | Modifications to Caption Labels

Post by Mahsa »

Thanks a lot.
Actually I just started with Lyx and am planning to switch later to latex, so, I am almost on board :).
I pasted the code in the preamble of the lye doc, but it gives errors: "You can't use `\spacefactor' in vertical mode" and "LaTeX Error: Missing \begin{document}." and ...
and I don't have a clue on how to fix it. Perhaps it wouldn't work with lyx...???
Regards,
Mahsa.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

elsarticle | Modifications to Caption Labels

Post by localghost »

Mahsa wrote:[…] Actually I just started with Lyx and am planning to switch later to latex, so, I am almost on board.[…]
That's an important information that you should have given right at the beginning. I wouldn't have answered then because I don't use LyX. I can only recommend not to wait and to use LaTeX from now on. But that's my personal thinking.
Mahsa wrote:[…] Perhaps it wouldn't work with lyx...? […]
Can't tell you (see above).
Mahsa
Posts: 4
Joined: Mon Mar 26, 2012 4:07 pm

Re: elsarticle | Modifications to Caption Labels

Post by Mahsa »

I exported the doc from Lyx to Latex, and pasted the code in the preamble, it does not work in Latex, it gives an error of undefined control sequence, Illegal unit of measure, etc...Any other hints??? Thanks in advance.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

elsarticle | Modifications to Caption Labels

Post by localghost »

This example compiles flawlessly using LaTeX. The proposed additions for the preamble work.

Code: Select all

\documentclass[%
  3p,               % Document type
  10pt,             % Font size
  a4paper,          % Paper size
  sort&compress,    % »natbib« option
  times,            % »txfonts« package for Times style
  twocolumn,        % two-column text layout
]{elsarticle}
\usepackage[T1]{fontenc}
\usepackage{lipsum}

\renewcommand{\figurename}{Fig.}
\renewcommand{\tablename}{Tab.}
\makeatletter
\long\def\@makecaption#1#2{%
  \vskip\abovecaptionskip
  \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

\begin{document}
  \title{The Title}
  \author[addr]{First Author\corref{ca}}
  \ead{e.mail@provider.org}
  \author[addr]{Second Author}
  \author[addr]{Third Author}
  \address[addr]{Institution, City, Country}
  \cortext[ca]{Corresponding author.}

  \begin{abstract}
    \lipsum[1]
  \end{abstract}
  \begin{keyword}
    keyword\sep keyword\sep keyword\sep keyword

    \PACS 71.35.Be\sep 16.20.Hr\sep 85.10.Cr\sep 32.54Er
  \end{keyword}
  \maketitle

  \lipsum[2]

  \begin{figure}[!ht]
    \centering
    \rule{0.8\linewidth}{0.45\linewidth}
    \caption{Dummy Figure}
    \label{fig:dummy}
  \end{figure}

  \lipsum[3]

  \begin{table}[!ht]
    \centering
    \rule{0.8\linewidth}{0.45\linewidth}
    \caption{Dummy Table}
    \label{tab:dummy}
  \end{table}

  \lipsum[4]
\end{document}
The obtained output is attached.
Attachments
EVexample.pdf
The output as obtained by the given code example.
(65.27 KiB) Downloaded 357 times
Mahsa
Posts: 4
Joined: Mon Mar 26, 2012 4:07 pm

Re: elsarticle | Modifications to Caption Labels

Post by Mahsa »

Great, Thanks a lot! It works on your doc indeed and now also on my document.

For some reason on my doc it was giving an error and I have no idea why...I had to make the slightest change of "\renewcommand{\figurename}{Fig.} " to "\renewcommand{\fnum@figure}{Fig.~\thefigure}" together with the rest of the code you provided, the problem is solved and the colon is removed. Thanks again. I really appreciate it :)
Post Reply