[Tex/LaTex] Cross-reference to appendix sections in elsarticle document class

cleverefelsarticle

I am trying to reference a result given in the appendix, but cleveref is giving "Appendix Appendix" (doubling it). A way to solve this problem is to add \def\appendixname{}

Not surprisingly, this also eliminates the name "Appendix" from the section title. How can I solve the problem while still keeping "Appendix" in the section title?

MWE:

main.tex

\documentclass[final,times,letterpaper,authoryear,12pt]{elsarticle}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{indentfirst}
\usepackage{amscd}
\usepackage{mathrsfs}
\usepackage{ulem}
\usepackage{bbm}
\usepackage{hyperref}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{setspace}
\usepackage{color}

\usepackage{subfiles}
\usepackage{graphicx}  
\usepackage[nameinlink]{cleveref} 
\usepackage{lmodern}
\usepackage{kpfonts}
\usepackage{etex,etoolbox}
\usepackage{amsthm,amssymb}
\usepackage{thmtools}
\usepackage{environ}
\crefname{lem}{lemma}{lemmas}
\crefname{prop}{Proposition}{Propositions}
\crefname{thr}{Theorem}{Theorems}
\crefname{cor}{Corollary}{Corollaries}
\crefname{ass}{Assumption}{Assumptions}
\crefname{df}{Definiton}{Definitons}
\usepackage{enumitem}
\usepackage{caption}
\usepackage{subcaption}
\usepackage[letterpaper,layout=letterpaper]{geometry}

\begin{document}

\subfile{testa}
\appendix
\subfile{appendix}
\subfile{appendixb}

\end{document}

testa.tex

\documentclass[main.tex]{subfiles}
\begin{document}

\section{Introduction}
This is an introduction. From now on we will hold Result A and B as true. We prove Result A in \Cref{regsol} and we prove Result B in \Cref{regsolB}.

appendix.tex

\documentclass[main.tex]{subfiles}
\def\appendixname{}

\begin{document}

\section{Proof of Result A}\label{regsol}
We show that...

\end{document}

appendixb.tex

\documentclass[main.tex]{subfiles}
\begin{document}

\section{Proof of Result B}\label{regsolB}
We show that...

\end{document}
\end{document}

Best Answer

The elsarticle document class redefines the \thesection macro in a quick and unfortunately rather dirty way when \appendix is executed:

\gdef\thesection{\appendixname\@Alph\c@section}

[Update mid-2020: The instruction in the most recent version of elsarticle.cls is

\gdef\thesection{\appendixname~\@Alph\c@section}

The only difference, relative to the code in the original form of my answer, is the insertion of an unbreakable space after \appendixname.]

This prefixes the word "Appendix" to the (Alpha-stye) section "number" (A, B, C, etc.) That's what's causing the cross-reference call-outs generated by cleveref to contain the string "Appendix" twice.

A quick fix consists of using the instruction \labelcref instead of \cref when making cross-references to sections located in the appendix part of the document.

A more elaborate fix, i.e., one which lets you continue to use \Cref and \cref to cross-reference sections in the appendix, involves inserting the following code block immediately after the \appendix instruction:

\gdef\thesection{\Alph{section}} % corrected redefinition of "\thesection"
\makeatletter
\renewcommand\@seccntformat[1]{\appendixname\ \csname the#1\endcsname.\hspace{0.5em}}
\makeatother

A separate comment: The preamble of your document is, to put it delicately, not organized all that well. For instance, you provide the option times to the \documentclass instruction; this option tells LaTeX to load the "Times Roman" font. Later, you load the lmodern font package, which loads the "Latin Modern" font family, followed by a loading of the kpfonts font package, which loads a clone of the "Palatino" font family. Which font family do you want? You may want to go through the preamble and determine which packages are really needed.


Here's an MWE that illustrates the workings of the "more elaborate fix" shown above. (I've simplified the preamble and the body of your test document in order to focus on the essentials.)

enter image description here

\documentclass[letterpaper,12pt]{elsarticle}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{geometry}
\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\begin{document}

\section{Introduction}

We prove Result A in \Cref{regsol} and we prove Result B in \Cref{regsolB}.

\appendix
%% now insert the new code block
\gdef\thesection{\Alph{section}}
\makeatletter
\renewcommand\@seccntformat[1]{\appendixname\ \csname the#1\endcsname.\hspace{0.5em}}
\makeatother


\section{Proof of Result A}\label{regsol}
We show that \dots

\section{Proof of Result B}\label{regsolB}
We show that \dots

\end{document}