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

図全体を拡大・縮小する

図全体を拡大・縮小する
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[scale=1.5]
  
  \draw (0,0) rectangle (3,2);
  \draw (0,0) -- (3,2);

  \node[draw] at (0,0) {普通サイズの文字};
\end{tikzpicture}

\end{document}
 
texからpdfを生成する
$ lualatex t018.tex
t018pdf
文字も含めて図全体を拡大・縮小する
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{tikzpicture}[scale=1.5, transform shape]
  
  \draw (0,0) rectangle (3,2);
  \draw (0,0) -- (3,2);

  \node[draw] at (0,0) {サイズを拡大した文字};

\end{tikzpicture}

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

よく使う命令のまとめ

\draw線や図形を描く
\fill図形を塗りつぶす
\filldraw図形の輪郭を描き、塗りつぶす
\node文字や箱を配置する
\coordinate座標に名前をつける
--直線を描く
rectangle長方形を描く
circle円を描く
cycle図形を閉じる
plotグラフを描く
\foreach処理を繰り返す

サンプル(1)

次の記述は、「点Aから点Bへ50%進んだ位置」、すなわち「2点の中間点」を求めている。
($(A)!0.5!(B)$)
いろいろな要素を使った例
\documentclass[a4paper,11pt]{ltjsarticle}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\begin{document}


\begin{center}
\begin{tikzpicture}[scale=1.2]

  \coordinate (A) at (0,0);
  \coordinate (B) at (5,0);
  \coordinate (C) at (2,3);
  \coordinate (D) at (2,0);

  % 三角形
  \draw[thick] (A) -- (B) -- (C) -- cycle;

  % 垂線
  \draw[dashed] (C) -- (D);

  % 直角記号
  \draw
    ($(D)+(0,0.25)$)
    -- ($(D)+(0.25,0.25)$)
    -- ($(D)+(0.25,0)$);

  % 頂点
  \fill (A) circle (2pt);
  \fill (B) circle (2pt);
  \fill (C) circle (2pt);
  \fill (D) circle (2pt);

  % 頂点名
  \node[below left] at (A) {$A$};
  \node[below right] at (B) {$B$};
  \node[above] at (C) {$C$};
  \node[below] at (D) {$D$};

  % 辺の説明
  \node[below] at ($(A)!0.5!(B)$) {$5$};
  \node[left] at ($(C)!0.5!(D)$) {$h$};

\end{tikzpicture}
\end{center}

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

サンプル2

日本語を使う例を示す。 ここで使用している ltjsarticle は、LuaLaTeX-ja が提供する日本語用の文書クラスである。
日本語を使った例
\documentclass[a4paper,11pt]{ltjsarticle}

% TikZを使用する
\usepackage{tikz}

% TikZの追加機能
\usetikzlibrary{
  arrows.meta,
  positioning
}


\title{LuaLaTeXによる日本語文書}
\author{山田太郎}
\date{\today}

\begin{document}

\maketitle

\section{はじめに}

これは、LuaLaTeXを使って作成したUTF-8の日本語文書です。

ソースファイルには、日本語をそのまま記述できます。
漢字、ひらがな、カタカナ、英数字を混在させることもできます。

\section{TikZによる簡単な図}

次の図は、四角形と円を矢印で結んだものです。

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

  \node[box] (input) {データを入力};
  \node[box, right=of input] (process) {データを処理};
  \node[
    draw,
    circle,
    right=of process,
    minimum size=2cm,
    align=center
  ] (output) {結果};

  \draw[arrow] (input) -- (process);
  \draw[arrow] (process) -- (output);

\end{tikzpicture}
\end{center}lualate

\section{日本語を含む三角形}

