![各章之间的交叉引用](https://linux22.com/image/306005/%E5%90%84%E7%AB%A0%E4%B9%8B%E9%97%B4%E7%9A%84%E4%BA%A4%E5%8F%89%E5%BC%95%E7%94%A8.png)
我更新了章节/节的命令……等等,所以我看不到所有(子)节名称中的章节编号。另外,当我在章节中并且使用 来\ref
引用当前章节的节时,我只会看到节的编号(而不是之前的章节编号)。以下是我\renewcommand
为此使用的:
\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}}
其中两个参数分别是章节的标签和本章中节的标签。因此,使用该命令,我可以得到我想要的引用,但我显然有 2 个超文本链接。有没有办法得到类似的东西,但只有一个指向该节的唯一链接?
这是一个完整的独立示例:
\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}
答案1
我认为这种情况并不经常发生,否则您将从一开始就坚持使用默认的章节编号分层定义,包括章节。为此,定义一个新的\label
宏,将\totallabel
其添加\thechapter.
到现有标签的前面,您可以像往常一样使用来引用它\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}
答案2
这使用了一个“黑客”
标签使用\@currentlabel
评估计数器格式输出宏的当前设置的方法进行存储\the....
。
我临时恢复了它,使用\@currentchapterlabel
,强制它为,\@currentlabel
并添加了一个以 为前缀的自动附加标签chapterfullabel::
,它们的写法类似,都在一个组中,这样就不会影响任何外部宏。
引用是通过 \fullref{labelname} 完成的,它会自动使用标签标记。我不建议\ref
为此类事情重新定义。
\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}
答案3
那这样怎么样,但你仍然有目录的问题
\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}