定理環境的宏

定理環境的宏

打字很痛苦

\begin{theorem}
...
\end{theorem}

\begin{proof}
...
\end{proof}

在課堂上即時發送文件時。

當我宣布

\newcommmand{\theorem}[2]{\begin{theorem} {#1} \begin{proof} {#2} \end{proof} \end{theorem}}

我知道這\theorem已經被定義了。

如果我嘗試

\renewcommmand{\theorem}[2]{\begin{theorem} {#1} \begin{proof} {#2} \end{proof} \end{theorem}}

我收到一個致命錯誤。

有什麼辦法可以讓自己不必多次輸入開頭、結尾和證明這些字嗎?當我在課堂上即時發送簡訊筆記時,這一點很重要。

答案1

你可以這樣做

\newcommand{\thmpr}[2]{%
  \begin{theorem}#1\end{theorem}%
  \begin{proof}#2\end{proof}%
}

但我警告你,這更糟:你需要追蹤可能最終彼此相距很遠的牙套。

如果您正在做課堂筆記,那麼標記會更容易:

THEOREM\\
Whatever the guy at the blackboard is saying

PROOF\\
Something even more mysterious that I'll go through later

當您修改材料時,可以輕鬆放置所需內容\begin並添加標籤。\end


問題是確實為其內部目的\newtheorem{theorem}{Theorem}定義了巨集。\theorem所以 LaTeX 拒絕這樣做\newcommand{\theorem}[2]{...};但與

\renewcommand{\theorem}[2]{\begin{theorem}...}

您正在\theorem根據自身進行定義,這將導致無限循環。

答案2

您的定義中的問題是\begin{theorem}隱式呼叫\theorem.這就是為什麼在您的第一次嘗試中您會收到“已定義”錯誤。 (最終的環境實際上就是那個巨集。)在第二次嘗試中,您在要重新定義的巨集中呼叫巨集。因此你最終陷入了無限循環。試著\theorem透過說來儲存一個版本

\let\oldtheorem\theorem

然後你的第二次嘗試基本上應該會有效:

\documentclass{article}
\usepackage{amsthm}

\newtheorem{theorem}{Theorem}
\let\oldtheorem\theorem
\let\endoldtheorem\endtheorem
\renewcommand\theorem[2]{\begin{oldtheorem}#1\end{oldtheorem}\begin{proof}#2\end{proof}}

\begin{document}
\theorem{a theorem}{with proof}
\end{document}

另請注意,proof環境通常不會嵌套在theorem和參數周圍的分組中,即“ {#1}”是不必要的。

相關內容