好吧,我正在嘗試寫一個關於我的模板的簡短摘要,但我已經來到了十字路口。在我的類別定義中,我重新定義了\section{}
命令以\section*{}
保持模板的某種風格,但現在我想獨立引用這些部分,但保留如果我編寫\section{A random title}
標題將保持未編號的屬性。
讓我更詳細地解釋一下:
我
\section{}
用這些程式碼行重新定義了命令:\let\oldsection\section \renewcommand{\section}[1]{% \oldsection*{#1} \phantomsection \addcontentsline{toc}{section}{#1} }
當我
\ref{A label given to the section}
在兩個不同的部分中使用時,我得到以下結果:
- 我不需要對
\section{}
命令進行編號,因為我已經建立了一個小摘要,其中包含章節中每個部分的名稱,如下所示:
我認為沒有必要包含這個小摘要的程式碼,以免貼文資訊過多。
本質上,我需要幫助來弄清楚如何使這項\ref{}
工作發揮作用。
答案1
有可能\ref{}
按照自己的意願進行工作。您需要\@currentlabel
在 next 之前重新定義\label
。在您的情況下,這將位於重新定義的\section
.
由於您加載了hyperref
,因此不需要,\phantomsection
因為每個加星標的部分已經添加了錨點。也建議使用\NewCommandCopy
而不是\let
.
以下範例產生:
我稍微擴展了您的宏,以便\ref{}
在指定時使用較短的部分標題。我還假設冒號後面是頁碼,因為在這種情況下節號是沒有意義的。
\documentclass{report}
\usepackage[colorlinks]{hyperref}
\usepackage{blindtext}
\title{The Title}
\author{First Last}
\date{}
\makeatletter
\NewCommandCopy\oldsection\section
\RenewDocumentCommand\section{O{#2}m}{%
\oldsection*{#2}\addcontentsline{toc}{section}{\numberline{}#2}%
\edef\@currentlabel{(titled as "#1")}}
\makeatother
\begin{document}
\maketitle
\tableofcontents
\chapter{Chapter one}
\section{Section: Aaa}\label{sec:aaa}
\Blindtext
\chapter{Chapter two}
\section[Bbb]{Section: Bbb}\label{sec:bbb}
\Blindtext
\chapter{Chapter three}
Referring to the section 1 \ref{sec:aaa}: \pageref{sec:aaa}.\\*
Referring to the section 2 \ref{sec:bbb}: \pageref{sec:bbb}.
\end{document}