Me gustaría utilizar la clase de libro y la ecuación numérica de esa manera:
La ecuación tiene que llevar solo el número de ecuación.sin número de capítulo. Entiendo esto usando
\renewcommand{\theequation}{\arabic{equation}}
Entonces, cuando uso \ref
el comando, me gustaría eso.
- si
\ref
se invoca en el mismo capítulo de la ecuación, la referencia es simplemente(equation)
- si
\ref
se invoca en otro capítulo de la ecuación la referencia a ser(equation)\textsubscript{name-of-the-chapter-containing-equation}
Aquí está mi intento de usarzref
\documentclass{book}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{zref-user}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\makeatother
\numberwithin{equation}{section}
\renewcommand{\theequation}{\arabic{equation}}
\newcommand{\myref}[1]{\ifnum\thechapter=\zref[chapter]{#1} (\zref{#1}) \else (\zref{#1})\textsubscript{something} \fi}
\begin{document}
\chapter{First}
\ifnum\myref[chapter]{eq:1}=1 a \else b \fi
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\section{First First}
\begin{equation}\zlabel{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\zlabel{eq:2}
0=0
\end{equation}
\chapter{Second}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}
Tengo dos problemas en la definición de\myref
\ifnum
no funciona correctamente porque tal vez\zref[chapter]{#1}
no devuelve un número y por eso es imposible compararlo con `\thechapter'Tengo que encontrar un comando para obtener el nombre del capítulo correspondiente a un número (o quizás usar
\nameref
)
¿Cómo puedo solucionarlos?
Respuesta1
Puedes hacer esto de la siguiente manera. Primero use \zrefused
y \zref@extract
para obtener el valor a comparar para activar el formato adicional.
Ahora el paquete zref-titleref
le da una idea de cómo obtener el campo de título, pero actualiza no solo en los capítulos sino también en las secciones, etc. Entonces, en lugar de eso, escribimos una versión modificada del zref-titleref
código, que solo toma los títulos de los capítulos.
\documentclass{book}
\usepackage{amsmath}
\usepackage{zref-user,gettitlestring}
\usepackage{hyperref}
\newcommand{\setcurrentchaptername}[1]{\GetTitleStringNonExpand{#1}\edef\currentchaptername{\detokenize\expandafter{\GetTitleStringResult}}}
\def\currentchaptername{}
\makeatletter
\zref@newprop{chapter}{\thechapter}
\zref@addprop{main}{chapter}
\zref@newprop{chaptername}{\currentchaptername}
\zref@addprop{main}{chaptername}
\ZREF@patch{@chapter}{\def\@chapter[#1]{\setcurrentchaptername{#1}%
\ZREF@org@@chapter[{#1}]}}
\makeatother
\numberwithin{equation}{chapter}
\renewcommand{\theequation}{\arabic{equation}}
\makeatletter
\newcommand{\myref}[1]{\zrefused{#1}%
\ifnum\zref@extract{#1}{chapter}=\thechapter\relax
(\zref{#1}) \else (\zref{#1})\textsubscript{\zref@extract{#1}{chaptername}}\fi}
\makeatother
\begin{document}
\chapter{First}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\section{First First}
\begin{equation}\zlabel{eq:1}
E=\gamma m
\end{equation}
\section{First Second}
\begin{equation}\zlabel{eq:2}
0=0
\end{equation}
\chapter{Second}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:3}
e^{i\pi}+1=0
\end{equation}
\chapter{Third}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\begin{equation}\zlabel{eq:4}
f(w) = \frac{1}{2i\pi}\oint_{C_w}\frac{f(z)\mathrm{d}z}{z-w}
\end{equation}
\chapter{Last}
\myref{eq:1}, \myref{eq:2}, \myref{eq:3}, \myref{eq:4}
\end{document}
Como en su código, \zref@newprop
se utiliza para configurar nuevas propiedades para realizar un seguimiento. Estos deben tener una forma de obtener los datos correctos en el momento en que \zlabel
se llama al comando. Para el número de capítulo, esto está en \thechapter
, que ya usó. Para el título del capítulo, lo almacenamos \currentchaptername
y modificamos el \chapter
comando para que se establezca este nombre. Si desea utilizar un nombre diferente (por ejemplo, más corto), puede redefinirlo \currentchaptername
inmediatamente después del \chapter
comando. Ahora, en el comando de referencia \myref
, obtenemos los datos de la etiqueta emitiendo primero \zrefused
y luego \zref@extract
obteniendo un campo en particular.
He cambiado la numeración relativa de ecuaciones para que esté dentro de chapter
s en lugar de section
s; de lo contrario, obtendrás etiquetas duplicadas. Si realmente desea restablecer cada sección, entonces también debería hacer referencia al título de la sección correspondiente y probablemente podría usarlo zref-titleref
directamente.
El código anterior solo toma títulos de capítulos numerados. Puede aplicar un parche similar a \chapter*
través de @schapter
si lo desea o, alternativamente, configurarlo \currentchatpername
usted mismo en tales circunstancias.