Содержание и перекрестные ссылки в нескольких главах

Содержание и перекрестные ссылки в нескольких главах

Это продолжение вопросаПерекрестные ссылки в нескольких главах. Следуя ответу на этот вопрос, я использую следующие макросы:

\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недостаточно.

Для TOC я локально отключаю \maybechapterкоманду, чтобы она поглощала свой аргумент.

введите описание изображения здесь

Связанный контент