
Ich habe meinen Befehl für Kapitel/Abschnitt usw. erneuert. Daher sehe ich die Kapitelnummer nicht in allen (Unter-)Abschnittsnamen. Außerdem ging es darum, dass ich, wenn ich in einem Kapitel bin und \ref
auf einen Abschnitt des aktuellen Kapitels verweise, nur die Nummer des Abschnitts sehe (und nicht die Kapitelnummer davor). Hier sind meine, \renewcommand
die ich dafür verwende:
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
Jetzt möchte ich aber auch auf einen Abschnitt aus einem anderen Kapitel verweisen können und in diesem Fall soll die Nummer des Kapitels angezeigt werden. Ich kann keine Möglichkeit finden, das zu tun... mein verzweifelter Versuch, dieses Problem zu beheben, besteht derzeit darin, Folgendes zu verwenden:
\newcommand\fullref[2]{\ref{#1}.\ref{#2}}
wobei die beiden Argumente die Bezeichnung des Kapitels und die Bezeichnung des Abschnitts in diesem Kapitel sind. Mit diesem Befehl habe ich also das gewünschte Aussehen der Referenz, aber ich habe offensichtlich zwei Hypertext-Links. Gibt es eine Möglichkeit, etwas Ähnliches zu haben, aber mit einem eindeutigen Link, der auf den Abschnitt verweist?
Hier ist ein vollständiges eigenständiges Beispiel:
\documentclass[a4paper]{report}
\usepackage{hyperref}
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\newcommand\fullref[2]{\ref{#1}.\ref{#2}}
\begin{document}
\begin{titlepage}
\end{titlepage}
\chapter{Hello}
\label{chapter: Hello}
\section{I'm Brian}
\label{section: Brian}
\section{I'm from the US}
\label{section: US}
A section from this chapter don't need to have the chapter number in it's reference : \ref{section: Brian}.
But a section from an other chapter should have it : \fullref{Chapter: Welcome}{section: Bob}
\chapter{Welcome}
\label{Chapter: Welcome}
\section{I'm Bob}
\label{section: Bob}
\section{I'm from the Canada}
\label{section: Canada}
\end{document}
Antwort1
Ich nehme an, dass dies nicht oft vorkommt, sonst wären Sie von Anfang an bei den standardmäßigen hierarchischen Definitionen der Abschnittsnummern geblieben, einschließlich der Kapitel. Definieren Sie zu diesem Zweck ein neues \label
Makro, \totallabel
das beispielsweise \thechapter.
dem vorhandenen Label vorangestellt wird, auf das Sie wie gewohnt mit folgendem verweisen können \ref
:
\documentclass[a4paper]{report}
\usepackage{hyperref}
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\newcommand\fullref[2]{\ref{#1}.\ref{#2}}
\makeatletter
\newcommand{\totallabel}[1]{% \totallabel{<label>}
\edef\@currentlabel{\thechapter.\@currentlabel}% Prepend \thechapter. to current label
\label{#1}%
}
\makeatother
\begin{document}
\chapter{Hello}
\label{chap:Hello}
\section{I'm Brian}
\label{sec:Brian}
\section{I'm from the US}
\label{sec:US}
A section from this chapter don't need to have the chapter number in it's reference : \ref{sec:Brian}.
But a section from an other chapter should have it: \fullref{chap:Welcome}{sec:Bob} \ref{chap:sec:Bob}
\chapter{Welcome}
\label{chap:Welcome}
\section{I'm Bob}
\label{sec:Bob}\totallabel{chap:sec:Bob}% Insert a \totallabel
\section{I'm from the Canada}
\label{sec:Canada}
\end{document}
Antwort2
Dies verwendet einen "Hack"
Die Speicherung der Bezeichnung erfolgt mit Hilfe des \@currentlabel
Ansatzes, der die aktuellen Einstellungen des \the....
Ausgabemakros für das Zählerformat auswertet.
Ich habe es vorübergehend wiederhergestellt, verwendet \@currentchapterlabel
, es in den Zustand erzwungen \@currentlabel
und ein automatisches zusätzliches Label mit dem Präfix hinzugefügt chapterfullabel::
, das ähnlich geschrieben wird, alles in einer Gruppe, sodass keine äußeren Makros betroffen sind.
Die Referenz wird mit \fullref{labelname} erstellt, wobei automatisch das Label-Tag verwendet wird. Ich empfehle nicht, \ref
für solche Dinge eine Neudefinition vorzunehmen.
\documentclass{book}
\usepackage{xpatch}
\usepackage[hypertexnames=true]{hyperref}
% Save the definitions of \the.... macros first
\let\latexthechapter\thechapter
\let\latexthesection\thesection
\let\latexthesubsection\thesubsection
\let\latexthesubsubsection\thesubsubsection
% Now redefine
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\makeatletter
% Generate a user-defined tag for the full chapter label
\newcommand{\fullreftagname}{chapterfulllabel::}
% Command for restoring \the.... macros
\newcommand{\@@restorechapterformat}{%
\let\thechapter\latexthechapter
\let\thesection\latexthesection
\let\thesubsection\latexthesubsection
\let\thesubsubsection\latexthesubsubsection
}%
\xapptocmd{\refstepcounter}{%
\begingroup%
\@@restorechapterformat% Temporarily use the full format
\protected@xdef\@currentchapterlabel
{\csname p@#1\endcsname\csname the#1\endcsname}%
\endgroup}{\typeout{Great Success}}{\typeout{Miserable fail}}
\AtBeginDocument{% Must be here due to hyperref`s change
\let\LaTeXLabel\label%
\renewcommand{\label}[1]{%
\begingroup
\let\@currentlabel\@currentchapterlabel%
\LaTeXLabel{\fullreftagname#1}% Write another label with \fullreftagname prefix
\endgroup
\LaTeXLabel{#1}% regular label
}%
}
\newcommand{\fullref}[1]{%
\ref{\fullreftagname#1}%
}
\makeatother
\begin{document}
\tableofcontents
\chapter{First}
\section{Introduction} \label{sec::introduction}
\subsection{Background}
\chapter{Other chapter}
\section{Solution of everything} \label{sec::solution_of_everything}
\subsection{The world formula -- at last}
\subsubsection{More down} \label{subsubsec::something}
\chapter{Last chapter}
In \fullref{sec::introduction} we saw... whereas in \fullref{sec::solution_of_everything}, however in
\fullref{subsubsec::something}
\end{document}
Antwort3
Wie wäre es damit, aber Sie haben immer noch das Problem des Inhaltsverzeichnisses
\documentclass[a4paper]{report}
\usepackage{hyperref}
\makeatletter
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\quad}%
{\csname #1@cntformat\endcsname}}
\def\section@cntformat{\arabic{section}\quad}
\def\subsection@cntformat{\arabic{section}.\arabic{subsection}\quad}
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
\makeatother
\begin{document}
\begin{titlepage}
\end{titlepage}
\tableofcontents
\chapter{Hello}
\label{chapter: Hello}
\section{I'm Brian}
\label{section: Brian}
\subsection{I'm Brian}
\section{I'm from the US}
\label{section: US}
A section from this chapter don't need to have the chapter number in it's reference : \ref{section: Brian}.
But a section from an other chapter should have it : \ref{section: Bob}
\chapter{Welcome}
\label{Chapter: Welcome}
\section{I'm Bob}
\label{section: Bob}
\section{I'm from the Canada}
\label{section: Canada}
\end{document}