> 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/komando/02-environments.md).

# 環境

LaTeX 環境は、文書内容の一部に特定の組版効果を適用するために使われます。この記事では、既存の環境の使い方と、新しい環境の定義方法を説明します。

## はじめに

環境は次で始まり `\begin{name}` で終わります `\end{name}`:

```
\begin{name}
ここにコンテンツを入れます...
...ここに入ります...
\end{name}
```

「`名前`」は使用する環境の名前です。たとえば、次の例では「`center`」を使って、段落の書式を（中央揃えに）整えています：

```latex
\documentclass{article}
\begin{document}
\begin{center}
これは \texttt{center} 環境のデモです。
この段落のテキストは \textit{中央揃え} になります。なぜなら
特別な環境の中に
含まれているからです。環境は、
\end{center}
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Simple+environment+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Bcenter%7D%0AThis+is+a+demonstration+of+the+%5Ctexttt%7Bcenter%7D+environment.+%0AThis+paragraph+of+text+will+be+%5Ctextit%7Bcentred%7D+because+it+is+%0Acontained+within+a+special+environment.+Environments+provide+%0Aan+efficient+way+to+modify+blocks+of+text+within+your+document.%0A%5Cend%7Bcenter%7D%0A%5Cend%7Bdocument%7D)

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

![中央揃えを示す画像](/files/59c82bdef2c7fab587f830e11574a1de4feeb28d)

## 環境

次の例では `tabular` 環境を使って小さな表を組版します。 `tabular` 追加引数を受け取ります `{ c c c }` これはセルの配置を定義します。詳細は [表](/latex/ja/to/01-tables.md) 記事を参照してください。

```latex
\documentclass{article}
\begin{document}
\begin{tabular}{ c c c }
  cell1 & cell2 & cell3 \\
  cell4 & cell5 & cell6 \\
  cell7 & cell8 & cell9 \\
 \end{tabular}
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Simple+tabular+environment+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btabular%7D%7B+c+c+c+%7D+%0A++cell1+%26+cell2+%26+cell3+%5C%5C+%0A++cell4+%26+cell5+%26+cell6+%5C%5C+%0A++cell7+%26+cell8+%26+cell9+%5C%5C+%0A+%5Cend%7Btabular%7D%0A%5Cend%7Bdocument%7D)

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

![tabular 環境の例](/files/b667e56ef20008f563fe393c9129307097ba51b7)

いくつかの環境は、通常は括弧内（`[...]`).

## 新しい環境の定義

新しい環境を定義するには `\newenvironment` コマンドを使います。一般的な形は次のとおりです：

```
\newenvironment{name}[numarg][optarg_default]{begin_def}{end_def}
```

次のとおりです：

