%20%E5%92%8C%20%5Cmdtheorem%20(mdframed)%20%E7%9A%84%E5%95%8F%E9%A1%8C.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
包含很多 case,因此此範例使用循環來取得 all \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}