引用标题中带有计数器的部分的正确方法是什么?目录生成正确,但交叉引用就没那么幸运了。
梅威瑟:
\documentclass[letterpaper, 12pt, twoside]{exam}
\usepackage{hyperref}
\newcounter{KSRLessonNumberCounter}
\setcounter{KSRLessonNumberCounter}{1}
\newcommand{\KSRSection}[2][\value{KSRLessonNumberCounter}]{
\setcounter{KSRLessonNumberCounter}{#1}
\section*{Lesson \arabic{KSRLessonNumberCounter}: #2}
\addcontentsline{toc}{section}{Lesson \arabic{KSRLessonNumberCounter}: {#2}}
\stepcounter{KSRLessonNumberCounter}
}
\newcommand{\KSRSubsection}[1]{
\subsection*{#1}
\addcontentsline{toc}{subsection}{#1}
}
\newcommand{\KSRLessonNumber}{
}
\begin{document}
\KSRSection{Sample}
This is the first lesson. The second lesson is \nameref{sec:blah}.
\KSRSection{Blah}
\label{sec:blah}
This is the second section
\KSRSection{Lorem Ipsum}
Dollars are Set in \nameref{sec:blah}.
\end{document}
答案1
这是扩展问题。标题字符串未扩展,且包含:
Lesson \arabic {KSRLessonNumberCounter}: Blah
包hyperref
加载nameref
,使用包gettitlestring
获取干净的标题字符串(删除\label
、\index
、...)。有两种方法,传统的不扩展方法或带扩展的版本。前者是默认方法。通过切换到方法,expand
问题得到解决:
\usepackage{hyperref}
\usepackage{nameref}% loads gettitlestring
\GetTitleStringSetup{expand}
答案2
与 Heiko 的解决方案不同的是,它使用标准名称。您只需输入\subsection
小节,无需*
;对于节的引用,请使用\lectref
。
\documentclass[letterpaper, 12pt, twoside]{exam}
\usepackage[colorlinks]{hyperref}
\makeatletter
\renewcommand{\@seccntformat}[1]{Lesson \csname the#1\endcsname: }
\makeatother
\newcommand{\lectref}[1]{%
\hyperref[#1]{Lesson~\ref*{#1}: \nameref*{#1}}%
}
\setcounter{secnumdepth}{1}
\begin{document}
\section{Sample}
This is the first lesson. The second lesson is \lectref{sec:blah}.
\section{Blah}\label{sec:blah}
This is the second section
\subsection{A subsection}
Here it is
\section{Lorem Ipsum}
Dollars are Set in \lectref{sec:blah}.
\end{document}
我曾经colorlinks
在照片中展示过它们。