
He estado intentando usar \label
y \ref
ni después de \caption
ni \captionof
, así:
Figure \Roman{chapter}.\arabic{figure}: The caption % A manually created caption
\label{fig:the_ref_name} % the ref that sould ref the \Roman{chapter}.\arabic{figure} number
¿Hay alguna manera de usar este par de comandos sin tener que estar después de \caption
o \captionof
, pero de manera que no pierda la capacidad de continuar haciendo referencia a ecuaciones, figuras, tablas y todas las cosas que normalmente haría?
Respuesta1
El \label
comando almacena un valor para la etiqueta según la macro interna \@currentlabel
. Esta macro se configura mediante comandos como \section
y entornos como \begin{figure}
. Sin embargo, también puede configurarlo usted mismo y luego el siguiente \label
comando tomará el valor que haya especificado.
Debido a que \@currentlabel
es una macro interna que contiene el @
símbolo, necesita \makeatletter
y \makeatother
alrededor del código en el que usa la macro. El siguiente MWE asigna un valor a la macro usando \edef
(definición expandida), lo que garantiza que los contadores estén representados por los valores reales.
\documentclass{report}
\begin{document}
\chapter{My chapter}
\setcounter{figure}{8} % set the figure counter to some number for illustration purposes
Figure \Roman{chapter}.\arabic{figure}: The caption % A manually created caption
\makeatletter\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}\makeatother% set value for use by \label
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}
Resultado (después de dos compilaciones para escribir la etiqueta en el archivo auxiliar y volver a leerla):
El código anterior pretende ilustrar el principio de establecer el valor de la etiqueta interna y posteriormente asignarlo a una etiqueta como fig:the_ref_name
. Sin embargo, esto, por supuesto, no es muy práctico. Puede escribir una pequeña macro para generar el título y establecer el valor de la etiqueta simultáneamente:
\documentclass{report}
\makeatletter
\newcommand{\customfigcaption}[1]{%
Figure \Roman{chapter}.\arabic{figure}: #1%
\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}%
}
\makeatother
\begin{document}
\chapter{My chapter}
\setcounter{figure}{8}
\customfigcaption{The caption}
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}
El resultado es el mismo que el anterior.
Una adición final es agregar una sección fantasma que permite hacer referencia a la etiqueta mediante hyperref
:
\documentclass{report}
\usepackage{hyperref}
\makeatletter
\newcommand{\customfigcaption}[1]{%
Figure \Roman{chapter}.\arabic{figure}: #1%
\csname phantomsection\endcsname%
\edef\@currentlabel{\Roman{chapter}.\arabic{figure}}%
}
\makeatother
\begin{document}
\chapter{My chapter}
\setcounter{figure}{5}
\customfigcaption{The caption}
\label{fig:the_ref_name} % assign the label
\section{New section}
As we have seen in Figure \ref{fig:the_ref_name}, figures can have captions.
\end{document}