LaTeX/計數器
外觀
< LaTeX
計數器是 LaTeX 的重要組成部分:它們允許您控制所有內容(節、列表、標題等)的編號機制。為此,每個計數器在 長整數 範圍內儲存一個整數值,即從 到 。 [1]
在 LaTeX 中,建立新的計數器以及在另一個計數器增加時自動重置的計數器(例如,節中的小節)相當容易。使用命令
\newcounter{NameOfTheNewCounter}
|
您將建立一個新計數器,該計數器自動設定為零。如果您希望計數器在每次另一個計數器增加時重置為零,請使用
\newcounter{NameOfTheNewCounter}[NameOfTheOtherCounter]
|
例如,如果您希望在每章中獨立列舉方程式,您可以建立一個類似於“equationschapter”的計數器,該計數器將在每節開頭自動重置。
\newcounter{equationschapter}[section]
\section{First Section}
I present one equation:
\stepcounter{equationschapter} $a=b+c$ (Eq. \arabic{section}.\arabic{equationschapter})
\section{Second Section}
I present more equations:
\stepcounter{equationschapter} $a=c+d$ (Eq. \arabic{section}.\arabic{equationschapter})
\stepcounter{equationschapter} $d=e$ (Eq. \arabic{section}.\arabic{equationschapter})
|
要將另一個計數器新增到現有計數器中,並在增加時導致重置,請使用
\counterwithin*{NameOfTheCounter}{NameOfTheOtherCounter}
|
如果這不起作用,可能是因為您使用的 LaTeX 版本過舊,在這種情況下,以下方法應該可以正常工作
\makeatletter
\@addtoreset{NameOfTheCounter}{NameOfTheOtherCounter}
\makeatother
|
要撤消此效果,可以使用
\counterwithout*{NameOfTheCounter}{NameOfTheOtherCounter}
|
或
\makeatletter
\@removefromreset{NameOfTheCounter}{NameOfTheOtherCounter}
\makeatother
|
要增加計數器,請使用
\stepcounter{NameOfTheNewCounter}
|
或
\refstepcounter{NameOfTheNewCounter} % used for labels and cross referencing
|
或
\addtocounter{NameOfTheNewCounter}{number}
|
這裡的數字也可以是負數。對於自動重置,您需要使用 \stepcounter。
要顯式設定計數器值,請使用
\setcounter{NameOfTheNewCounter}{number}
|
有幾種方法可以訪問計數器。
\theNameOfTheNewCounter將列印與計數器相關的格式化字串(注意計數器實際名稱之前的“the”)。\value{NameOfTheNewCounter}將返回計數器值,該值可供其他計數器使用或用於計算。它不是格式化字串,因此不能在文字中使用。\arabic{NameOfTheNewCounter}將使用阿拉伯數字列印格式化的計數器。
請注意,\arabic{NameOfTheNewCounter} 也可以用作值,但其他命令不行。
奇怪的是,LaTeX 計數器在任何情況下都不用反斜槓引入,即使使用 \the 命令也是如此。plainTeX 等效項 \count 和 \newcount\mycounter 則遵守反斜槓規則。
以下內部 LaTeX 命令將把指定計數器的數值轉換為可列印的字串,並將字串插入文件中
\arabic- 數字從 到 (含)轉換為字串 «-2147483648», «-2147483647», …, «-1», «0», «1», …, «2147483646», «2147483647»。
- 示例:1、2、3、…
\alph- 數字從 1 到 26(含)轉換為字串 «a», «b», …, «z». 其他數字(負數、零、27、28、…, ) 轉換為空字串。
- 示例:a、b、c、…
\Alph- 與
\alph相同,但使用大寫字母。 - 示例:A、B、C、…
\roman- 數字從 1 到 4999(含)轉換為字串 «i»(1)、«ii»(2)、…, «mmmmcmxcix»(4999),其中 «i» — 1、«v» — 5、«x» — 10、«l» — 50、«c» — 100、«d» — 500、«m» — 1000。數字從 5000 到 (含)轉換為字串 «mmmmm»(5000)、«mmmmmi»(5001)、…。其他數字(負數、零)轉換為空字串。
- 示例:i、ii、iii、…
\Roman- 與
\roman相同,但使用大寫字母。 - 示例:I、II、III、…
\fnsymbol- 針對腳註;列印一系列符號。
數字 符號 1 ∗ 2 † 3 ‡ 4 § 5 ¶ 6 ∥ 7 ∗∗ 8 †† 9 ‡‡ 其他數字 空字串
- 示例:∗、†、‡、…
- 部分
- 章節
- 節
- 小節
- 小小節
- 段落
- 子段落
- 頁
- 圖
- 表
- 腳註
- 多行腳註
對於 enumerate 環境
- enumi
- enumii
- enumiii
- enumiv
對於 eqnarray 環境
- 公式
以下是一個在 book 類中使用部分和節,但沒有章節的例子。
\renewcommand{\thesection}{\thepart .\arabic{section}}
\part{My Part}
\section{My Section}
\subsection{My Subsection}
|
參見 列表結構 章節。
這是一個重建類似於 LaTeX 中已經存在的節和子節計數器的例子。
\newcounter{mysection}
\newcounter{mysubsection}[mysection]
\addtocounter{mysection}{2} % set them to some other numbers than 0
\addtocounter{mysubsection}{10} % same
%
\arabic{mysection}.\arabic{mysubsection}
Blah blah
\stepcounter{mysection}
\arabic{mysection}.\arabic{mysubsection}
Blah blah
\stepcounter{mysubsection}
\arabic{mysection}.\arabic{mysubsection}
Blah blah
\addtocounter{mysubsection}{25}
\arabic{mysection}.\arabic{mysubsection}
Blah blah and more blah blah
|
