\nameref com aspas

\nameref com aspas

Como posso redefinir o \namerefcomando 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 \DeclareRobustCommandby packages namerefe hyperrefem \begin{document}. Portanto, muitas coisas devem ser consideradas:

  • A redefinição de \namerefpode ser feita em\AtBeginDocument depoispacote hyperrefé carregado.

  • O pacote letltxmacrocuida do comando interno, definido por \DeclareRobustCommand.

  • Macro \namerefpode ser definida como um comando robusto por \DeclareRobustCommand.

  • A forma de estrela \namerefpara referência sem link também deve ser fornecida.

  • O pacote babelcom o idioma apropriado define as abreviações "`e "'.

    Também csquotesvale 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}

insira a descrição da imagem aqui

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}}%
    }%
  }%
}

informação relacionada