* `名前` これは、このユーザー定義引数の名前です；
* `numarg` は、この環境が受け取る引数の数（1〜9）です。もし `[numarg]` が省略されると、この環境は引数を受け取りません。たとえば、次の例で定義される [`boxed` 環境がそれに当たります](#defining-simple-environments);
* `optarg_default` 最初の引数を任意にし、既定値を与えます。つまり、任意の引数の値が与えられない場合に使われる値です；
* `begin_def` これは、環境が始まる（開く）ときに実行される LaTeX コードです。つまり、 `\begin{name}`と書いたときです。このコード内では、環境が受け取る引数を使えます。なお、任意引数は #1 で、残りの引数は #2 から #numarg で参照します；
* `end_def` これは、環境が終わる（閉じる）ときに実行される LaTeX コードです。つまり、 `\end{name}`と書いたときです。このコード内では、いかなる引数も使えません。

### シンプルな環境の定義

この最初の例では、 `boxed` 環境を定義します。これは引数を取りません。この環境の中に含まれるテキストの周囲に枠を描きます：

```latex
\documentclass{article}
%環境はプリアンブルで定義できます
\newenvironment{boxed}
    {\begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    }
    {
    \\\\\hline
    \end{tabular}
    \end{center}
    }
%
\begin{document}
これで文書内で \texttt{boxed} 環境を使えるようになります：

\begin{boxed}
このテキストは \texttt{boxed} 環境内で整形されています。
\end{boxed}

このテキストは \texttt{boxed} 環境の外で組版されています。
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Creating+a+new+environment\&snip=%5Cdocumentclass%7Barticle%7D%0A%25We+can+define+the+environment+in+the+preamble%0A%5Cnewenvironment%7Bboxed%7D%0A++++%7B%5Cbegin%7Bcenter%7D%0A++++%5Cbegin%7Btabular%7D%7B%7Cp%7B0.9%5Ctextwidth%7D%7C%7D%0A++++%5Chline%5C%5C%0A++++%7D%0A++++%7B+%0A++++%5C%5C%5C%5C%5Chline%0A++++%5Cend%7Btabular%7D+%0A++++%5Cend%7Bcenter%7D%0A++++%7D%0A%25%0A%5Cbegin%7Bdocument%7D%0ANow+we+can+use+the+%5Ctexttt%7Bboxed%7D+environment+in+our+document%3A%0A%0A%5Cbegin%7Bboxed%7D%0AThis+text+is+formatted+within+the+%5Ctexttt%7Bboxed%7D+environment.%0A%5Cend%7Bboxed%7D%0A%0AThis+text+is+typeset+outside+the+%5Ctexttt%7Bboxed%7D+environment.%0A%5Cend%7Bdocument%7D)

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

![カスタム LaTeX 環境のデモ](/files/43f81580eba5a87934a259f0ca11daa506503c71)

もし `boxed` の定義を `\newenvironment` の一般形と比べると、

* `名前` は `boxed`;
* も `[numarg]` または `[optarg_default]` が与えられていないことがわかります。なぜならこの環境は引数を取らないからです；
* `begin_def` は LaTeX コードです。中括弧の組 `{...}`—これは環境が *始まる* （開くとき）：

```latex
\begin{center}
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
```

* `end_def` は LaTeX コードです。中括弧の組 `{...}`—これは環境が *終わる* （閉じるとき）：

```latex
    \\\\\hline
    \end{tabular}
    \end{center}
```

この例では、 `begin_def` が `center` 環境を開始し、その中で `tabular` 環境が開かれて、環境内に置いたテキストの周囲に縦線と横線を描きます。 `end_def` さらに別の横線を引いてから、 `tabular` や `center` 環境を閉じます。

### 引数を取る環境の定義

前の例を次のように拡張してみましょう：

* タイトルを組版するために任意引数を使います。タイトルが指定されない場合は、既定値 `これは箱です`;
* 環境自体の中で提供するテキストの前に組版する内容を含む 2 つ目の引数を使います。

したがって、引数は 2 つあります：

* 最初の引数（引数 1）は任意です；
* 2 つ目の引数（引数 2）は任意ではありません。

つまり `numarg`=2.

更新した `boxed` 環境を使用しなければならないことに注意してください:

```latex
\newenvironment{boxed}[2][This is a box]
    {\begin{center}
    引数 1（\#1）=#1\\[1ex]
    \begin{tabular}{{{!}}p{0.9\textwidth}{{!}}}
    \hline\\
    引数 2（\#2）=#2\\[2ex]
    }
    {
    \\\\\hline
    \end{tabular}
    \end{center}
    }
```

この拡張版は、元の版と次の点が異なります：

* \[numarg] が存在し、値は 2 です；
* \[optarg\_default] も存在し、最初の引数が既定値付きの任意引数になります `これは箱です`.

次の例では、拡張した `boxed` 環境を使用しなければならないことに注意してください:

```latex
\documentclass{article}
% 最初の
% 引数の既定値は [This is a box] で与えられます
\newenvironment{boxed}[2][This is a box]
    {\begin{center}
    引数 1（\#1）=#1\\[1ex]
    \begin{tabular}{|p{0.9\textwidth}|}
    \hline\\
    引数 2（\#2）=#2\\[2ex]
    }
    {
    \\\\\hline
    \end{tabular}
    \end{center}
    }
\begin{document}
\textbf{例 1}：最初の引数に既定値を使う：

\begin{boxed}{Some preliminary text}
このテキストは環境の\textit{内側}にあります。
\end{boxed}

このテキストは環境の\textit{外側}にあります。

\vskip12pt

\textbf{例 2}：最初の引数に値を与える：

\begin{boxed}[This is not the default value]{Some more preliminary text}
このテキストもまだ環境の\textit{内側}にあります。
\end{boxed}

このテキストも環境の\textit{外側}にあります。
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Environment+with+an+optional+argument\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Note+the+default+value+for+the+first%0A%25+argument+is+provided+by+%5BThis+is+a+box%5D%0A%5Cnewenvironment%7Bboxed%7D%5B2%5D%5BThis+is+a+box%5D%0A++++%7B%5Cbegin%7Bcenter%7D%0A++++Argument+1+%28%5C%231%29%3D%231%5C%5C%5B1ex%5D%0A++++%5Cbegin%7Btabular%7D%7B%7Cp%7B0.9%5Ctextwidth%7D%7C%7D%0A++++%5Chline%5C%5C%0A++++Argument+2+%28%5C%232%29%3D%232%5C%5C%5B2ex%5D%0A++++%7D%0A++++%7B+%0A++++%5C%5C%5C%5C%5Chline%0A++++%5Cend%7Btabular%7D+%0A++++%5Cend%7Bcenter%7D%0A++++%7D%0A%5Cbegin%7Bdocument%7D%0A%5Ctextbf%7BExample+1%7D%3A+Use+the+default+value+for+the+first+argument%3A%0A+%0A%5Cbegin%7Bboxed%7D%7BSome+preliminary+text%7D%0AThis+text+is+%5Ctextit%7Binside%7D+the+environment.%0A%5Cend%7Bboxed%7D%0A%0AThis+text+is+%5Ctextit%7Boutside%7D+the+environment.%0A%0A%5Cvskip12pt%0A%0A%5Ctextbf%7BExample+2%7D%3A+Provide+a+value+for+the+first+argument%3A%0A+%0A%5Cbegin%7Bboxed%7D%5BThis+is+not+the+default+value%5D%7BSome+more+preliminary+text%7D%0AThis+text+is+still+%5Ctextit%7Binside%7D+the+environment.%0A%5Cend%7Bboxed%7D%0A%0AThis+text+is+also+%5Ctextit%7Boutside%7D+the+environment.%0A%5Cend%7Bdocument%7D)

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

