> 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/01-commands.md).

# コマンド

## はじめに

この記事では、〜への最初の入門を紹介します *コマンド*。これらは LaTeX の組版機能に不可欠な要素です。ほとんどの LaTeX コマンドは、特別な文字、通常は `\`であるバックスラッシュ文字を前に付けた単純な語です。いくつか例を見てみましょう：

```latex
\documentclass{article}
\begin{document}
文書には、さまざまな種類の \textbf{コマンド} があります
これらは要素の表示方法を定義します。これらの
コマンドは特別な要素を挿入することがあります：$\alpha \beta \Gamma$
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+commands+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AIn+a+document+there+are+different+types+of+%5Ctextbf%7Bcommands%7D+%0Athat+define+the+way+the+elements+are+displayed.+This+%0Acommands+may+insert+special+elements%3A+%24%5Calpha+%5Cbeta+%5CGamma%24%0A%5Cend%7Bdocument%7D)

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

![LaTeX を使った基本的なテキスト整形](/files/5c0c1bfc77f88fa036233bbe7094d615362cd2a1)

前の例では、さまざまな種類のコマンドがあります。たとえば、 `\textbf` は、コマンドにパラメータとして渡されたテキストを太字にします。数式モードには、次のような特殊なコマンドがあります： `\alpha`, `\beta` と `\Gamma` ギリシャ文字を表示するためのものです。

## コマンド

箇条書きリストを作成するコマンドを含む LaTeX コードの例です：

```latex
\documentclass{article}
\begin{document}
リストの例：
\begin{itemize}
  \item[\S] 最初の項目
  \item 2つ目の項目
\end{itemize}
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+itemized+list+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0AA+list+example%3A%0A%5Cbegin%7Bitemize%7D%0A++%5Citem%5B%5CS%5D+First+item%0A++%5Citem+Second+item%0A%5Cend%7Bitemize%7D%0A%5Cend%7Bdocument%7D)

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

![LaTeX で組版した基本的なリスト](/files/77f4ce54a78e2eda0b7200f6c7b3084e7269c7bd)

コマンド `\begin{itemize}` を開始します `itemize` *環境*—を参照してください。 [環境に関する記事](/latex/ja/komando/02-environments.md) 詳細はそちらをご覧ください。環境宣言の下にあるのがコマンド `\item` で、LaTeX に新しいリスト項目を開始するよう指示します。整形のために、特別な印（bullet と呼ばれる小さな黒い点）を付け、項目をインデントします。

いくつかのコマンドには 1 つ以上の *パラメータ*が必要です。たとえば、 `\textbf` コマンド（上で使ったもの）は、1 つのパラメータ、つまり太字で表示するテキストを受け取ります。これは次のように波括弧の中に書きます： `\textbf{make this bold}`.

また、 *オプションの* コマンドに渡して動作を変更できるパラメータもあります。これらのオプション引数は角括弧の中に入れる必要があります： `[...]`。上の例では、コマンド `\item[\S]` は次と同じことをします： `\item`ただし、角括弧の中には `\S`があり、項目テキストの前にある黒い点の代わりに別の文字を置きます。

## 新しいコマンドの定義

LaTeX には非常に多くのコマンドが用意されていますが、作業を簡単にしたり、繰り返し作業を減らしたり、複雑な整形を行ったりするために、自分専用の特別なコマンドを定義する必要がしばしばあります。

### シンプルなコマンド

新しいコマンドは `\newcommand`によって定義します。では、簡単な例を見てみましょう：

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\R}{\mathbb{R}}
実数の集合は通常
黒板太字の大文字 R: \( \R \) で表されます。
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Creating+a+simple+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5CR%7D%7B%5Cmathbb%7BR%7D%7D%0AThe+set+of+real+numbers+are+usually+represented+%0Aby+a+blackboard+bold+capital+R%3A+%5C%28+%5CR+%5C%29.%0A%5Cend%7Bdocument%7D)

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

![新しいコマンドの出力](/files/58549c82a0ca8e3b9f6efff27fceee834e689922)

この文 `\newcommand{\R}{\mathbb{R}}` には、新しいコマンドを定義する 2 つの引数があります。内訳は次のとおりです：

* `\R`：新しいコマンドの名前です。
* `\mathbb{R}`：新しいコマンドが行う処理です。この場合、文字 R が黒板太字体で出力されます。 `\mathbb` コマンドの使用には `amssymb` パッケージが必要です。これは上の例（プリアンブル内）に追加されています。

コマンドを定義した後は、上で示したように本文中で使用できます。この例では、新しいコマンドは使用する段落の直前で定義されていますが、このような定義を文書全体に散らばらせるのは推奨される方法ではありません。一般に、新しいコマンドの定義は文書のプリアンブルにまとめるか、より大きな場合は別ファイルに入れておき、それを [メイン文書に読み込む](/latex/ja/wen-shu-gou-zao/07-management-in-a-large-project.md#inputting-and-including-files).

### パラメータ付きコマンド

いくつかのパラメータを受け取る新しいコマンドを作成することもできます。たとえば：

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}
\newcommand{\bb}[1]{\mathbb{#1}}
他の数体系にも同様の表記があります。
複素数 \( \bb{C} \)、有理
数 \( \bb{Q} \)、整数 \( \bb{Z} \) です。
\end{document}
```

