
Renové mi comando para capítulo/sección... etc. Entonces no veo el número de capítulo en el nombre de todas las (sub)secciones. Además, el punto es que cuando estoy en un capítulo y suelo \ref
hacer referencia a una sección del capítulo actual, solo veo el número de la sección (y no el número del capítulo anterior). Aquí están los \renewcommand
que estoy usando para eso:
\renewcommand{\thechapter}{\Roman{chapter}}
\renewcommand{\thesection}{\arabic{section}}
\renewcommand{\thesubsection}{\thesection.\arabic{subsection}}
\renewcommand{\thesubsubsection}{\thesubsection.\arabic{subsubsection}}
Pero ahora también quiero poder hacer referencia a una sección de otro capítulo y en ese caso quiero que aparezca el número del capítulo. No puedo encontrar una manera de hacer eso... mi intento desesperado de solucionar este problema actualmente es usar eso:
\newcommand\fullref[2]{\ref{#1}.\ref{#2}}
donde los dos argumentos son la etiqueta del capítulo y la etiqueta de la sección de este capítulo. Entonces, con ese comando tengo el aspecto que quiero que tenga la referencia, pero obviamente tengo 2 enlaces de hipertexto. ¿Existe alguna forma de tener algo similar pero con un enlace único que apunte a la sección?
A continuación se muestra un ejemplo independiente completo:
\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}
Respuesta1
Supongo que esto no sucede con frecuencia; de lo contrario, se habría quedado con las definiciones jerárquicas predeterminadas de números de sección, incluidos los capítulos desde el principio. Para ello, defina una nueva \label
macro, digamos \totallabel
que se antepone \thechapter.
a la etiqueta existente a la que puede hacer referencia como de costumbre usando \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}
Respuesta2
Esto usa un 'truco'
La etiqueta se almacena mediante el \@currentlabel
método que evalúa la configuración actual de la \the....
macro de salida del formato del contador.
Lo restauré temporalmente, lo usé \@currentchapterlabel
, lo forcé \@currentlabel
y agregué una etiqueta adicional automática con el prefijo chapterfullabel::
, que está escrita de manera similar, todo en un grupo, de modo que no se efectúen macros externas.
La referencia se realiza con \fullref{labelname} que utiliza automáticamente la etiqueta de etiqueta. No recomiendo redefinir \ref
para tales cosas.
\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}
Respuesta3
¿Qué pasa con esto? Pero aún tienes el problema de la tabla de contenidos.
\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}