多章的目錄和交叉引用

多章的目錄和交叉引用

這是問題的後續多章交叉引用。根據這個問題的答案,我使用以下巨集:

\newcommand*\maybechapter[1]{%   
 % Tests if current chapter is equal to the chapter number of label)
 \ifnum\value{chapter}=0#1\relax
     % Print nothing if so
 \else
     % Set 'chapter' locally (=> no \setcounter) to the label chapter and
     % print it in the usual format followed by a dot
     {\value{chapter}=#1\relax\thechapter.}%
 \fi
}
\renewcommand*\thesection{%
    \protect\maybechapter{\arabic{chapter}}\arabic{section}%
}

這按照參考的預期工作,但在目錄中,章節號現在都前面有其章節號:

I The first chapter
    I.1 The first section of the first chapter

但我想要得到類似的東西

I The first chapter
    1 The first section of the first chapter

我應該如何修改 \maybechapter 才能得到這個結果?

答案1

我會在以下幾點修改答案:

\documentclass{book}

\renewcommand*\thechapter{\Roman{chapter}}

\renewcommand*\thesection{%
    \maybechapter{\arabic{chapter}}\arabic{section}%
}

% Maybe print the chapter number
\protected\def\maybechapter#1{%
   % Tests if current chapter is equal to the chapter number of label)
   \ifnum\value{chapter}=0#1
   \else
     \uppercase\expandafter{\romannumeral#1}.%
   \fi
}

% Test document:
\begin{document}

\begingroup
\def\maybechapter#1{}
\tableofcontents
\endgroup

\chapter{One}

\section{S1}\label{S1}

Here's a reference to Section~\ref{S1},
one to Section~\ref{S2}, and one to
Section~\ref{S3}.

\section{S2}\label{S2}

Text

\chapter{Two}

\section{S3}\label{S3}

\end{document}

使用\protected\def確保\maybechapter在寫入操作中永遠不會擴展;由於 TOC 的條目經過寫入操作,一個\protect是不夠的。

對於目錄,我在本地禁用該\maybechapter命令,以便它吞噬其參數。

在此輸入影像描述

相關內容