\nameref(hyperref)와 \mdtheorem(mdframed)을 함께 사용할 때 발생하는 문제

\nameref(hyperref)와 \mdtheorem(mdframed)을 함께 사용할 때 발생하는 문제

\mdtheorem(from mdframed)과 함께 \nameref(from ) 명령을 사용할 때 hyperref, 를 사용하여 만든 정리에 적용된 레이블이 \mdtheorem해당 정리에 부여된 제목을 적절하게 취하지 못하는 것 같습니다. 대신 다음 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}

주목할 만한 점은 사용할 때 문제가 존재하지 않는다는 것입니다 . (제공된 번호가 없는 환경이 필요 하고 정리 제목이 프레임 제목인 것을 선호하기 \newmdtheoremenv때문에 유효한 수정 사항이 아닙니다 .)\mdtheorem

이 문제를 해결할 수 있는 방법이 있나요?

답변1

당신은 허용해야nameref설정 시 이름을 추출합니다 thm. 이를 위해 우리는 패치를 할 수 있습니다 \thm(다음을 사용하여xpatch):

여기에 이미지 설명을 입력하세요

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

물론 에 대한 선택적 인수로 이름을 제공하지 않으면 실패합니다 thm. \nameref그러나 이를 위해 사용할 수도 있습니다 \autoref.

답변2

베르너의 일반화답변. 다음 예제 패치는 \mdtheorem이 명령으로 모든 정리를 포착합니다. 의 코드에는 \mdtheorem많은 경우가 포함되어 있으므로 예제에서는 루프를 사용하여 \ifstrempty제목을 가져오기 위해 인수가 재사용되는 all 을 가져옵니다. 이 예에서는 제목이 비어 있으면 제목을 설정하지 않습니다. 에 대해 빈 제목을 사용하도록 변경할 수도 있습니다 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}

관련 정보