%20%D0%B8%20%5Cmdtheorem%20(mdframed).png)
При использовании \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
, аргумент которого повторно используется для получения заголовка. Пример не устанавливает заголовок, если заголовок пустой. Это также можно изменить, чтобы использовать пустой заголовок для 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}