![任意パラメータ付きの環境](/files/1d7ce6a4d73fc79cd1650842719f8af9b314f065)

### 番号付き環境

番号付き環境は、手動でも、あるいはコマンド `\newtheorem`を直接使っても作成できます。次の例を見てください：

```latex
\documentclass{article}
% プリアンブル内で番号付き環境を定義する
\newcounter{example}[section]
\newenvironment{example}[1][]{\refstepcounter{example}\par\medskip
   \noindent \textbf{Example~\theexample. #1} \rmfamily}{\medskip}

% \newtheorem を使って定義した別の番号付き環境
\usepackage{amsmath} % \newtheorem コマンド用
\newtheorem{SampleEnv}{Sample Environment}[section]
\begin{document}

\section{User-defined numbered environments}

\begin{example}
最初のユーザー定義番号付き環境（番号 \theexample）。
\end{example}

\begin{example}
2 つ目のユーザー定義番号付き環境（番号 \theexample）。
\end{example}

\section{More user-defined numbered environments}
例の番号が 1 から再開していることに注目してください：

\begin{example}
最初のユーザー定義番号付き環境（番号 \theexample）。
\end{example}

\begin{SampleEnv}
\verb|\newtheorem| コマンドで作成されたユーザー定義環境。
\end{SampleEnv}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Creating+a+numbered+environment\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Define+our+numbered+environment+within+the+preamble%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%25+Another+numbered+environment+defined+with+%5Cnewtheorem%0A%5Cusepackage%7Bamsmath%7D+%25+For+the+%5Cnewtheorem+command%0A%5Cnewtheorem%7BSampleEnv%7D%7BSample+Environment%7D%5Bsection%5D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%7BUser-defined+numbered+environments%7D%0A%0A%5Cbegin%7Bexample%7D%0AFirst+user-defined+numbered+environment+%28number+%5Ctheexample%29.%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7Bexample%7D%0ASecond+user-defined+numbered+environment+%28number+%5Ctheexample%29.%0A%5Cend%7Bexample%7D%0A%0A%5Csection%7BMore+user-defined+numbered+environments%7D%0ANote+how+the+example+numbering+has+restarted+at+1%3A%0A%0A%5Cbegin%7Bexample%7D%0AFirst+user-defined+numbered+environment+%28number+%5Ctheexample%29.%0A%5Cend%7Bexample%7D%0A%0A%5Cbegin%7BSampleEnv%7D%0AUser-defined+environment+created+with+the+%5Cverb%7C%5Cnewtheorem%7C+command.%0A%5Cend%7BSampleEnv%7D%0A%5Cend%7Bdocument%7D)

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

![ユーザー定義番号付き環境の例](/files/4413a89f2215d4776c2c43b423d7ef8548e758e9)

#### 注意点

* 手動で定義した `example` 環境では、コマンド `\newcounter{example}[section]` によりカウンタが作成され、これも `example`と呼ばれます。 `example` カウンタは次のとおりです：
  * \refstepcounter{example} `を使って 1 ずつ増加します；` 環境定義の中で；
  * 新しい `\section{...}` が始まるたびにリセットされます；
  * 現在の値を `\theexample`を使って組版できる変数です。 [カウンタに関する記事](/latex/ja/shu-shi-she-ding/10-counters.md) を参照すると、さらに詳しく学べます。
* コマンド `\newtheorem` 直接番号付き環境を作成し、3 つの引数を取ります：
  * 新しい環境の名前： `SampleEnv` は、この例では；
  * 行の先頭に太字で出力されるテキスト： `Sample Environment` は、この例では；
  * カウンタがリセットされるタイミングを決める任意引数です。これを使うと、カウンタの前にそのリセット用カウンタの値が付きます： `section` この例では。

この [`amsthm` パッケージ](https://ctan.org/pkg/amsthm?lang=en)と `amsmath`、は `\newtheorem`と並んで便利な追加定義を提供します； [定理と証明](/latex/ja/fen-ye-bie/01-theorems-and-proofs.md) を参照してください。

## 既存の環境の再定義

環境は `\renewenvironment` を使って再定義できます。構文は `\newenvironment`と同じです。 [新しい環境の定義](#defining-a-new-environment) さまざまなパラメータの説明は

```
\newenvironment{name}[numarg][optarg_default]{begin_def}{end_def}
```

次の例では `itemize` 環境を再定義します。これは手順を示すための例にすぎず、実際の文書で使うべきではありません。新しい `itemize` 環境はもはや箇条書きを生成しません。代わりに、その中のテキストを中央揃えにし、強調（斜体化）します（ `\em` コマンドを使って）。

```latex
\documentclass{article}
% プリアンブルで環境を再定義する
\renewenvironment{itemize}
{\begin{center}\em}
{\end{center}}
\begin{document}

