%20%E3%81%A8%20%5Cmdtheorem%20(mdframed)%20%E3%82%92%E4%B8%80%E7%B7%92%E3%81%AB%E4%BD%BF%E7%94%A8%E3%81%99%E3%82%8B%E5%A0%B4%E5%90%88%E3%81%AE%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
多くのケースが含まれているため、例ではループを使用してすべての を取得し\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}