\begin{center}
\begin{tikzpicture}[scale=1.2]

  % 頂点を定義
  \coordinate (A) at (0,0);
  \coordinate (B) at (5,0);
  \coordinate (C) at (2,3);

  % 三角形を描く
  \draw[thick] (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[below] at (2.5,-0.4) {底辺};
  \node[right] at (3.7,1.6) {斜辺};
  \node[left] at (0.9,1.6) {斜辺};

\end{tikzpicture}
\end{center}

\section{数式}

日本語と数式を混在させることもできます。

三角形の底辺を $b$、高さを $h$ とすると、面積 $S$ は

\[
  S = \frac{1}{2}bh
\]

となります。

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

サンプル3

geometry パッケージを使って、文章の表示範囲を定義する。 tikz パッケージを使って、枠や表題を表弁する。
問題用紙の例
\documentclass[a4paper,11pt]{ltjsarticle}

\usepackage[margin=20mm,top=45mm]{geometry} % 問題文の表示開始位置を定義する
\usepackage{tikz}
\usetikzlibrary{
  arrows.meta,
  positioning,
  calc
}

\usepackage{eso-pic}
\usepackage{amsmath}
\usepackage{calc}

\pagestyle{empty}

% 枠線の設定
\newcommand{\PageFrame}{%
  \begin{tikzpicture}[remember picture,overlay]

    % 用紙端からの余白
    \coordinate (TL) at ([xshift=15mm,yshift=-15mm]current page.north west);
    \coordinate (TR) at ([xshift=-15mm,yshift=-15mm]current page.north east);
    \coordinate (BL) at ([xshift=15mm,yshift=15mm]current page.south west);
    \coordinate (BR) at ([xshift=-15mm,yshift=15mm]current page.south east);

    \node[font=\large, below right] at ($(TL)$) {2027年度 津田塾大学 総合型選抜 問題用紙};
    \node[font=\large, below left] at ($(TR)$) {\underline{ 1 } 枚のうちその \underline{ 1 }};
    
    % ページ全体の外枠
    %\draw[line width=0.8pt] (TL) rectangle (BR);

    % 上段の高さ
    \coordinate (HL) at ([yshift=-10mm]TL);
    \coordinate (HR) at ([yshift=-10mm]TR);

    \coordinate (L1) at ($(HL)+(0,-10mm)$);
    \coordinate (L2) at ($(L1)+(0,-5mm)$);

    \coordinate (R1) at ($(HR)+(0,-10mm)$);
    \coordinate (R2) at ($(R1)+(0,-5mm)$);

    % 上枠
    \draw[line width=0.8pt] (HL) rectangle (R1);
    % 下枠
    \draw[line width=0.8pt] (L2) rectangle (BR);

    % 上枠の縦線1:3/10 の位置
    \coordinate (V1Top) at ($(HL)!0.3!(HR)$);
    \coordinate (V1Bottom) at ($(L1)!0.3!(R1)$);
    \draw[line width=0.8pt] (V1Top) -- (V1Bottom);

    % 上枠の縦線2:1/2 の位置
    \coordinate (V2Top) at ($(HL)!0.35!(HR)$);
    \coordinate (V2Bottom) at ($(L1)!0.35!(R1)$);
    \draw[line width=0.8pt] (V2Top) -- (V2Bottom);

    %上枠の文字
    \node[font=\large\bfseries] at ($(HL)!0.5!(V1Bottom)+(0,0)$) {情報科学科};
    \node[font=\small,align=center] at ($(V1Top)!0.5!(V2Bottom)+(0,0)$) {科\\目};
    \node[font=\large\bfseries] at ($(V2Top)!0.5!(R1)+(0,0)$) {数学に関する事前課題};
    

  \end{tikzpicture}%
}

\AddToShipoutPictureBG{\PageFrame}

\begin{document}

\noindent
次の問いに答えなさい。

\bigskip

\textbf{問題1}

関数
\[
  f(x)=x^2-4x+3
\]
の最小値を求めなさい。

\vspace{30mm}

\textbf{問題2}

次の方程式を解きなさい。
\[
  x^2-5x+6=0
\]

\end{document}
texからpdfを生成する
授業で配布するプリントを参照して下さい。
t032pdf