> 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/to/05-tikz-package.md).

# TikZ パッケージ

## はじめに

[Ti*k*Z](https://en.wikipedia.org/wiki/PGF/TikZ) は、LaTeXで図形要素を作成するための、たぶん最も複雑で強力なツールです。簡単な例から始めて、この記事では、線、点、曲線、円、長方形などを描くいくつかの基本概念を紹介します。

まず、 `tikz` パッケージは次の行を含めて読み込みます `\usepackage{tikz}` をドキュメントのプリアンブルに追加し、その後、次の `tikzpicture` 環境内で。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[gray, thick] (-1,2) -- (2,-4);
\draw[gray, thick] (-1,-1) -- (2,2);
\filldraw[black] (0,0) circle (2pt) node[anchor=west]{交点};
\end{tikzpicture}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=First+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cdraw%5Bgray%2C+thick%5D+%28-1%2C2%29+--+%282%2C-4%29%3B%0A%5Cdraw%5Bgray%2C+thick%5D+%28-1%2C-1%29+--+%282%2C2%29%3B%0A%5Cfilldraw%5Bblack%5D+%280%2C0%29+circle+%282pt%29+node%5Banchor%3Dwest%5D%7BIntersection+point%7D%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

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

![OLV2tikz1.png](/files/f4a989f4eb7f7bc10915e58c5d53c1262dc57c0c)

この例では、2本の線と1つの点が描かれています。線を追加するには、コマンド `\draw[gray, thick]` は、次の特徴を持つ図形要素を定義します。 *色* は `gray` と、 `太い` 線幅です。線は実際には2つの端点によって定義され、 `(-1,2)` や `(2,-4)`、それらが `--`.

この *点* は実際には `円` として描かれます。 `\filldraw[black]`というコマンドは、円を描くだけでなく、黒で塗りつぶします。このコマンドでは、中心点 `(0,0)` と半径 `(2pt)` が指定されています。点の横には `ノード`があり、これは実際にはテキスト `交点`を含むボックスで、 `西側` に固定されています。

各 `;` draw `の最後に使うセミコロン` コマンド。

に注意することが重要です。 `tikzfigure` 環境は figure やそれに類する環境の中に入れることができます。この話題については [画像の挿入](/latex/ja/sononotopikku/27-inserting-images.md) 記事を参照してください。

## 基本要素: 点、線、パス

この節では、より凝った図を作るために組み合わせられる、いくつかの基本的な図形要素をどのように作成するかを示す例を紹介します。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}

\draw (-2,0) -- (2,0);
\filldraw [gray] (0,0) circle (2pt);
\draw (-2,-2) .. controls (0,0) .. (2,-2);
\draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);

\end{tikzpicture}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Second+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%0A%5Cdraw+%28-2%2C0%29+--+%282%2C0%29%3B%0A%5Cfilldraw+%5Bgray%5D+%280%2C0%29+circle+%282pt%29%3B%0A%5Cdraw+%28-2%2C-2%29+..+controls+%280%2C0%29+..+%282%2C-2%29%3B%0A%5Cdraw+%28-2%2C2%29+..+controls+%28-1%2C0%29+and+%281%2C0%29+..+%282%2C2%29%3B%0A%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

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

![OLV2tikz2.png](/files/3c5f1202432a675b2b847007d9a8e672da105a41)

この例には3つの基本コマンドがあります:

* **`\draw (-2,0) -- (2,0);`**：これは、端点が `(-2,0)` や `(2,0)`.
* **`\filldraw [gray] (0,0) circle (2pt);`**：点は非常に小さな `gray` `円` を中心とし `(0,0)` 、半径が `(2pt)`と呼ばれます。 `\filldraw` コマンドは要素を描画し、特定の色で塗りつぶすために使われます。さらに例は次の節を参照してください。
* **`\draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);`**： [ベジエ曲線](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)。これを定義する点は4つあります: `(-2,2)` や `(2,2)` が端点で、 `(-1,0)` や `(1,0)` すでに [*制御点*](https://en.wikipedia.org/wiki/Control_point_\(mathematics\)) が、その曲がり具合を決めます。これら2点は「引力点」と考えることができます。

## 基本的な図形: 円、楕円、多角形

図形はより単純な要素から構成できるので、まずは円、楕円、弧から始めましょう。

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\filldraw[color=red!60, fill=red!5, very thick](-1,0) circle (1.5);
\fill[blue!50] (2.5,0) ellipse (1.5 and 0.5);
\draw[ultra thick, ->] (6.5,0) arc (0:220:1);
\end{tikzpicture}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Third+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cfilldraw%5Bcolor%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%5D%28-1%2C0%29+circle+%281.5%29%3B%0A%5Cfill%5Bblue%2150%5D+%282.5%2C0%29+ellipse+%281.5+and+0.5%29%3B%0A%5Cdraw%5Bultra+thick%2C+-%3E%5D+%286.5%2C0%29+arc+%280%3A220%3A1%29%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

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

![TikzEx3.png](/files/997cdf8ec3668e6c28b679681d45b56075c42a11)

* **`\filldraw[color=red!60, fill=red!5, very thick](-1,0) circle (1.5);`**：このコマンドは前の節で、 *点*を描画するために使われましたが、今回は角括弧内にいくつか追加のパラメータがあります。以下で説明します:
  * **`color=red!60`**：円の周りの輪郭の色は60%の赤（「純粋な」赤より薄い）に設定されています。LaTeXで利用可能な既定の色の一覧は [リファレンスガイド](#reference-guide) を参照してください。また、 [LaTeXで色を使う](/latex/ja/shu-shi-she-ding/13-using-colors-in-latex.md) を参照して、自分で色を作る方法を学んでください。
  * **`fill=red!5`**：円はさらに薄い赤で塗りつぶされます。
  * **`非常に太い`**：このパラメータは線の太さを定義します。 [リファレンスガイド](#reference-guide) 値の完全な一覧は
* **`\fill[blue!50] (2.5,0) ellipse (1.5 and 0.5);`**：楕円を作成するには中心点を指定し `(2.5,0)`、2つの半径、つまり水平方向と垂直方向の半径（`1.5` や `0.5` それぞれ）を与えます。また、コマンド `fill` の代わりに `の最後に使うセミコロン` または filldraw にも注目してください。これは、この場合、外側と内側の色を制御する必要がないためです。
* **`\draw[ultra thick, ->] (6.5,0) arc (0:220:1);`**：このコマンドは、 `(6.5,0)`から始まる弧を描画します。追加のパラメータ `->` は、弧の終端に矢印が付くことを示します。開始点に加えて、開始角、終了角、半径の3つの値を指定する必要があります。ここでは、これら3つのパラメータ値は次の形式で指定されています `(0:220:1)`.

曲線の図形に加えて、同様の構文で直線を使う要素も作成できます:

```latex
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[blue, very thick] (0,0) rectangle (3,2);
\draw[orange, ultra thick] (4,0) -- (6,0) -- (5.7,2) -- cycle;
\end{tikzpicture}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Fourth+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%0A%5Cdraw%5Bblue%2C+very+thick%5D+%280%2C0%29+rectangle+%283%2C2%29%3B%0A%5Cdraw%5Borange%2C+ultra+thick%5D+%284%2C0%29+--+%286%2C0%29+--+%285.7%2C2%29+--+cycle%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

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

![OLV2tikz4.png](/files/25dc8abcb406012505ace5f6a215830ae9dae7f4)

* **`\draw[blue, very thick] (0,0) rectangle (3,2);`**：長方形は特別なコマンド `rectangle`で作成します。2点を指定する必要があり、1つ目は「鉛筆」が長方形の描画を始める位置、2つ目は対角の反対側の角の点です。
* **`\draw[orange, ultra thick] (4,0) -- (6,0) -- (5.7,2) -- cycle;`**：多角形を描くには、 *閉じた* 直線のパスを描きます: 〜からの線 `(4,0)` から `(6,0)` と〜からの線 `(6,0)` から `(5.7,2)`と呼ばれます。 `cycle` という命令は、開始点と終了点を一致させて「閉じた」パス（図形）を作成することを意味し、その結果、最後の線分が生成されます。

## 図

ノードはTikZの中でもおそらく最も多用途な要素です。導入部ですでに1つのノードを使い、図にテキストを追加しました。次の例では、ノードを使って図を作成します。

```latex
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[
roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm},
squarednode/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=5mm},
]
%ノード
\node[squarednode]      (maintopic)                              {2};
\node[roundnode]        (uppercircle)       [above=of maintopic] {1};
\node[squarednode]      (rightsquare)       [right=of maintopic] {3};
\node[roundnode]        (lowercircle)       [below=of maintopic] {4};

%線
\draw[->] (uppercircle.south) -- (maintopic.north);
\draw[->] (maintopic.east) -- (rightsquare.west);
\draw[->] (rightsquare.south) .. controls +(down:7mm) and +(right:7mm) .. (lowercircle.east);
\end{tikzpicture}
\end{document}
```

[Overleaf でこの例を開く](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=More+advanced+TikZ+example\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Btikz%7D%0A%5Cusetikzlibrary%7Bpositioning%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cbegin%7Btikzpicture%7D%5B%0Aroundnode%2F.style%3D%7Bcircle%2C+draw%3Dgreen%2160%2C+fill%3Dgreen%215%2C+very+thick%2C+minimum+size%3D7mm%7D%2C%0Asquarednode%2F.style%3D%7Brectangle%2C+draw%3Dred%2160%2C+fill%3Dred%215%2C+very+thick%2C+minimum+size%3D5mm%7D%2C%0A%5D%0A%25Nodes%0A%5Cnode%5Bsquarednode%5D++++++%28maintopic%29++++++++++++++++++++++++++++++%7B2%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28uppercircle%29+++++++%5Babove%3Dof+maintopic%5D+%7B1%7D%3B%0A%5Cnode%5Bsquarednode%5D++++++%28rightsquare%29+++++++%5Bright%3Dof+maintopic%5D+%7B3%7D%3B%0A%5Cnode%5Broundnode%5D++++++++%28lowercircle%29+++++++%5Bbelow%3Dof+maintopic%5D+%7B4%7D%3B%0A%0A%25Lines%0A%5Cdraw%5B-%3E%5D+%28uppercircle.south%29+--+%28maintopic.north%29%3B%0A%5Cdraw%5B-%3E%5D+%28maintopic.east%29+--+%28rightsquare.west%29%3B%0A%5Cdraw%5B-%3E%5D+%28rightsquare.south%29+..+controls+%2B%28down%3A7mm%29+and+%2B%28right%3A7mm%29+..+%28lowercircle.east%29%3B%0A%5Cend%7Btikzpicture%7D%0A%5Cend%7Bdocument%7D)

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

![OLV2tikz5.png](/files/bceb44d11ce5ff32aafc686c0e5e2e852525cd43)

この図には本質的に3つのコマンドがあります。ノード定義、ノード宣言、そして2つのノードを結ぶ線です。

* **`roundnode/.style={circle, draw=green!60, fill=green!5, very thick, minimum size=7mm}`**： `tikzpicture` 環境にパラメータとして渡されます。これにより、次の名前で参照されるノードが定義されます `roundnode`：このノードは円になり、外周は次の色で描画されます `green!60` で塗りつぶされます `green!5`。線幅は `非常に太い` 形状 `最小サイズ` は `7mm`。その下の行は、次の名前の2つ目の長方形ノードを定義します `squarednode`、同様のパラメータを使います。
* **`\node[squarednode] (maintopic) {2};`**：これは、前のコマンドで定義された `squarednode`を作成します。このノードのIDは `maintopic` で、数値 `2`を含みます。中括弧内を空白にすると、テキストは表示されません。
* **`[above=of maintopic]`**：最初のノード以外にはすべて、他のノードに対する位置を決める追加パラメータがあることに注意してください。たとえば、 `[above=of maintopic]` は、このノードが次の名前のノードの上に表示されることを意味します `maintopic`。この位置指定システムを使うには、 `\usetikzlibrary{positioning}` をプリアンブルに追加する必要があります。 `配置` ライブラリがない場合は、次の構文を使えます `above of=maintopic` その代わりに使えますが、 `配置` この構文のほうが柔軟で強力です。拡張して次のようにも書けます `above=**3cm** of maintopic` つまり、実際の距離を制御できます `maintopic`.
* **`\draw[->] (uppercircle.south) -- (maintopic.north);`**：矢印のような直線が描画されます。この構文はすでに [基本要素](#basic-elements-points-lines-and-paths) の節で説明しました。唯一の違いは、線の端点の書き方です。ノードを参照し（そのため名前を付けています）、そのノードに対する相対位置を指定します。

## リファレンスガイド

で指定可能な色と線の太さのパラメータ `tikz` パッケージを使ったサンプルコードです：

| パラメータ | 値                       | picture                                                              |
| ----- | ----------------------- | -------------------------------------------------------------------- |
| 色     | 白、黒、赤、緑、青、シアン、マゼンタ、黄    | ![BasicColours.png](/files/ca413b1e824dc5d0e27f6475ee6b7e46ee9c5ee8) |
| 線の太さ  | 極細、かなり細い、細い、太い、かなり太い、極太 | ![Thickness.png](/files/d25d1d67f16554597f85f14d86dc4e3de3f822aa)    |

お使いのLaTeX配布版には、さらに多くの色が用意されている場合があります。See [LaTeXで色を使う](/latex/ja/shu-shi-she-ding/13-using-colors-in-latex.md)

## さらに読む

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

* [LaTeXで色を使う](/latex/ja/shu-shi-she-ding/13-using-colors-in-latex.md)
* [Pgfplotsパッケージ](/latex/ja/fen-ye-bie/08-pgfplots-package.md)
* [画像の挿入](/latex/ja/sononotopikku/27-inserting-images.md)
* [表と図の一覧](/latex/ja/to/03-lists-of-tables-and-figures.md)
* [画像と表の配置](/latex/ja/to/02-positioning-images-and-tables.md)
* [LaTeXで直接図を描く](/latex/ja/to/04-picture-environment.md)
* [TikZ と PGF パッケージマニュアル](http://mirrors.ctan.org/graphics/pgf/base/doc/pgfmanual.pdf)
* [TeXample.net の TikZ と PGF の例](http://www.texample.net/tikz/examples/all/)


---

# 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/to/05-tikz-package.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.