\begin{itemize}
\texttt{itemize} 環境を再定義したので、その中のテキストはすべて
中央揃えになり、強調（斜体化）されています。もはや
箇条書きは作成されません。これはあくまで例であり、
実際の文書での使用は意図していません！
\end{itemize}
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Redefining+an+existing+environment\&snip=%5Cdocumentclass%7Barticle%7D%0A%25+Redefine+the+environment+in+the+preamble%0A%5Crenewenvironment%7Bitemize%7D%0A%7B%5Cbegin%7Bcenter%7D%5Cem%7D%0A%7B%5Cend%7Bcenter%7D%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Cbegin%7Bitemize%7D%0AWe+have+redefined+the+%5Ctexttt%7Bitemize%7D+environment+so+that+any+text+%0Awithin+it+is+centred+and+emphasised+%28italicized%29.+It+no+longer+creates%0Aa+bulleted+list---this+is+only+an+example+and+not+intended+for+use+%0Ain+real+documents%21%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

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

![再定義した itemize 環境の例](/files/4689aa5b3866c7cce4250970f6c4babc5468ab03)

## さらに読む

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

* [コマンド](/latex/ja/komando/01-commands.md)
* [パッケージとクラスファイルを理解する](/latex/ja/kurasufairu/01-understanding-packages-and-class-files.md)
* [独自のパッケージを書く](/latex/ja/kurasufairu/03-writing-your-own-package.md)
* [独自のクラスを書く](/latex/ja/kurasufairu/04-writing-your-own-class.md)
* [LaTeX における長さ](/latex/ja/shu-shi-she-ding/01-lengths-in-latex.md)
* [LaTeXで色を使う](/latex/ja/shu-shi-she-ding/13-using-colors-in-latex.md)
* [ページサイズと余白](/latex/ja/shu-shi-she-ding/07-page-size-and-margins.md)
* [パッケージとクラスファイルの一覧](/latex/ja/kurasufairu/02-overleaf-and-tex-live.md)
* [LaTeX2εへのそれほど短くない入門](http://www.ctan.org/tex-archive/info/lshort/)
* [WikiBooks の LaTeX/Creating\_Packages](http://en.wikibooks.org/wiki/LaTeX/Creating_Packages)


---

# 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/komando/02-environments.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.
