0

QUESTION: How to make cleveref and nameref work with \documentclass{wlscirep} and \section*?

Note that \documentclass{wlscirep} comes from "Template for submissions to Scientific Reports". Here following I show the cases I tested with cleveref and nameref:

(1) cleveref works with \documentclass{article} and \section

\documentclass{article}
\usepackage{cleveref}
\begin{document}
\section{Methods}\label{test}
We see in \cref{test} that ...
\end{document}

enter image description here

(2) cleveref does not work with \documentclass{article} and \section*

\documentclass{article}
\usepackage{cleveref}
\begin{document}
\section*{Methods}\label{test}
We see in \cref{test} that ...
\end{document}

enter image description here

(3) cleveref works with \documentclass[fleqn,10pt]{wlscirep} and \section

\documentclass[fleqn,10pt]{wlscirep}
\usepackage{cleveref}
\begin{document}
\section{Methods}\label{test}
We see in \cref{test} that ...
\end{document}

enter image description here

(4) cleveref does not work with \documentclass[fleqn,10pt]{wlscirep} and \section*

\documentclass[fleqn,10pt]{wlscirep}
\usepackage{cleveref}
\begin{document}
\section*{Methods}\label{test}
We see in \cref{test} that ...
\end{document}

enter image description here

(5) nameref works with \documentclass[fleqn,10pt]{wlscirep} and \section

\documentclass[fleqn,10pt]{wlscirep}
\usepackage{nameref}
\begin{document}
\section{Methods}\label{test}
We see in \nameref{test} that ...
\end{document}

enter image description here

(6) nameref does not work with \documentclass[fleqn,10pt]{wlscirep} and \section*

\documentclass[fleqn,10pt]{wlscirep}
\usepackage{nameref}
\begin{document}
\section*{Methods}\label{test}
We see in \nameref{test} that ...
\end{document}

enter image description here

cabohah
  • 11,455
Ommo
  • 835
  • 5
    What exactly is clever ref suppose to return here? Perhaps yuo should explain what your end goal is instead. Then there might be better ways – daleif Nov 10 '23 at 12:39
  • 3
    Since the section doesn't have a number, what do you expect \cleverref to do? – John Kormylo Nov 10 '23 at 12:39
  • You are both right... I edited my question.. :-) – Ommo Nov 10 '23 at 12:43
  • 2
    You want package nameref and command \nameref instead of cleveref and \cref. – cabohah Nov 10 '23 at 12:49
  • I agree with cabohah it is nameref you need, as far as I know cleverref does not collect the title of a sectional command. – daleif Nov 10 '23 at 12:52
  • nameref works for \documentclass{article}, but it does not work with my \documentclass[fleqn,10pt]{wlscirep}. Should I modify the current question or open a new one?? In addition, even though it worked, it would not have the link to bring you to the corresponding section of the document - Instead, with \cref, by clicking on the name (in the PDF), it would bring you to the corresponding section of the document... – Ommo Nov 10 '23 at 14:02
  • 1
    You should specify where that class comes from as it is not on CTAN – daleif Nov 10 '23 at 14:12
  • Yes, sorry, \documentclass[fleqn,10pt]{wlscirep} comes from "Template for submissions to Scientific Reports", (https://www.overleaf.com/latex/templates/template-for-submissions-to-scientific-reports/xyrztqvdccns) – Ommo Nov 10 '23 at 14:14
  • 1
    The class is terribly written, and loads hyperref before loading titlesec to modify sections. Just a guess, but that's not a good combination and is a possible source of the problem. If I were you I would not use this class at all. – Alan Munn Nov 10 '23 at 14:33
  • thanks a lot for your comment @AlanMunn! – Ommo Nov 10 '23 at 14:34

1 Answers1

3

Your class uses titlesec and titlesec doesn't properly support hyperref and nameref. You can try something like this:

\documentclass[fleqn,10pt]{article}

% loaded by the class \RequirePackage[explicit]{titlesec} \usepackage{hyperref}

% new code \titleformat{name=\section,numberless} {\large\sffamily\bfseries} {} {0em} {\leavevmode\MakeLinkTarget[section]{}\ignorespaces#1} []

\ExplSyntaxOn\makeatletter \def\ttl@straight@i#1[#2]#3{% \tl_if_empty:nTF {#2} {\NR@gettitle{#3}} {\NR@gettitle{#2}} \gdef\ttl@savemark{\csname#1mark\endcsname{#3}}% \let\ttl@savewrite@empty \def\ttl@savetitle{#3}% \gdef\thetitle{\csname the#1\endcsname}% \if@noskipsec \leavevmode \fi \par \ttl@labelling{#1}{#2}% \ttl@startargs\ttl@straight@ii{#1}{#3}}

\ExplSyntaxOff\makeatother

\begin{document}

\section*{Methods}\label{test} We see in \nameref{test} that ...

\section{Normal}\label{test2}

\nameref{test2}

\section[short]{Long}\label{test3}

\nameref{test3} \end{document}

enter image description here

Ulrike Fischer
  • 327,261
  • Manyyyy thanks @Ulrike Fischer! Very nice :-) It works also by using \documentclass[fleqn,10pt]{wlscirep} (instead of \documentclass{article}) :-) – Ommo Nov 10 '23 at 14:49