# Elsevier cas-sc template: figure caption cannot be centered

I am using Elsevier's cas-sc.cls. Captions of figures are left-aligned, but I want the captions to be aligned to the center below the figure. I tried using \captionsetup{justification=centering} along with the package \usepackage{caption}, but that doesn't seem to work. Here's an MWE:

\documentclass[a4paper,fleqn]{cas-sc}

\usepackage{graphicx}
\usepackage{caption}

\begin{document}

\begin{figure}[pos=htbp]
\centering
\captionsetup{justification=centering}
\includegraphics[width=0.5\textwidth]{example-image-a}
\caption{My caption.}
\label{FIG}
\end{figure}

\end{document}


• Being cas-sc a documentclass for journal papers, you're supposed to stick with the template. Changing the format will not make the editor happy, I guess. Unless you are using it for other purposes, of course – Alessandro Cuttin Apr 15 at 15:01
• Welcome to TeX - LaTeX! Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Alessandro Cuttin Apr 15 at 15:05

The class provides a align option to the figure and table environments which sets the alignment of both the contents of the figure/table and the position of the caption. That doesn't work, however, because the caption is typeset in a \parbox with the width equal to the value of the width option, and inside the \parbox the text is justified by default. The correct way would be to put the alignment inside the \parbox.

There are three (hacky) ways to have what you want. The first (and slightly less hacky) is to use align=\centering to center the \parbox that contains the caption and use width=<width-of-the-typeset-caption> to make the width of the \parbox just right. It requires a bunch of manual tweaking to get the value of width right. 3cm seems to be a good guess for your example:

\begin{figure}[pos=htbp,width=3cm,align=\centering]


The second way (hacky, and depends on an implementation detail of els-cas), is to use the pseudo-hook of align to redefine \@parboxrestore to inject \centering if used inside a cas-sc caption. To do that you define:

\makeatletter
\def\redefparbox{\def\@parboxrestore{\@arrayparboxrestore\let\\\@normalcr
\if@minipage\expandafter\@gobbletwo\fi
\@firstofone{\centering\casscparboxtest}}}
\def\casscparboxtest#1{%
\ifx\rightskip#1\relax\expandafter\dimen@\else
\expandafter\@secondoftwo
\fi\@gobble{#1}}
\makeatother


and then use:

\begin{figure}[pos=htbp,align=\redefparbox]


The third way (also hacky, also depends on the implementation, and is politically incorrect), is to just redefine \__make_fig_caption:nn to put the align at the right place:

% -----------
% Definition for the third way:
\ExplSyntaxOn
% For figure:
\cs_gset_protected:Npn \__make_fig_caption:nn #1#2
{
\skip_vertical:N \l_fig_abovecap_skip
\parbox { \dim_eval:n { \l_fig_width_dim } }
{
\tl_use:N \l_fig_align_tl
\sffamily \small \textbf{\color{scolor}#1:}~#2\par
}
\skip_vertical:N \l_fig_belowcap_skip
}
% For table:
\cs_gset_protected:Npn \__make_tbl_caption:nn #1#2
{
\skip_vertical:N \l_tbl_abovecap_skip
\parbox{ \dim_eval:n { \l_tbl_width_dim } }
{
\tl_use:N \l_tbl_align_tl
\sffamily \small \textbf{\color{scolor}#1}\par#2\par\vskip4pt
}
\skip_vertical:N \l_tbl_belowcap_skip
}
\ExplSyntaxOff
% -----------


Here's an MWE with the three options marked:

 \documentclass[a4paper,fleqn]{cas-sc}
\usepackage{natbib}
\usepackage{graphicx}

% -----------
% Definition for the second way:
\makeatletter
\def\redefparbox{\def\@parboxrestore{\@arrayparboxrestore\let\\\@normalcr
\if@minipage\expandafter\@gobbletwo\fi
\@firstofone{\centering\casscparboxtest}}}
\def\casscparboxtest#1{%
\ifx\rightskip#1\relax\expandafter\dimen@\else
\expandafter\@secondoftwo
\fi\@gobble{#1}}
\makeatother
% -----------

% -----------
% Definition for the third way:
\ExplSyntaxOn
% For figure:
\cs_gset_protected:Npn \__make_fig_caption:nn #1#2
{
\skip_vertical:N \l_fig_abovecap_skip
\parbox { \dim_eval:n { \l_fig_width_dim } }
{
\tl_use:N \l_fig_align_tl
\sffamily \small \textbf{\color{scolor}#1:}~#2\par
}
\skip_vertical:N \l_fig_belowcap_skip
}
% For table:
\cs_gset_protected:Npn \__make_tbl_caption:nn #1#2
{
\skip_vertical:N \l_tbl_abovecap_skip
\parbox{ \dim_eval:n { \l_tbl_width_dim } }
{
\tl_use:N \l_tbl_align_tl
\sffamily \small \textbf{\color{scolor}#1}\par#2\par\vskip4pt
}
\skip_vertical:N \l_tbl_belowcap_skip
}
\ExplSyntaxOff
% -----------

\begin{document}

% \begin{figure}[pos=htbp,width=3cm,align=\centering] % First way
% \begin{figure}[pos=htbp,align=\redefparbox] % Second way
\begin{figure}[pos=htbp,align=\centering] % Third way
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}
\caption{My caption.}
\label{FIG}
\end{figure}

\end{document}