
Como posso redefinir o \nameref
comando para ter a legenda referenciada entre aspas?
Aqui está o que eu tentei:
\documentclass{article}
\let\oldnameref\nameref
\renewcommand{\nameref}[1]{"`\oldnameref{#1}"'} % Error: \nameref undefined.
\usepackage[pdfborder={0 0 0}]{hyperref}
\begin{document}
\begin{figure}
\caption{white rectangle}
\label{fig:figure}
\end{figure}
As shown in figure~\ref{fig:figure} \nameref{fig:figure}
\end{document}
Resultado esperado:
Conforme mostrado na figura 1 "retângulo branco"
Responder1
\nameref
é definido via \DeclareRobustCommand
by packages nameref
e hyperref
em \begin{document}
. Portanto, muitas coisas devem ser consideradas:
A redefinição de
\nameref
pode ser feita em\AtBeginDocument
depoispacotehyperref
é carregado.O pacote
letltxmacro
cuida do comando interno, definido por\DeclareRobustCommand
.Macro
\nameref
pode ser definida como um comando robusto por\DeclareRobustCommand
.A forma de estrela
\nameref
para referência sem link também deve ser fornecida.O pacote
babel
com o idioma apropriado define as abreviações"`
e"'
.Também
csquotes
vale a pena dar uma olhada no pacote.
Exemplo completo:
\documentclass{article}
\usepackage[ngerman]{babel}% some language for the shorthand
\usepackage{letltxmacro}
\usepackage[hidelinks]{hyperref}
\makeatletter
\shorthandon{"}
\AtBeginDocument{%
\LetLtxMacro\oldnameref\nameref
\DeclareRobustCommand*{\nameref}{%
\@ifstar{\my@nameref*}{\my@nameref{}}%
}%
\newcommand*{\my@nameref}[2]{%
"`\oldnameref#1{#2}"'%
}%
}
\shorthandoff{"}
\makeatother
\begin{document}
\begin{figure}
\caption{white rectangle}
\label{fig:figure}
\end{figure}
As shown in figure~\ref{fig:figure} \nameref{fig:figure}
Star form: figure~\ref*{fig:figure} \nameref*{fig:figure}
\end{document}
Responder2
Eu preferiria usar um comando diferente e explorar csquotes
:
\documentclass{article}
\usepackage[ngerman,english]{babel}% some language for the shorthand
\usepackage[autostyle=true]{csquotes}
\usepackage{xparse}
\usepackage[colorlinks]{hyperref}
\NewDocumentCommand{\qnameref}{sm}{%
\enquote{%
\IfBooleanTF{#1}{\nameref*{#2}}{\nameref{#2}}%
}%
}
\begin{document}
\begin{figure}
\caption{white rectangle}
\label{fig:figure}
\end{figure}
As shown in figure~\ref{fig:figure} \qnameref{fig:figure}
Star form: figure~\ref*{fig:figure} \qnameref*{fig:figure}
\selectlanguage{ngerman}
Abbildung~\ref{fig:figure} \qnameref{fig:figure}
Abbildung~\ref*{fig:figure} \qnameref*{fig:figure}
\end{document}
Se você usa apenas o inglês, o carregamento babel
é desnecessário. Se você realmente deseja redefinir \nameref
, o código deve se tornar
\usepackage{letltxmacro} % in addition to the previous packages
\AtBeginDocument{\LetLtxMacro{\oldnameref}{\nameref}%
\RenewDocumentCommand{\nameref}{sm}{%
\enquote{%
\IfBooleanTF{#1}{\oldnameref*{#2}}{\oldnameref{#2}}%
}%
}%
}