
Dies ist eine Folgefrage zu der FrageQuerverweise in mehreren Kapiteln. Als Antwort auf diese Frage verwende ich die folgenden Makros:
\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}%
}
Dies funktioniert wie erwartet für Referenzen, im Inhaltsverzeichnis werden den Abschnittsnummern jetzt jedoch immer die Kapitelnummern vorangestellt:
I The first chapter
I.1 The first section of the first chapter
aber ich hätte gerne sowas wie
I The first chapter
1 The first section of the first chapter
Wie muss ich \maybechapter ändern, um dieses Ergebnis zu erhalten?
Antwort1
Ich würde die Antwort in einigen Punkten abändern:
\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}
Durch die Verwendung \protected\def
wird sichergestellt \maybechapter
, dass bei einem Schreibvorgang niemals erweitert wird. Da Einträge für das InhaltsverzeichniszweiSchreibvorgänge, einer \protect
reicht nicht aus.
Für das Inhaltsverzeichnis deaktiviere ich den \maybechapter
Befehl lokal, sodass er sein Argument verschlingt.