> For the complete documentation index, see [llms.txt](https://overleaf-pro.ayaka.space/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://overleaf-pro.ayaka.space/latex/ja/shu-shi-she-ding/10-counters.md).

# カウンタ

## LaTeXカウンタ入門

LaTeXベースの組版は、特殊な形のプログラミングです。つまり、組版言語（LaTeXコマンド）を使って「指示」を与え、その結果として出力、すなわちPDFファイル形式のコンパイル済みLaTeX文書を生成します。どのようなプログラミングでもそうですが、LaTeXには、文書（コード）内で後で再利用するために整数値を一時的に保存しておく必要があることが多いでしょう。たとえば、次のようなことをしたいかもしれません：

* 文書内でその保存された整数を組版する
* 文書内の特定要素に自動番号を付ける
* 条件分岐のコード（またはループ）を制御する変数として使う
* 一定の値を加算／減算して値を変更・更新する
* 完全に別の値にリセットする

このような整数処理は、JavaScript、C、Lua、あるいはお好きな任意の言語のような「通常の」プログラミング言語では非常に一般的です。通常は（整数を保持する）変数を宣言（名前を付け）し、それに値を代入します。LaTeXの組版言語も同様の機能を提供しますが、その実装方法はかなり異なります。以下で見ていきましょう。

要するに、LaTeXカウンタとは、上記の作業やその他さまざまな用途に使える整数値を保存するための「LaTeX変数」の名前です。LaTeX自体も、ページ、節、表、図、箇条書き／番号付きリストなどの番号付けを行う多数の内部カウンタを使っています。この記事では、それらのカウンタにアクセスして変更する方法と、新しいカウンタを作成して使う方法を説明します。

## カウンタを扱うためのLaTeXコマンド

以下の各節に説明へのリンクを付けて、LaTeXのカウンタ用コマンドを一覧にします。

カウンタの作成と増加：

* [`\newcounter{somecounter}[anothercounter]`](#newcountersomecounteranothercounter)
* [`\setcounter{somecounter}{number}`](#setcountersomecounternumber)
* [`\addtocounter{somecounter}{number}`](#addtocountersomecounternumber)
* [`\stepcounter{somecounter}`](#stepcountersomecounter)
* [`\refstepcounter{somecounter}`](#refstepcountersomecounter)

カウンタ値へのアクセスと出力：

* [`\arabic{somecounter}`](#arabicsomecounter)
* [`\roman{somecounter}`](#romansomecounter)
* [`\Roman{somecounter}`](#romanoutput)
* [`\alph{somecounter}`](#alphsomecounter)
* [`\Alph{somecounter}`](#alphoutput)
* [`\fnsymbol{somecounter}`](#fnsymbolsomecounter)
* [`\value{somecounter}`](#valuesomecounter)

その他のカウンタ用コマンド：

* [`\counterwithin{somecounter}{anothercounter}`](#counterwithinsomecounteranothercounter)
* [`\counterwithout{somecounter}{anothercounter}`](#counterwithoutsomecounteranothercounter)

### カウンタの作成と増加

#### \newcounter{somecounter}\[anothercounter]

新しいカウンタを定義するために使います。第2引数は、 `[]`で囲まれており、任意ですが、使用すると `*somecounter*` を、 `*anothercounter*` がステップされたときにリセットされる新しいカウンタとして定義します。 `\newcounter` は、より単純な形 `\newcounter{*somecounter*}` を使うことがよくあります。これにより `*somecounter*` が作成され、0で初期化されます。

* **注**： `\newcounter` コマンドによって作成された各カウンタについて、それは *また* カウンタの値を組版するために使えるコマンドを定義します。たとえば、 `\newcounter{*mycounter*}` というコマンドを定義します。 `\the*mycounter*`。以下のLaTeXコード断片に例を示します。提供されたリンクを使って、Overleafで完全な文書として開くことができます：

```latex
\newcounter{mycounter}
\setcounter{mycounter}{42}
これで \verb|\themycounter| と書けば \themycounter を得られます。
```

[このLaTeX断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bmycounter%7D%0A%5Csetcounter%7Bmycounter%7D%7B42%7D%0AYou+can+now+write+%5Cverb%7C%5Cthemycounter%7C+to+obtain+%5Cthemycounter.%0A%5Cend%7Bdocument%7D)

この例の出力は次のとおりです： $$\text{You can now write }\verb|\themycounter|\text{ to obtain 42.}$$

* **注**：もし `*mycounter*` がすでに定義されていると、LaTeXはエラー `c@mycounter already defined`を出します。これは、Overleafで開ける次の例で示されています。

```latex
\newcounter{mycounter}
…たくさんのコード… でもうっかり \verb|\newcounter{mycounter}| をもう一度書いてしまう
\newcounter{mycounter}
```

[この ***エラーを発生させるコード*** Overleaf で](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bmycounter%7D%0A...lots+of+code...+but+we+forget+and+write+%5Cverb%7C%5Cnewcounter%7Bmycounter%7D%7C+again%0A%5Cnewcounter%7Bmycounter%7D%0A%5Cend%7Bdocument%7D)

#### \setcounter{somecounter}{number}

設定します `*somecounter*` その値を保持するように `*number*`.

* **注**: `*number*` 正の値にも負の値にもできます。

以下のLaTeXコード断片に例を示します。提供されたリンクを使って、Overleafで完全な文書として開くことができます：

```latex
\noindent 新しいカウンタ \texttt{myvar} を作成し、値 \texttt{42} を代入します。
\vspace{10pt}

\newcounter{myvar}
\setcounter{myvar}{42}

\noindent \verb|\themymar| と書くと \texttt{\themyvar} が組版されます。
\vspace{10pt}

\noindent 次に、\verb|\setcounter{myvar}{-42}| と書いて、\texttt{myvar} を \texttt{-42} に変更します。
\setcounter{myvar}{-42}。これで、\verb|\themyvar| と書くと \texttt{\themyvar} が出力されます。
```

[このLaTeX断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+newcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Create+a+new+counter+%5Ctexttt%7Bmyvar%7D+and+assign+it+the+value+%5Ctexttt%7B42%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnewcounter%7Bmyvar%7D%0A%5Csetcounter%7Bmyvar%7D%7B42%7D+%0A%0A%5Cnoindent+Writing+%5Cverb%7C%5Cthemymar%7C+typesets+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+Next%2C+we%27ll+change+%5Ctexttt%7Bmyvar%7D+to+%5Ctexttt%7B-42%7D%2C+writing+%5Cverb%7C%5Csetcounter%7Bmyvar%7D%7B-42%7D%7C%0A%5Csetcounter%7Bmyvar%7D%7B-42%7D.+Now%2C+writing+%5Cverb%7C%5Cthemyvar%7C+outputs+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cend%7Bdocument%7D)

#### \addtocounter{somecounter}{number}

カウンタを増やします `*somecounter*` を `*number*`.

* **注**: `*number*` で増やします。正の値ならカウンタ値を増加させ、負の値なら減少させます。たとえば：

```latex
\addtocounter{somecounter}{-1} % somecounter を 1 減らします
```

以下のLaTeXコード断片に例を示します。提供されたリンクを使って、Overleafで完全な文書として開くことができます：

```latex
\noindent 新しいカウンタ \texttt{myvar} を作成し、値 \texttt{42} を代入します。
\vspace{10pt}

\newcounter{myvar}
\setcounter{myvar}{42}

\noindent \verb|\themymar| と書くと \texttt{\themyvar} が組版されます。
\vspace{10pt}

\noindent 次に、\verb|\addtocounter{myvar}{100}|\addtocounter{myvar}{100} と書いて、\texttt{myvar} を \texttt{142} に変更します。これで、\verb|\themyvar| と書くと \texttt{\themyvar} が出力されます。
```

[このLaTeX断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+addtocounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Create+a+new+counter+%5Ctexttt%7Bmyvar%7D+and+assign+it+the+value+%5Ctexttt%7B42%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnewcounter%7Bmyvar%7D%0A%5Csetcounter%7Bmyvar%7D%7B42%7D+%0A%0A%5Cnoindent+Writing+%5Cverb%7C%5Cthemymar%7C+typesets+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+Next%2C+we%E2%80%99ll+change+%5Ctexttt%7Bmyvar%7D+to+%5Ctexttt%7B142%7D+by+writing+%5Cverb%7C%5Caddtocounter%7Bmyvar%7D%7B100%7D%7C%5Caddtocounter%7Bmyvar%7D%7B100%7D.+Now%2C+writing+%5Cverb%7C%5Cthemyvar%7C+outputs+%5Ctexttt%7B%5Cthemyvar%7D.%0A%5Cend%7Bdocument%7D)

#### \stepcounter{somecounter}

増やします `*somecounter*` を 1 だけ。以下のLaTeXコード断片に例を示します。提供されたリンクを使って、Overleafで完全な文書として開くことができます：

```latex
\verb|\newcounter{mycounter}| と書いて新しいカウンタを作成すると
\newcounter{mycounter} と書くと、カウンタ \texttt{mycounter} が作成され
初期値0に設定されます。\verb|\themycounter| と書けば確認できます：
\texttt{mycounter} は現在、次の値を保持しています：\themycounter。

もしここで \verb|\stepcounter{mycounter}|\stepcounter{mycounter} と書くと
\verb|\themycounter| には現在、値 \themycounter が入っています。
```

[このLaTeX断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+stepcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIf+we+create+a+new+counter+by+writing+%5Cverb%7C%5Cnewcounter%7Bmycounter%7D%7C%0A%5Cnewcounter%7Bmycounter%7D+then+the+counter+%5Ctexttt%7Bmycounter%7D+is+created+and+%0Ainitialized+to+zero%2C+as+you+can+see+if+you+write+%5Cverb%7C%5Cthemycounter%7C%3A%0A%5Ctexttt%7Bmycounter%7D+currently+stores%3A+%5Cthemycounter.+%0A%0AIf+we+now+write+%5Cverb%7C%5Cstepcounter%7Bmycounter%7D%7C%5Cstepcounter%7Bmycounter%7D+then+%0A%5Cverb%7C%5Cthemycounter%7C+now+contains+the+value+%5Cthemycounter.%0A%5Cend%7Bdocument%7D)

#### \refstepcounter{somecounter}

を増やし `*somecounter*` を 1 だけ増やし、参照機構から見えるようにします。また、 `\label` の後で使えるように値も設定します。

**\refstepcounter を使う例**

以下の例では `\refstepcounter` を使って `example` 環境用の新しいカウンタを作成し、 `\label` や `\ref` を使って参照の例を示します `example` カウンタ変数。

```latex
\documentclass{article}

\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \noindent\textbf{Example~\theexample. #1} \rmfamily}{\medskip}

\begin{document}
\section{3つの例}
\begin{example}
この最初の例ではラベルを作成します。\verb|\label{ex:1}|\label{ex:1}。これは最初の例です。\texttt{example} カウンタは、新しい各 \verb|\section| の開始時にリセットされます。
\end{example}

\begin{example}
そして、もう1つ番号付きの例があります。後でこれを参照するために、2つ目の \verb|\label{ex:2}|\label{ex:2} を作成します。例 \ref{ex:1} では…何かを読んでいます。
\end{example}

\begin{example}
そして、もう1つ番号付きの例があります。\verb|\theexample| を使って、\texttt{example} カウンタに現在割り当てられている番号を組版します：\theexample です。
\end{example}

\section{別の節}
ちょうど新しい節を始めたところなので、\texttt{example} カウンタは \theexample に設定されています。
前の節の例を参照します（例 \ref{ex:1} と \ref{ex:2}）。これはただテキストを入れるだけの、まったく目的のないダミー節です。この節の \texttt{section} カウンタは、\verb|\thesection| を使って組版できます：現在は \thesection の値が割り当てられています。

\begin{example}
これはこの節の最初の例です。\texttt{example} カウンタがステップされ、\theexample に設定されました。
\end{example}
\end{document}
```

[この `\refstepcounter` Overleaf 上の例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=An+example+using+refstepcounter\&snip=%5Cdocumentclass%7Barticle%7D%0A%0A%5Cnewcounter%7Bexample%7D%5Bsection%5D%0A%5Cnewenvironment%7Bexample%7D%5B1%5D%5B%5D%7B%5Crefstepcounter%7Bexample%7D%5Cpar%5Cmedskip%0A+++%5Cnoindent%5Ctextbf%7BExample%7E%5Ctheexample.+%231%7D+%5Crmfamily%7D%7B%5Cmedskip%7D%0A%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BThree+examples%7D%0A%5Cbegin%7Bexample%7D%0ACreate+a+label+in+this+first+example+%5Cverb%7C%5Clabel%7Bex%3A1%7D%7C%5Clabel%7Bex%3A1%7D.+This+is+the+first+example.+The+%5Ctexttt%7Bexample%7D+counter+will+be+reset+at+the+start+of+each+new+each+document+%5Cverb%7C%5Csection%7C.%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7Bexample%7D%0AAnd+here%27s+another+numbered+example.+Create+a+second+%5Cverb%7C%5Clabel%7Bex%3A2%7D%7C%5Clabel%7Bex%3A2%7D+to+later+reference+this+one.+In+Example+%5Cref%7Bex%3A1%7D+we+read...+something.+%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7Bexample%7D%0AAnd+here%27s+another+numbered+example%3A+use+%5Cverb%7C%5Ctheexample%7C+to+typeset+the+number+currently+assigned+to+the+%5Ctexttt%7Bexample%7D+counter%3A+it+is++%5Ctheexample.%0A%5Cend%7Bexample%7D%0A%0A%5Csection%7BAnother+section%7D%0AWe%27ve+just+started+a+new+section+meaning+that+the++%5Ctexttt%7Bexample%7D+counter+has+been+set+to+%5Ctheexample.++%0AWe%27ll+reference+examples+from+the+previous+section+%28Examples+%5Cref%7Bex%3A1%7D+and+%5Cref%7Bex%3A2%7D%29.++This+is+a+dummy+section+with+no+purpose+whatsoever+but+to+contain+text.+The+%5Ctexttt%7Bsection%7D+counter+for+this+section+can+be+typeset+using+%5Cverb%7C%5Cthesection%7C%3A+it+is++currently+assigned+the+value+of+%5Cthesection.%0A%0A%5Cbegin%7Bexample%7D%0AThis+is+the+first+example+in+this+section%3A+the+%5Ctexttt%7Bexample%7D+counter+has+been+stepped+and+now+set+to+%5Ctheexample.+%0A%5Cend%7Bexample%7D%0A%5Cend%7Bdocument%7D)

**コードの概要**

この例では、新しい環境 `example` が定義されており、この環境にはカウント専用のコマンドが3つあります：

* `\newcounter{example}[section]`：新しいカウンタを作成します。名前は `example` で、 `section` カウンタが増加するたびに0にリセットされます。 `section` の代わりに別の任意のカウンタを入れてもよく、カウンタを自動リセットしたくない場合はこの引数を省略できます。
* `を使って 1 ずつ増加します；`： `example` カウンタを1だけ増やし、参照機構から見えるようにします。これにより、 `\label` の後で使えるように値も設定します。
* `\theexample`：カウンタの現在値を出力します `example`.

ユーザー定義環境についてさらに詳しくは [新しい環境を定義する記事](/latex/ja/komando/02-environments.md#defining-a-new-environment)

### カウンタ値へのアクセスと出力

以下の説明は、ユーザーが読み込んだパッケージによるカスタマイズが適用される前の、LaTeXのデフォルトに基づいています。

#### \arabic{somecounter}

の表現を出力する `*somecounter*` をアラビア数字で：1, 2, 3...

#### \roman{somecounter}

の表現を出力する `*somecounter*` を小文字のローマ数字で：i, ii, iii...

* **注**：もし `*somecounter*` $$\leq$$ `0` は出力されません。さらに、 *極めて* 大きな整数値を小文字のローマ数字に変換するのは完了までかなり時間がかかることがあり、その結果として長い列の `m` 文字（数を表す `1000`).
* **ヒント**：TeXがアラビア数字をローマ数字に変換するために使うアルゴリズムに興味があるなら、こちらの記事で読むことができます： [ローマ数字のアルゴリズム](https://www.hanshq.net/roman-numerals.html#tex).

#### \Roman{somecounter}

の表現を出力する `*somecounter*` を大文字のローマ数字で：I, II, III...

* **注**：もし `*somecounter*` $$\leq$$ `0` は出力されません。さらに、 *極めて* 大きな整数値を大文字のローマ数字に変換するのは完了までかなり時間がかかることがあり、その結果として長い列の `M` 文字（数を表す `1000`).
* **ヒント**：TeXがアラビア数字をローマ数字に変換するために使うアルゴリズムに興味があるなら、こちらの記事で読むことができます： [ローマ数字のアルゴリズム](https://www.hanshq.net/roman-numerals.html#tex).

#### \alph{somecounter}

の表現を出力する `*somecounter*` を小文字のアルファベットで：a=1, b=2, c=3...

* **注**: `*somecounter*` は範囲内でなければなりません `1` $$\leq$$ `*somecounter*` $$\leq$$ `26`、そうでないとエラー `! LaTeXエラー：カウンタが大きすぎます。`

#### \Alph{somecounter}

の表現を出力する `*somecounter*` を大文字のアルファベットで：A=1, B=2, C=3...

* **注**: `*somecounter*` は範囲内でなければなりません `1` $$\leq$$ `*somecounter*` $$\leq$$ `26`、そうでないとエラー `! LaTeXエラー：カウンタが大きすぎます。`

#### \fnsymbol{somecounter}

の表現を出力する `*somecounter*` を脚注記号として：1=∗、2=†...

* **注**: `*somecounter*` は範囲内でなければなりません `1` $$\leq$$ `*somecounter*` $$\leq$$ `9`、そうでないとエラー `! LaTeXエラー：カウンタが大きすぎます。`

|                    |                         |
| ------------------ | ----------------------- |
| の値 `*somecounter*` | によって組版される文字 `\fnsymbol` |
| 1                  | \*                      |
| 2                  | †                       |
| 3                  | ‡                       |
| 4                  | §                       |
| 5                  | ¶                       |
| 6                  | ∥                       |
| 7                  | ∗∗                      |
| 8                  | ††                      |
| 9                  | ‡‡                      |

#### カウンタ値を出力する例

以下の例では次のコマンドの使用を示します `\arabic{*somecounter*}`, `\roman{*somecounter*}`, `\Roman{*somecounter*}`, `\alph{*somecounter*}`, `\Alph{*somecounter*}`、そして `\fnsymbol{*somecounter*}`.

```latex
\newcounter{somecounter}
\setcounter{somecounter}{9}
\begin{itemize}
    \item \verb|\arabic{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \arabic{somecounter} として組版します
    \item \verb|\roman{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \roman{somecounter} として組版します
    \item \verb|\Roman{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \Roman{somecounter} として組版します
    \item \verb|\alph{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \alph{somecounter} として組版します
   \item \verb|\Alph{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \Alph{somecounter} として組版します
    \item \verb|\fnsymbol{somecounter}| は \texttt{somecounter} の値 \thesomecounter{} を \fnsymbol{somecounter} として組版します
\end{itemize}
```

[このLaTeX断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Converting+counter+values\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcounter%7Bsomecounter%7D%0A%5Csetcounter%7Bsomecounter%7D%7B9%7D%0A%5Cbegin%7Bitemize%7D%0A++++%5Citem+%5Cverb%7C%5Carabic%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as+%5Carabic%7Bsomecounter%7D+%0A++++%5Citem+%5Cverb%7C%5Croman%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Croman%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5CRoman%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5CRoman%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5Calph%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Calph%7Bsomecounter%7D%0A+++%5Citem+%5Cverb%7C%5CAlph%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5CAlph%7Bsomecounter%7D%0A++++%5Citem+%5Cverb%7C%5Cfnsymbol%7Bsomecounter%7D%7C+typesets+the+%5Ctexttt%7Bsomecounter%7D+value+of++%5Cthesomecounter%7B%7D+as++%5Cfnsymbol%7Bsomecounter%7D%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

この例の出力は次のとおりです：

![LaTeXでカウンタを変換する例](/files/69dfe44980b1330a20d0bc22ede23b3b3f2f7448)

#### \value{somecounter}

LaTeXのソースコードに説明されているとおり、このコマンドの目的は「カウンタの値をTeX数としてアクセスすること」です。つまり、 `\value{somecounter}` を、LaTeXが数値を処理することを想定している場面で使います。

**（任意）\value コマンドに関する補足**

コマンド `\value{*somecounter*}` は *直接* の値を組版する `*somecounter*`ように思えますが、そうではありません。これは他のコマンドの中で、カウンタの値をTeX数として取得するために使うことを意図しており、次のコマンドとは異なります `\thesomecounter` これは `*somecounter*`.

の印字表現を生成します。 `\value` は次のように定義されています：

```latex
\def\value#1{\csname c@#1\endcsname}
```

その結果、 `\value{*somecounter*}` はLaTeX内部の制御綴り `\c@somecounter` のインスタンスを生成し、そこにカウンタ `*somecounter*`.

**例**

の値が入っています。 `\value`.

```latex
以下の例は、
\begin{verbatim}
\noindent まず2つの新しいカウンタを宣言します：
\newcounter{first}
\end{verbatim}

\noindent まず2つの新しいカウンタを宣言します：
\newcounter{first}

\noindent その後、コードの少し後でそれらの値を設定します：
\begin{verbatim}
\setcounter{first}{100}
\setcounter{second}{50}
\end{verbatim}

\setcounter{first}{100}
\setcounter{second}{50}
\noindent その後、さらに \LaTeX{} コードを書きます...\vspace{10pt}

\noindent ある時点で、カウンタ \texttt{second} の値をカウンタ \texttt{first} に加えたくなるかもしれません。その一つの方法は、\verb|\value| を使って \texttt{second} カウンタに保存されている\textit{現在の}値を取得することです：

\begin{verbatim}
\addtocounter{first}{\value{second}}
\end{verbatim}

\addtocounter{first}{\value{second}}\noindent \texttt{first} の値は \verb|\thefirst| で出力できます：\thefirst です。また、\verb|\the\value{first}| と書いても \the\value{first} として組版されます。しかし、\verb|\value{first}| と書くと、\LaTeX{} が内部コマンド \verb|\c@first| を実行しようとするため、\texttt{Missing number, treated as zero} というエラーが発生します。エラーを発生させるには、次の行のコメントを外してください：
\begin{verbatim}
%\value{first}
\end{verbatim}
%\value{first}
```

[このコード断片をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+the+value+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnoindent+Start+by+declaring+two+new+counters%3A%0A%5Cbegin%7Bverbatim%7D%0A%5Cnewcounter%7Bfirst%7D%0A%5Cnewcounter%7Bsecond%7D++++%0A%5Cend%7Bverbatim%7D%0A%0A%5Cnewcounter%7Bfirst%7D%0A%5Cnewcounter%7Bsecond%7D%0A%0A%5Cnoindent+Sometime+later+in+your+code+you+set+their+values%3A%0A%5Cbegin%7Bverbatim%7D%0A%5Csetcounter%7Bfirst%7D%7B100%7D%0A%5Csetcounter%7Bsecond%7D%7B50%7D++++%0A%5Cend%7Bverbatim%7D%0A%0A%5Csetcounter%7Bfirst%7D%7B100%7D%0A%5Csetcounter%7Bsecond%7D%7B50%7D++%0A%5Cnoindent+Then+you+write+more+%5CLaTeX%7B%7D+code...%5Cvspace%7B10pt%7D%0A%0A%5Cnoindent+At+some+point+we+might+want+to+add+the+value+of+counter+%5Ctexttt%7Bsecond%7D+to+counter+%5Ctexttt%7Bfirst%7D.+One+way+to+do+that+is+using+%5Cverb%7C%5Cvalue%7C+to+obtain+the+%5Ctextit%7Bcurrent%7D+value+stored+in+the+%5Ctexttt%7Bsecond%7D+counter%3A%0A%0A%5Cbegin%7Bverbatim%7D%0A%5Caddtocounter%7Bfirst%7D%7B%5Cvalue%7Bsecond%7D%7D%0A%5Cend%7Bverbatim%7D%0A%0A%5Caddtocounter%7Bfirst%7D%7B%5Cvalue%7Bsecond%7D%7D%5Cnoindent+The+value+of+%5Ctexttt%7Bfirst%7D+can+be+output+by+%5Cverb%7C%5Cthefirst%7C%3A+%5Cthefirst.+We+can+also+write+%5Cverb%7C%5Cthe%5Cvalue%7Bfirst%7D%7C+which+also+typesets+%5Cthe%5Cvalue%7Bfirst%7D.+However%2C++writing+%5Cverb%7C%5Cvalue%7Bfirst%7D%7C+will+generate+the+error+%5Ctexttt%7BMissing+number%2C+treated+as+zero%7D+because+%5CLaTeX%7B%7D+then+tries+to+execute+the+internal+command+%5Cverb%7C%5Cc%40first%7C.+Uncomment+the+following+line+to+generate+the+error%3A%0A%5Cbegin%7Bverbatim%7D%0A%25%5Cvalue%7Bfirst%7D%0A%5Cend%7Bverbatim%7D%0A%25%5Cvalue%7Bfirst%7D%0A%5Cend%7Bdocument%7D)

この例では次の出力が生成されます:

![\value コマンドを使う例](/files/70920787c2346bc733a4a87f0876419008c3e7b5)

### その他のカウンタ用コマンド

ここでは次のコマンドについて説明します `\counterwithin` や `\counterwithout` に由来し [`chngcntr` パッケージ](https://ctan.org/pkg/chngcntr) ましたが、今ではLaTeX本体に統合されています— [LaTeX News、2018年4月](https://www.latex-project.org/news/latex2e-news/ltnews28.pdf)と呼ばれます。 `chngcntr` [パッケージのドキュメント](https://mirror.ox.ac.uk/sites/ctan.org/macros/latex/contrib/chngcntr/chngcntr.pdf) は、これらのコマンドの目的を理解するのに役立つ次のコメントを述べています：

オン `\counterwithin`:

> あるカウンタを `\newcounter{<ctr>}` によって定義されたかのように振る舞うよう変更したいことがあります `\newcounter{<ctr>}[<within>]`。このパッケージは次のコマンドを提供します `\counterwithin{<ctr>}{<within>}` これで実現できます。

オン `\counterwithout`:

> 同様に、コマンド `\counterwithout{<ctr>}{<within>}` は、 `\newcounter{<ctr>}[<within>]` によって作成されたかのように振る舞うように `\newcounter{<ctr>}`.

要するに、これら2つのコマンドは、定義後の2つのカウンタを関連付ける（`\counterwithin`）／関連を外す（`\counterwithout`）方法を提供します。

**\counterwithin{somecounter}{anothercounter}**

リセット `*somecounter*` がステップされるたびに0に `*anothercounter*` 、2つのカウンタを結びつける仕組みです。リンクを作成するだけでなく、このコマンドは `\the*somecounter*`：カウンタの値を出力する代わりに `*somecounter*` それは生成します `<anothercounterの値>**.**<somecounterの値>`。星付き形式（`\counterwithin*{somecounter}{anothercounter}`）は `*somecounter*`を再定義しません。次の例で示すように、コードの下のリンクを選択してOverleafで開いてください：

```latex
\documentclass{article}
\begin{document}
\section{A short example}
\newcounter{example}
\counterwithin{example}{section}
\newcounter{exampletwo}
\counterwithin*{exampletwo}{section}
\setcounter{example}{100}
\setcounter{exampletwo}{100}
\begin{itemize}
\item \verb|\theexample| は \theexample を組版します
\item \verb|\theexampletwo| は \theexampletwo を組版します
\end{itemize}
\end{document}
```

[この `\counterwithin` Overleaf 上の例を開く](https://www.overleaf.com/docs?engine=\&snip_name=Example+of+starred+version+of+counterwithin\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BA+short+example%7D%0A%5Cnewcounter%7Bexample%7D%0A%5Ccounterwithin%7Bexample%7D%7Bsection%7D%0A%5Cnewcounter%7Bexampletwo%7D%0A%5Ccounterwithin%2A%7Bexampletwo%7D%7Bsection%7D%0A%5Csetcounter%7Bexample%7D%7B100%7D%0A%5Csetcounter%7Bexampletwo%7D%7B100%7D%0A%5Cbegin%7Bitemize%7D%0A%5Citem+%5Cverb%7C%5Ctheexample%7C+typesets+%5Ctheexample%0A%5Citem+%5Cverb%7C%5Ctheexampletwo%7C+typesets+%5Ctheexampletwo%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

この例では次の出力が生成されます:

![\counterwithin コマンドを使う](/files/e169389ec8ed3e65209a0ab9336246237783514e)

次のように `\counterwithin` LaTeXが提供する標準カウンタと一緒に使えます。たとえば：

* `section`：〜で使われる `\section` コマンド
* `equation` によって使われる `equation` 環境

、または自分で作成したカウンタと一緒に使えます。

**\counterwithin と \counterwithin\* の例**

以下のコードは、 `\counterwithin` や `\counterwithin*`を使う、より完全な例を示します。コードの下のリンクを選択してOverleafで開いてください：

```latex
\documentclass{article}
\counterwithin{equation}{section}
\newcounter{example}
\counterwithin*{example}{section}
\newenvironment{example}[1][]{%
\stepcounter{example}%
\par\vspace{5pt}\noindent
\fbox{\textbf{Example~\thesection.\theexample}}%
\hrulefill\par\vspace{10pt}\noindent\rmfamily}%
{\par\noindent\hrulefill\vrule width10pt height2pt depth2pt\par}
\begin{document}
\section{First equation}
導入テキスト...
\begin{example}
\begin{equation}
    f(x)=\frac{x}{1+x^2}
\end{equation}
\end{example}

\subsection{さらに詳しく}
\begin{example}
ここでは
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
…しかし何も言いません
\end{example}

\subsubsection{さらにさらに詳しく}
\begin{example}
\begin{equation}
    f(x)=\frac{x^2}{1+x^3}
\end{equation}
\begin{equation}
    f(x+\delta x)=\frac{(x+\delta x)^2}{1+(x+\delta x)^3}
\end{equation}
\end{example}

\section{3番目の式}
\begin{example}
次の関数...
\begin{equation}
    f_1(x)=\frac{x+1}{x-1}
\end{equation}
…は関数です
\begin{equation}
    f_2(x)=\frac{x^2}{1+x^3}
\end{equation}
\begin{equation}
    f_3(x)=\frac{3+ x^2}{1-x^3}
\end{equation}
\end{example}
\end{document}
```

[この `\counterwithin` Overleaf 上の例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+using+counterwithin\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Ccounterwithin%7Bequation%7D%7Bsection%7D%0A%5Cnewcounter%7Bexample%7D%0A%5Ccounterwithin%2A%7Bexample%7D%7Bsection%7D%0A%5Cnewenvironment%7Bexample%7D%5B1%5D%5B%5D%7B%25%0A%5Cstepcounter%7Bexample%7D%25%0A%5Cpar%5Cvspace%7B5pt%7D%5Cnoindent%0A%5Cfbox%7B%5Ctextbf%7BExample%7E%5Cthesection.%5Ctheexample%7D%7D%25%0A%5Chrulefill%5Cpar%5Cvspace%7B10pt%7D%5Cnoindent%5Crmfamily%7D%25%0A%7B%5Cpar%5Cnoindent%5Chrulefill%5Cvrule+width10pt+height2pt+depth2pt%5Cpar%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BFirst+equation%7D%0ASome+introductory+text...%0A%5Cbegin%7Bexample%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%7D%7B1%2Bx%5E2%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%0A%5Csubsection%7BMore+detail%7D%0A%5Cbegin%7Bexample%7D%0AHere+we+discuss%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%2B1%7D%7Bx-1%7D%0A%5Cend%7Bequation%7D%0A...+but+don%27t+say+anything%0A%5Cend%7Bexample%7D%0A%0A%5Csubsubsection%7BEven+more+detail%7D%0A%5Cbegin%7Bexample%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%29%3D%5Cfrac%7Bx%5E2%7D%7B1%2Bx%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cbegin%7Bequation%7D%0A++++f%28x%2B%5Cdelta+x%29%3D%5Cfrac%7B%28x%2B%5Cdelta+x%29%5E2%7D%7B1%2B%28x%2B%5Cdelta+x%29%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%0A%5Csection%7BThird+equation%7D%0A%5Cbegin%7Bexample%7D%0AThe+following+function...%0A%5Cbegin%7Bequation%7D%0A++++f_1%28x%29%3D%5Cfrac%7Bx%2B1%7D%7Bx-1%7D%0A%5Cend%7Bequation%7D%0A..is+a+function%0A%5Cbegin%7Bequation%7D%0A++++f_2%28x%29%3D%5Cfrac%7Bx%5E2%7D%7B1%2Bx%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cbegin%7Bequation%7D%0A++++f_3%28x%29%3D%5Cfrac%7B3%2B+x%5E2%7D%7B1-x%5E3%7D%0A%5Cend%7Bequation%7D%0A%5Cend%7Bexample%7D%0A%5Cend%7Bdocument%7D)

この例では次の出力が生成されます:

![\counterwithin コマンドを使う](/files/db0af02a68fa81fd4ea6588c9ea40c419da827fe)

**コードの概要**

* `\newcounter{example}`：これは、環境で使うための新しいカウンタを作成します。名前も `example`
* `\counterwithin*{example}{section}`：これは、私たちの `example` カウンタを `section` コマンド内で使われる `\section` コマンドを実行するたびに `\section` コマンドが `example` カウンタは0にリセットされます。星付き版 `\counterwithin*` を使って `\theexample`
* の再定義を避けていることに注意してください。 `example`次に、同じく `\stepcounter{example}` を使って環境のカウンタを増やします。さらに、 `\textbf{Example~\thesection.\theexample}` を出力するために **例** `<節番号>.<例番号>`

```latex
\newenvironment{example}[1][]{%
\stepcounter{example}%
\par\vspace{5pt}\noindent
\fbox{\textbf{Example~\thesection.\theexample}}%
\hrulefill\par\vspace{10pt}\noindent\rmfamily}%
{\par\noindent\hrulefill\vrule width10pt height2pt depth2pt}
```

**\counterwithout{somecounter}{anothercounter}**

`\counterwithout{*somecounter*}{*anothercounter*}` 間のリンクを解除します `*somecounter*` や `*anothercounter*` これにより、それらは独立します。任意の2つのカウンタについて、 `\counterwithout` や `\counterwithin`を使う方法と切り替えることができます `example` や `section` 、次の例は

```latex
\section{First equation}
\begin{example}
\begin{equation}
    f(x)=\frac{x}{1+x^2}
\end{equation}
\end{example}

\subsection{2番目の式}
\begin{example}
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
\end{example}
\vspace{6pt}\noindent \texttt{example} カウンタが \ref{sec:n0} 節の開始時にリセットされることに注目してください。
\section{3番目の式}
\label{sec:n0}
\begin{example}
\begin{equation}
    f(x)=\frac{x+1}{x-1}
\end{equation}
\end{example}
\vspace{6pt}\noindent ここでは、\verb|\counterwithout{example}{section}| と書いて、\texttt{example} が節の開始時にもうリセットされないようにしました。\ref{sec:n1} 節と \ref{sec:n2} 節では、\texttt{example} カウンタは増え続けます。\counterwithout{example}{section}
\section{4番目の式}
\label{sec:n1}
\begin{example}
\begin{equation}
    f(x)=\frac{x^2+x^3}{1+x^3}
\end{equation}
\end{example}
\section{5番目の式}
\label{sec:n2}
\begin{example}
\begin{equation}
    f(x,k)=\frac{x^2-x^k}{1+x^3}
\end{equation}
\end{example}
```

[この `\counterwithout` Overleaf 上の例を開く](<https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+of+using+counterwithout\&snip=\documentclass{article}&#xA;\counterwithin{equation}{section}&#xA;\newcounter{example}&#xA;\counterwithin*{example}{section}&#xA;\newenvironment{example}\[1]\[]{%&#xA;\stepcounter{example}%&#xA;\par\vspace{3pt}\noindent&#xA;\fbox{\textbf{Example~\thesection.\theexample}}%&#xA;\hrulefill\par\vspace{3pt}\noindent\rmfamily}%&#xA;{\par\noindent\hrulefill\vrule+width10pt+height2pt+depth2pt\par}&#xA;\begin{document}&#xA;\section{First+equation}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x}{1+x^2}&#xA;\end{equation}&#xA;\end{example}&#xA;&#xA;\subsection{Second+equation}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x+1}{x-1}&#xA;\end{equation}&#xA;\end{example}&#xA;\vspace{6pt}\noindent+Note+how+the+\texttt{example}+counter+is+reset+at+the+start+Section+\ref{sec:n0}.+&#xA;\section{Third+equation}&#xA;\label{sec:n0}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x+1}{x-1}&#xA;\end{equation}&#xA;\end{example}&#xA;\vspace{6pt}\noindent+Here,+we+wrote+\verb|\counterwithout{example}{section}|+so+that+\texttt{example}+is+no+longer+reset+at+the+start+of+a+section.+In+Sections+\ref{sec:n1}+and+\ref{sec:n2}+the+\texttt{example}+counter+keeps+increasing.+\counterwithout{example}{section}&#xA;\section{Fourth+equation}&#xA;\label{sec:n1}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x)=\frac{x^2+x^3}{1+x^3}&#xA;\end{equation}&#xA;\end{example}&#xA;\section{Fifth+equation}&#xA;\label{sec:n2}&#xA;\begin{example}&#xA;\begin{equation}&#xA;++++f(x,k)=\frac{x^2-x^k}{1+x^3}&#xA;\end{equation}&#xA;\end{example}&#xA;\end{document}>)

この例では次の出力が生成されます:

![counterwithoutコマンドを使う](/files/4477a04f0b14746e4d59fe1a18b6b81a31926e56)

## 例

### enumerate

この `enumerate` list環境では4つを使います *カウンター* 変数を使用して、各階層の現在のラベル値を追跡します:

|        |                        |
| ------ | ---------------------- |
| **階層** | **`enumerate`** カウンタ変数 |
| 階層1    | `enumi`                |
| 階層2    | `enumii`               |
| 階層3    | `enumiii`              |
| 階層4    | `enumiv`               |

これらのカウンタは、次を使って変更できます `\setcounter` コマンドです。以下の例を参照してください。これは必ずしも最善の解決策ではありません（次を参照 [Overleaf では次の一覧を示しています](/latex/ja/latexno/04-lists.md) 記事を参照してください）:

```latex
この例では、リストの番号を変更する一つの方法を示しています。ここでは、\texttt{enumi} カウンタの値を変更して、リストの番号付けを 4 から始めています（番号は \verb|\item| コマンドによって増加します）:

\begin{enumerate}
\setcounter{enumi}{3}
\item 何か。
\item 別のもの。
\item 別の要素。
\item リストの最後の項目。
\end{enumerate}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Modifying+an+enumerate+counter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BIntroduction%7D%0AThis+example+shows+one+way+to+change+the+numbering+of+a+list%3B+here%2C+changing+the+value+of+the+%5Ctexttt%7Benumi%7D+counter+to+start+the+list+numbering+at+4+%28it+is+incremented+by+the+%5Cverb%7C%5Citem%7C+command%29%3A%0A%0A%5Cbegin%7Benumerate%7D%0A%5Csetcounter%7Benumi%7D%7B3%7D%0A%5Citem+Something.%0A%5Citem+Something+else.%0A%5Citem+Another+element.%0A%5Citem+The+last+item+in+the+list.%0A%5Cend%7Benumerate%7D%0A%5Cend%7Bdocument%7D)

この例では次の出力が生成されます:

![enumerate カウンタの変更](/files/98b3cae1d755d201df8d23f8b5b87cd67cb95f74)

## LaTeX のデフォルトカウンタ

| 使い方                  | 名前                                                                  |
| -------------------- | ------------------------------------------------------------------- |
| 文書構造用                | <p>- 部<br>- 章<br>- 節<br>- 小節<br>- 小小節<br>- 段落<br>- 小段落<br>- ページ</p> |
| 浮動体用                 | <p>- 式<br>- 図<br>- 表</p>                                            |
| 脚注用                  | <p>- footnote<br>- mpfootnote</p>                                   |
| 次のもの用 `enumerate` 環境 | <p>- enumi<br>- enumii<br>- enumiii<br>- enumiv</p>                 |

## さらに読む

詳しくは以下を参照してください：

* [環境](/latex/ja/komando/02-environments.md)
* [コマンド](/latex/ja/komando/01-commands.md)
* [リスト](/latex/ja/latexno/04-lists.md)
* [ページ番号](/latex/ja/shu-shi-she-ding/03-page-numbering.md)
* [セクションと章](/latex/ja/wen-shu-gou-zao/01-sections-and-chapters.md)
* [数式](/latex/ja/shu-xue/01-mathematical-expressions.md)
* [画像の挿入](/latex/ja/sononotopikku/27-inserting-images.md)
* [表](/latex/ja/to/01-tables.md)
* [LaTeXで試験問題を作成する](/latex/ja/fen-ye-bie/09-typesetting-exams-in-latex.md)
* [独自のパッケージを書く](/latex/ja/kurasufairu/03-writing-your-own-package.md)
* [独自のクラスを書く](/latex/ja/kurasufairu/04-writing-your-own-class.md)
* [LaTeX2εへのそれほど短くない入門](http://www.ctan.org/tex-archive/info/lshort/)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://overleaf-pro.ayaka.space/latex/ja/shu-shi-she-ding/10-counters.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
