目次と複数の章の相互参照

目次と複数の章の相互参照

これは質問の続きです複数の章での相互参照この質問への回答に従って、私は次のマクロを使用しています。

\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}

書き込み操作では拡張されないことを保証します。TOCのエントリ\protected\def\maybechapter書き込み操作の場合、1 つだけ\protectでは不十分です。

TOC の場合、\maybechapterコマンドをローカルで無効にして、引数が無視されるようにします。

ここに画像の説明を入力してください

関連情報