[この例をOverleafで開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Basic+command+with+1+parameter\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewcommand%7B%5Cbb%7D%5B1%5D%7B%5Cmathbb%7B%231%7D%7D%0AOther+numerical+systems+have+similar+notations.+%0AThe+complex+numbers+%5C%28+%5Cbb%7BC%7D+%5C%29%2C+the+rational+%0Anumbers+%5C%28+%5Cbb%7BQ%7D+%5C%29+and+the+integer+numbers+%5C%28+%5Cbb%7BZ%7D+%5C%29.%0A%5Cend%7Bdocument%7D)

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

![パラメータ付き LaTeX コマンドの出力](/files/07dfb9a294fc30cdb6636b6243e5305afeff315c)

この行 `\newcommand{\bb}[1]{\mathbb{#1}}` は、1 つのパラメータを取る新しいコマンドを定義します。内訳は次のとおりです：

* `\bb` は新しいコマンドの名前です。
* `[1]` は、新しいコマンドが受け取るパラメータ数です。
* `\mathbb{#1}` はコマンドが実際に行う処理、つまりその定義です。

この場合、 `#1`で参照されるパラメータは、黒板太字の文字で出力されます。コマンドに複数のパラメータが必要な場合は、各パラメータを `#1`, `#2` のように参照できます。最大 9 個のパラメータに対応しています。

### オプション引数付きコマンド

ユーザー定義コマンドは、これまでの例よりさらに柔軟にできます。 *オプションの* 次の引数を取るコマンドを定義できます：

```latex
\documentclass{article}
\begin{document}

指数を含む式をたくさん書くときは、適切なコマンドを定義することで作業を簡略化し、時間を節約できます：

\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}

このように使えます：\[ \plusbinomial{x}{y} \]

指数さえも変更できます：

\[ \plusbinomial[4]{a}{b} \]
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Example+command+with+optional+parameters\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cbegin%7Bdocument%7D%0A%0AWhen+writing+many+expressions+with+exponents+we+can+simplify+our+task%2C+and+save+time%2C+by+defining+a+suitable+command%3A%0A%0A%5Cnewcommand%7B%5Cplusbinomial%7D%5B3%5D%5B2%5D%7B%28%232+%2B+%233%29%5E%231%7D%0A%0AWe+can+use+it+like+this%3A+%5C%5B+%5Cplusbinomial%7Bx%7D%7By%7D+%5C%5D%0A%0AAnd+even+the+exponent+can+be+changed%3A%0A%0A%5C%5B+%5Cplusbinomial%5B4%5D%7Ba%7D%7Bb%7D+%5C%5D%0A%5Cend%7Bdocument%7D)

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

![オプション引数付きコマンドの出力](/files/94909234256fd06897e0623c685e046a30ca636e)

この行の構文を見てみましょう `\newcommand{\plusbinomial}[3][2]{(#2 + #3)^#1}`:

* `\plusbinomial` は新しいコマンドの名前です。
* `[3]` はコマンドが受け取るパラメータ数で、この場合は 3 です。
* `[2]` は 1 つ目のパラメータの既定値です。これにより 1 つ目のパラメータがオプションになり、渡されなければこの既定値が使われます。
* `(#2 + #3)^#1` はコマンドが行う処理です。この場合、2 つ目と 3 つ目のパラメータを「二項式形式」にして、1 つ目のパラメータで表される指数を付けます。

## 既存のコマンドの上書き

すでに存在する *既存の* LaTeX コマンドと同じ名前の新しいコマンドを定義しようとすると、コンパイルはエラーメッセージを出して失敗します。これは、（すでに存在する） `\textbf` コマンドを定義しようとする次の例で示されます：

```latex
\documentclass{article}
\newcommand{\textbf}[1]{#1}% This will not work
\begin{document}

\section{This will fail}
\end{document}
```

[この（**エラーを発生させる**）例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Duplicate+command+name\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cnewcommand%7B%5Ctextbf%7D%5B1%5D%7B%231%7D%25+This+will+not+work%0A%5Cbegin%7Bdocument%7D%0A%0A%5Csection%7BThis+will+fail%7D%0A%5Cend%7Bdocument%7D)

この例をコンパイルすると、次の出力が得られます：

![重複した LaTeX コマンド定義によって生成されたエラー](/files/77a8b4b8de180eaefd636b360813221ac21fdd49)

スクリーンショットに示されているように、 `\textbf` の定義は失敗しました。LaTeX は次のエラーメッセージを生成しました： **LaTeX エラー: コマンド \textbf は既に定義されています**.

既存のコマンドを本当に上書きしたい場合は、 `\renewcommand`と同じ構文を使う `\newcommand`:

```latex
\documentclass{article}
\usepackage{amssymb}
\begin{document}

\renewcommand{\S}{\mathbb{S}}

リーマン球面（複素数に $\infty$ を加えたもの）は
しばしば \( \S \) で表されます。
\end{document}
```

[この例を Overleaf で開く。](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Redefine+existing+command\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Bamssymb%7D%0A%5Cbegin%7Bdocument%7D%0A%0A%5Crenewcommand%7B%5CS%7D%7B%5Cmathbb%7BS%7D%7D%0A%0AThe+Riemann+sphere+%28the+complex+numbers+plus+%24%5Cinfty%24%29+is+%0Asometimes+represented+by+%5C%28+%5CS+%5C%29.%0A%5Cend%7Bdocument%7D)

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

![再定義された LaTeX コマンドの出力](/files/70aac85675d1e1e2cf20e613c983b740c0f20227)

この例では、コマンド `\S` （ [コマンド](#commands) 節の例を参照）を上書きして、黒板太字の S を出力するようにしています。

## 参考文献

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

* [環境](/latex/ja/komando/02-environments.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/01-commands.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.
