
我想定義一個定理環境,在其中我可以手動發出編號,而不是跟隨一些內部計數器。環顧四周我發現這回答。他們在那裡提出以下程式碼作為解決方案:
\newtheorem{innercustomthm}{Theorem}
\newenvironment{customthm}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}
程式碼以我想要的方式運作,但是我試圖理解程式碼中的所有內容,以防我想稍微調整它(並且還要了解我在文件中放入的內容)並且有點卡住。
首先,\newenvironment 具有以下結構:
\newenvironment{<env-name>}[<n-args>][<default>]{<begin-code>}{<end-code>}
我知道第三個參數留空;沒問題。然後是 \renewcommand,其結構為:
\renewcommand{<cmd>}[<n-args>][<default>]{<text>}
在這裡,我很難理解正在發生的事情,因為沒有使用大括號;我想也許該命令允許您省略它們,但我沒有找到任何有關此的資訊。之後它變得更加混亂,因為命令 \theinnercustomthm、\innercustomthm 和 \endinnercustomthm 似乎是為這個特定的解決方案定制的,而且我不明白它們是如何工作的。
例如,我注意到的一件事是,更改環境名稱(例如 myeo 而不是 customthm)會破壞程式碼。
如果有人可以解釋正在發生的事情或向我指出一些手冊或網頁,以便我能夠理解正在發生的事情,我將非常感激。
答案1
首先要注意的是
\newtheorem{innercustomthm}{Theorem}
內部做了類似的事情
\newenvironment{innercustomthm}[1][]{<begin>}{<end>}
其中<begin>
和<end>
程式碼在這裡不是問題。請注意,環境會尋找可選參數(定理註釋或歸因)。它還設定了一個與環境同名的計數器。
故意選擇的名稱又長又不吸引人:這樣的名稱不太可能與現有或想像的環境發生衝突。
對於我們想到的應用程序,我們實際上並不需要計數器,而是事實上,任何呼叫innercustomthm
都會步進計數器並設定所需的內容,以便下一個\label
命令將使用計數器的值。然而,LaTeX 並不查看值本身,而是使用 的當前含義\the<counter>
,在本例中為\theinnercustomthm
。的意義\theinnercustomthm
也用於對環境進行編號。
我們的想法是手動對定理進行編號,因為我們需要引用與其他出版物上的編號相同的定理,因此自動編號是不可能的。好吧,我們建立一個包裝器,innercustomthm
它將採用我們希望語句具有的數字作為參數。
因此,我們的想法是啟動一個帶有參數的環境,該參數將用於(本地)重新定義\theinnercustomthm
並調用「內部」環境,該環境將完成排版語句的工作。
的定義customthm
可能是
\newenvironment{customthm}[1]
{\renewcommand{\theinnercustomthm}{#1}\begin{innercustomthm}}
{\end{innercustomthm}}
但這有一個小缺點:如果你忘記了,你最終會得到「缺少」提及而不是「提及」\end{customthm}
的錯誤。所以,\end
innercustomthm
customthm
男子氣概的程式設計師在這種情況下,使用「內部」環境的內部版本\begin
和\end
例程:簿記已經完成\begin{customthm}
,我們不需要重複。所以我們得到
\newenvironment{customthm}[1]
{\renewcommand{\theinnercustomthm}{#1}\innercustomthm}
{\endinnercustomthm}
好吧,你引用的實際程式碼是
\newenvironment{customthm}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}
為什麼缺少大括號?另一個(壞)習慣男子氣概的程式設計師誰知道這些牙套是不必要的,無論如何都會被移除。
如有疑問,請使用它們。
答案2
首先,我們應該問一下\newvironment{myenv}{..start code...}{...end code...}
實際的作用是什麼。稍微簡化一下,\newenvironment
定義了兩個命令\myenv
和\endmyenv
,其中\myenv
擴展為...start code...
和\endmyenv
擴展為...end code...
。當您編寫\begin{myenv}...\end{myenv}
LaTeX 時,會用(幾乎)等同於{\myenv ... \endmyend}
. (幕後還發生了更多事情。)
接下來,該行將環境\newtheorem{innercustomthm}{Theorem}
定義innercustomthm
為使用計數器的“定理”環境innercustomthm
。特別是,該環境的「定理數」是使用 列印的\theinnercustomthm
。
現在讓我們考慮一下:
\newenvironment{customthm}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}
這定義了customthm
環境,它需要一個參數#1
。透過上面的第一段,\begin{customthm}[X]...\end{customthm}
擴展為:
{\renewcommand\theinnercustomthm{X}\innercustomthm ... \endinnercustomthm}
也就是說,\theinnercustomthm
被重新定義為等於X
之後innercustomthm
再應用環境。因此,customthm
環境本質上與環境相同,innercustomthm
只是\theinnercustomthm
先設定為等於X
,其中X
是環境的參數customthm
。所以最終的效果是這個環境印刷:「Theorem X」......
最後你說更改環境名稱(例如,mytheo
而不是customthm
)會破壞程式碼。如果我說的是正確的,那麼這不可能是真的!事實上,如以下程式碼所示,如果我們更改環境名稱,程式碼的工作方式完全相同:
\documentclass{article}
\newtheorem{innercustomthm}{Theorem}
\newenvironment{mytheo}[1]
{\renewcommand\theinnercustomthm{#1}\innercustomthm}
{\endinnercustomthm}
\begin{document}
\begin{innercustomthm}Hi
\end{innercustomthm}
\begin{mytheo}Hi
\end{mytheo}
\end{document}