![如何使 cleveref 和 nameref 与 documentclass wlscirep 和 \section* 一起工作?](https://linux22.com/image/470840/%E5%A6%82%E4%BD%95%E4%BD%BF%20cleveref%20%E5%92%8C%20nameref%20%E4%B8%8E%20documentclass%20wlscirep%20%E5%92%8C%20%5Csection*%20%E4%B8%80%E8%B5%B7%E5%B7%A5%E4%BD%9C%EF%BC%9F.png)
问题:如何制作cleveref
并nameref
使用\documentclass{wlscirep}
和\section*
?
注意\documentclass{wlscirep}
来自“科学报告投稿模板“。下面我展示了我用cleveref
和测试的案例nameref
:
(1)cleveref
与\documentclass{article}
和\section
\documentclass{article}
\usepackage{cleveref}
\begin{document}
\section{Methods}\label{test}
We see in \cref{test} that ...
\end{document}
(2)cleveref
不适\documentclass{article}
用于\section*
\documentclass{article}
\usepackage{cleveref}
\begin{document}
\section*{Methods}\label{test}
We see in \cref{test} that ...
\end{document}
(3)cleveref
与\documentclass[fleqn,10pt]{wlscirep}
和\section
\documentclass[fleqn,10pt]{wlscirep}
\usepackage{cleveref}
\begin{document}
\section{Methods}\label{test}
We see in \cref{test} that ...
\end{document}
(4)cleveref
不适\documentclass[fleqn,10pt]{wlscirep}
用于\section*
\documentclass[fleqn,10pt]{wlscirep}
\usepackage{cleveref}
\begin{document}
\section*{Methods}\label{test}
We see in \cref{test} that ...
\end{document}
(5)nameref
与\documentclass[fleqn,10pt]{wlscirep}
和\section
\documentclass[fleqn,10pt]{wlscirep}
\usepackage{nameref}
\begin{document}
\section{Methods}\label{test}
We see in \nameref{test} that ...
\end{document}
(6)nameref
不适\documentclass[fleqn,10pt]{wlscirep}
用于\section*
\documentclass[fleqn,10pt]{wlscirep}
\usepackage{nameref}
\begin{document}
\section*{Methods}\label{test}
We see in \nameref{test} that ...
\end{document}
答案1
您的课程使用了 titlesec,而 titlesec 无法正确支持 hyperref 和 nameref。您可以尝试以下方法:
\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}