2026/07/23 Updated by

LaTeX

TikZ


[Up] Japanese English
tex ファイルのエンコーディングは utf-8 を使用すること。

線を描く: \draw

座標系の単位はほぼ 1cm となる。

直線を描く

  \draw (始点) -- (終点);
直線を描く
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw (0,0) -- (3,0);
\end{tikzpicture}

\end{document}
コンパイルする
$ lualatex t001.tex
t001.pdf が作成される。

pdf

3角形を描く

  \draw (始点) -- (第2点) -- (終点) -- cycle;
三角形を描く
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw (0,0) -- (3,0) -- (1.5, 3) -- cycle;
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t002.tex
t002pdf

長方形を描く

  \draw (左下点) rectangle (右上点);
長方形を描く
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw (0,0) rectangle (3, 2);
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t003.tex

t003pdf

長方形を塗りつぶす

  \draw[fill=色] (左下点) rectangle (右上点);
長方形を塗りつぶす
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[fill=gray!20] (0,0) rectangle (3, 2);
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t004.tex
t004pdf

円を描く

draw (中心座標,0) circle (半径);
円を描く
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw (5,5) circle (5cm);
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t005.tex
t005pdf

円を塗りつぶす

fill は塗りつぶす色、draw は枠線の色。
draw[fill=色] (中心座標,0) circle (半径);
draw[draw=色, fill=色] (中心座標,0) circle (半径);
円を塗りつぶす
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[fill=blue!20] (5,5) circle (5cm);
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t006.tex
t006pdf
円の枠を描き、を塗りつぶす
\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[draw=blue, fill=yellow] (5,5) circle (5cm);
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t007.tex
t007pdf

文字を描く: \node

\node at (座標) {文字};
文字を描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \node at (0,0) {中央};
  \node at (3,1) {右上};
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t008.tex
t008pdf
文字は指定した座標に対して位置を指定できる。
above
below
left
right
above right右上
below left左下
点の近くに文字を描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \fill (3,3) circle (2pt);
  \node[above] at (3,3) {点A};
\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t009.tex
t009pdf

図形の頂点に名前をつける

点と線を組み合わせる
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \coordinate (A) at (0,0);
  \coordinate (B) at (4,0);
  \coordinate (C) at (1,3);

  \draw (A) -- (B) -- (C) -- cycle;

  \fill (A) circle (2pt);
  \fill (B) circle (2pt);
  \fill (C) circle (2pt);

  \node[below left] at (A) {$A$};
  \node[below right] at (B) {$B$};
  \node[above] at (C) {$C$};

  \node at (4, 2) {三角形ABC};

\end{tikzpicture}

\end{document}
texからpdfを生成する
$ lualatex t010.tex
t010pdf

矢印を描く

矢印を描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[blue] (0,0) -- (4,1);
  \draw[red, thick, dashed] (0,1) -- (4,2);
  \draw[gree, very thick, dotted] (0,2) -- (4,3);

  \node[below left] at (0,0) {O};
  \node[above right] at (4,3) {A (4,3)};
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t011.tex
t011pdf

線の太さや種類を変える

thin細い線
thick太い線
very thickさらに太い線
dashed破線
dotted点線
線の種類を変更する
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[blue] (0,0) -- (4,1);
  \draw[red, thick, dashed] (0,1) -- (4,2);
  \draw[green, very thick, dotted] (0,2) -- (4,3);

  \node[below left] at (0,0) {O};
  \node[above right] at (4,3) {A (4,3)};
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t012.tex
t012pdf

座標軸を描く

座標軸を描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[->] (-0.5,0) -- (4.5,0) node[right] {$x$};
  \draw[->] (0,-0.5) -- (0,3.5) node[above] {$y$};

  \foreach \x in {1,2,3,4} {
    \draw (\x,0.1) -- (\x,-0.1);
    \node[below] at (\x,-0.1) {\x};
  }

  \foreach \y in {1,2,3} {
    \draw (0.1,\y) -- (-0.1,\y);
    \node[left] at (-0.1,\y) {\y};
  }
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t013.tex
t013pdf

関数のグラフを描く

関数y=x^2のグラフを描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
  \draw[->] (-2.5,0) -- (2.5,0) node[right] {$x$};
  \draw[->] (0,-0.5) -- (0,4.5) node[above] {$y$};

  \draw[domain=-2:2, samples=100, thick]
    plot (\x, {\x*\x});
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t014.tex
t014pdf

フローチャートを描く

フローチャートを描く
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
  box/.style={
    draw,
    rectangle,
    rounded corners,
    minimum width=3cm,
    minimum height=1cm,
    align=center
  },
  node distance=1.5cm
]

  \node[box] (start) {開始};
  \node[box, below=of start] (input) {データを入力};
  \node[box, below=of input] (process) {計算する};
  \node[box, below=of process] (end) {終了};

  \draw[->] (start) -- (input);
  \draw[->] (input) -- (process);
  \draw[->] (process) -- (end);

\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t015.tex
t015pdf
\usetikzlibrary{positioning}
上の宣言を追加しておくと、below=of start のように、他の要素を基準に位置を指定できる。

node 自体を図形にする

node自体を図形にする
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}

  \node[draw, rectangle] at (0,0) {四角};
  \node[draw, circle] at (3,0) {円};  

  \node[
    draw=blue,
    fill=blue!10,
    rounded corners,
    minimum width=3cm,
    minimum height=1cm
  ] at (2,5) {説明};
  
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t016.tex
t016pdf

スタイルを定義する

[定義] スタイル名/.style={設定内容}
[使用] \node[スタイル名]...
同じ書式を何度も使う場合は、スタイルを定義するとよい。
スタイルを定義する
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[
  mybox/.style={
    draw,
    thick,
    rounded corners,
    fill=gray!10,
    minimum width=3cm,
    minimum height=1cm
  }
]

  \node[mybox] at (0,0) {箱1};
  \node[mybox] at (4,0) {箱2};

\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t017.tex
t017pdf