
Я обновил свою команду для главы/раздела... и т. д. Поэтому я не вижу номер главы во всех названиях (под)разделов. Также дело в том, что когда я нахожусь в главе и использую \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}, который автоматически использует тег label. Я не рекомендую переопределять \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}