Problemas al usar \nameref (hyperref) y \mdtheorem (mdframed) juntos

Problemas al usar \nameref (hyperref) y \mdtheorem (mdframed) juntos

Cuando se usa el \mdtheoremcomando (from mdframed) con \nameref(from hyperref), parece que las etiquetas aplicadas a un teorema creado usando \mdtheoremno tomarán apropiadamente el título dado al teorema. En su lugar, toma el título de la sección actual, como en el siguiente MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{hyperref}
\tikzset{tatregris/.style =
{draw=gray, thick, fill=white, shading = theoremtitle, %
text=gray, rectangle, rounded corners, right,minimum height=.7cm}}
\pgfdeclarehorizontalshading{theorembackground}{100bp}
{color(0bp)=(black!2); color(100bp)=(black!15)}
\makeatletter
\mdfdefinestyle{theoremstyle}{%
outerlinewidth=0em,outerlinecolor=white,%
leftmargin=-1em,rightmargin=-1em,%
middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
apptotikzsetting={\tikzset{mdfbackground/.append style ={%
shading = theorembackground}}},
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipbelow={-1em},
needspace=\baselineskip,
frametitlefont=\sffamily\scshape,
}
\makeatother
\mdtheorem[style=theoremstyle]{thm}{Theorem}[section]
\begin{document}
\section{Section name}
\begin{thm}[Theorem name] \label{theorem name}
This is a theorem.
\end{thm}
\nameref{theorem name} is a theorem.
\end{document}

Vale la pena señalar que el problema no existe cuando se usa \newmdtheoremenv(lo cual no es una solución válida porque necesito el entorno sin numerar proporcionado por \mdtheoremy prefiero que el título del teorema sea el título del marco).

¿Hay alguna forma de evitar esto?

Respuesta1

Necesitas permitirnamerefpara extraer el nombre cuando se establezca thm. Para esto podemos parchear \thm(usandoxpatch):

ingrese la descripción de la imagen aquí

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{hyperref,xpatch}

\tikzset{tatregris/.style =
  {draw=gray, thick, fill=white, shading = theoremtitle, %
  text=gray, rectangle, rounded corners, right,minimum height=.7cm}}
  \pgfdeclarehorizontalshading{theorembackground}{100bp}
  {color(0bp)=(black!2); color(100bp)=(black!15)}

\mdfdefinestyle{theoremstyle}{%
  outerlinewidth=0em,outerlinecolor=white,%
  leftmargin=-1em,rightmargin=-1em,%
  middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
  apptotikzsetting={\tikzset{mdfbackground/.append style ={%
  shading = theorembackground}}},
  innertopmargin=1.2\baselineskip,
  skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
  skipbelow={-1em},
  needspace=\baselineskip,
  frametitlefont=\sffamily\scshape,
}

\mdtheorem[style=theoremstyle]{thm}{Theorem}[section]
\newcommand{\thmautorefname}{Theorem}
\makeatletter
\xpatchcmd{\thm}{\refstepcounter}{\NR@gettitle{#1}\refstepcounter}{}{}
\makeatother

\begin{document}
\section{Section name}

\begin{thm}[Theorem name] \label{theorem_name1}
This is a theorem.
\end{thm}

\nameref{theorem_name1} is a named theorem. \autoref{theorem_name2} is not a named theorem.

\begin{thm}\label{theorem_name2}
This is a theorem.
\end{thm}

\end{document}

Por supuesto, si no proporciona un nombre como argumento opcional thm, \namereffallará. Sin embargo, también puedes utilizarlo \autorefpara esto.

Respuesta2

La generalización de Werner.respuesta. El siguiente ejemplo parchea \mdtheorempara capturar todos los teoremas mediante este comando. El código de \mdtheoremcontiene muchos casos, por lo que el ejemplo utiliza un bucle para obtener todo \ifstrempty, cuyo argumento se reutiliza para obtener el título. El ejemplo no establece un título si el título está vacío. Esto también se puede cambiar para usar un título vacío para nameref.

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{hyperref,xpatch}

\tikzset{tatregris/.style =
  {draw=gray, thick, fill=white, shading = theoremtitle, %
  text=gray, rectangle, rounded corners, right,minimum height=.7cm}}
  \pgfdeclarehorizontalshading{theorembackground}{100bp}
  {color(0bp)=(black!2); color(100bp)=(black!15)}

\mdfdefinestyle{theoremstyle}{%
  outerlinewidth=0em,outerlinecolor=white,%
  leftmargin=-1em,rightmargin=-1em,%
  middlelinewidth=1.2pt,roundcorner=5pt,linecolor=gray,
  apptotikzsetting={\tikzset{mdfbackground/.append style ={%
  shading = theorembackground}}},
  innertopmargin=1.2\baselineskip,
  skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
  skipbelow={-1em},
  needspace=\baselineskip,
  frametitlefont=\sffamily\scshape,
}

\makeatletter
\newcommand*{\mdfNR@gettitle@ifstrempty}[1]{%
  \ifstrempty{#1}{%
    \@firstoftwo
  }{%
    \NR@gettitle{#1}%
    \@secondoftwo
  }%
}
% Variant: Define title string for nameref regardless of its emptyness
% \newcommand*{\mdfNR@gettitle@ifstrempty}[1]{%
%   \NR@gettitle{#1}%
%   \ifstrempty{#1}%
% }
\count@=\@ne
\@whilenum\count@>\z@\do{%
  \expandafter\xpatchcmd\csname mdtheorem code\endcsname{%
    \ifstrempty%
  }{%
    \mdfNR@gettitle@ifstrempty
  }{%
    \advance\count@\@ne
  }{%
    \count@=-\count@
  }%
}
\ifnum\count@>\z@
\else
  \@latex@warning{Could not patch \string\mdtheorem}%
\fi
\makeatother

\mdtheorem[style=theoremstyle]{thm}{Theorem}[section]
\newcommand{\thmautorefname}{Theorem}

\begin{document}
\section{Section name}

\begin{thm}[Theorem name] \label{theorem_name1}
This is a theorem.
\end{thm}

\nameref{theorem_name1} is a named theorem. \autoref{theorem_name2} is not a
named theorem.

\begin{thm}\label{theorem_name2}
This is a theorem.
\end{thm}

\end{document}

información relacionada