这是这个问题和这个。基本上,我想避免定理的可选参数中的序列 ([...]),如下所示:
为了获得这个结果,正如建议的那样这个答案,我使用了包\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 宏的部分优势,即您可以在一个地方重新定义外观并将其应用于任何地方——如果您突然决定以其他方式括住定理注释,则必须仔细检查并手动更改它们。因此,一个微小的改进是:
解决方案 1:
定义一个宏。
\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}