Problemas ao usar \nameref (hyperref) e \mdtheorem (mdframed) juntos

Problemas ao usar \nameref (hyperref) e \mdtheorem (mdframed) juntos

Ao usar o \mdtheoremcomando (from mdframed) com \nameref(from hyperref), parece que os rótulos aplicados a um teorema feito usando \mdtheoremnão levarão apropriadamente o título dado ao teorema. Em vez disso, leva o título da seção atual, como no seguinte 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 a pena notar que o problema não existe durante o uso \newmdtheoremenv(o que não é uma correção válida porque preciso do ambiente não numerado fornecido por \mdtheoreme prefiro que o título do teorema seja o título do quadro).

Existe alguma maneira de contornar isso?

Responder1

Você precisa permitirnamerefpara extrair o nome quando ele define thm. Para isso podemos corrigir \thm(usandoxpatch):

insira a descrição da imagem aqui

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

Claro, se você não fornecer um nome como argumento opcional para thm, então \namereffalhará. No entanto, você também pode usar \autorefpara isso.

Responder2

A generalização de Wernerresponder. O exemplo a seguir corrige \mdtheorempara capturar todos os teoremas por este comando. O código de \mdtheoremcontém muitos casos, portanto o exemplo usa um loop para obter all \ifstrempty, cujo argumento é reutilizado para obter o título. O exemplo não define um título, se o título estiver vazio. Isso também pode ser alterado para usar um título vazio 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}

informação relacionada