这是对这个问题的后续回答多个章节中的交叉引用按照这个问题的答案,我使用以下宏:
\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
Ensures\maybechapter
永远不会扩展;因为 TOC 的条目要经过二写操作,一个\protect
是不够的。
对于 TOC,我本地禁用该\maybechapter
命令,以便它吞噬其参数。