
Esta é uma continuação da perguntaReferência cruzada em vários capítulos. Seguindo a resposta a esta pergunta, estou usando as seguintes macros:
\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}%
}
Isso funciona conforme o esperado para referências, mas no Índice, os números das seções agora são todos precedidos pelos números dos capítulos:
I The first chapter
I.1 The first section of the first chapter
mas eu gostaria de conseguir algo como
I The first chapter
1 The first section of the first chapter
Como devo modificar \maybechapter para obter esse resultado?
Responder1
Eu modificaria a resposta em alguns pontos:
\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}
O uso \protected\def
garante \maybechapter
que nunca será expandido em uma operação de gravação; já que as inscrições para o TOC passam pordoisoperações de gravação, uma \protect
não é suficiente.
Para o TOC eu desativo localmente o \maybechapter
comando, para que ele engula seu argumento.