
這是後續這個問題和這個。基本上,我想避免定理可選參數中的序列 ([...]),如下所示:

為了獲得這個結果,請按照建議這個答案,我使用了套件\patchcmd
的巨集etoolbox
。然而,每次改變我都必須從 切換到
\patchcmd{\thmhead}{(#3)}{#3}{}{}
,
\patchcmd{\thmhead}{#3}{(#3)}{}{}
這對於一本有很多定理的大書來說不是很方便。因此我想知道是否可以使用單一命令。請注意,我使用了\cite{}
和兩種形式\cite[]{}
,這使事情變得更加複雜。
\documentclass[10pt]{article}
\usepackage[]{amsmath, amssymb, amsthm}
\newtheorem{theorem}{Theorem}[section]
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\begin{document}
\section{Pythagoras' theorem}
\noindent Correct versions:
\begin{theorem}[Pythagoras]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[Pythagoras, \cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[Pythagoras, {\cite[p.\ 345]{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\patchcmd{\thmhead}{(#3)}{#3}{}{}
\begin{theorem}[\cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[{\cite[p.\ 21]{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\noindent To be avoided:
\patchcmd{\thmhead}{#3}{(#3)}{}{}
\begin{theorem}[\cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[{\cite[p.\ 21]{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{thebibliography}{HD}
\bibitem[1]{Pythagoras}
Pythagoras' theorem.
\end{thebibliography}
\end{document}
答案1
您可以將參數與正規表示式進行匹配,該正規表示式表示\cite
後跟零次或一次出現[...]
和{...}
。
\documentclass[10pt]{article}
\usepackage[]{amsmath, amssymb, amsthm}
% see https://tex.stackexchange.com/a/17555/4427 for the parameters
\newtheoremstyle{jepinplain}
{\topsep} % ABOVESPACE
{\topsep} % BELOWSPACE
{\itshape} % BODYFONT
{0pt} % INDENT (empty value is the same as 0pt)
{\bfseries} % HEADFONT
{.} % HEADPUNCT
{5pt plus 1pt minus 1pt} % HEADSPACE
{\thmname{#1}\thmnumber{ #2}\thmnote{{\normalfont\ \checkcite{#3}}}} % CUSTOM-HEAD-SPEC
\ExplSyntaxOn
\NewDocumentCommand{\checkcite}{m}
{
\regex_match:nnTF {\A \c{cite}(?:\[[^]]*\])?\{.*\} \Z} { #1 }
{% only \cite
#1
}
{ (#1) }
}
\ExplSyntaxOff
\theoremstyle{jepinplain}
\newtheorem{theorem}{Theorem}[section]
\begin{document}
\section{Pythagoras' theorem}
\noindent Correct versions:
\begin{theorem}
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[Pythagoras]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[Pythagoras, \cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[Pythagoras, {\cite[p.\ 345]{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[\cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[{\cite[p.\ 21]{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{thebibliography}{1}
\bibitem[1]{Pythagoras}
Pythagoras' theorem.
\end{thebibliography}
\end{document}
新的定理樣式更好,除非您想修改標準\thmhead
命令。
答案2
這個問題現在有點老了,但我正在解決同樣的問題,所以我想我會發布我是如何做到的。如果能夠以某種方式標記是否應該添加括號,那就太好了,但我手動解決了這個問題。
解決方案0:
永久保留修改,並添加您自己的括號。
\patchcmd{\thmhead}{(#3)}{#3}{}{}
\begin{theorem}[(Pythagoras)]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[(Pythagoras, \cite{Pythagoras})]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[\cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
當然,這失去了 LaTeX 巨集的部分好處,即您可以在一個地方重新定義外觀並將其應用到任何地方 - 如果您突然決定以其他方式將定理註釋括起來,您將不得不經歷並手動更改它們。所以一個微小的改進是:
解決方案一:
定義一個巨集。
\patchcmd{\thmhead}{(#3)}{#3}{}{}
\newcommand{\notewrap}[1]{(#1)}
\begin{theorem}[\notewrap{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[\notewrap{Pythagoras, \cite{Pythagoras}}]
$a^2 + b^2 = c^2$.
\end{theorem}
\begin{theorem}[\cite{Pythagoras}]
$a^2 + b^2 = c^2$.
\end{theorem}