
我更新了章節/部分...等的命令,因此我沒有在所有(子)部分名稱中看到章節編號。另外一點是,當我在一個章節中並且用來\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}