mcpField
T
tikz

tikz

github.com/igbuendデザイン・UI9278 回インストール33 週間前に更新
リポジトリ

tikz について

tikzは、LaTeXのTikZ/PGFでベクター画像や図を作成するためのリファレンススキルです。描画構文、再利用可能なスタイル、ライブラリ、図のパターン、技術図や数学図のトラブルシューティングを扱います。

主な機能

  1. 01パス、ノード、座標、図形
  2. 02スタイル、変換、レイヤー
  3. 03フローチャート、ツリー、グラフ、状態機械
  4. 04プロット、回路図、マインドマップ、タイムライン
  5. 05ライブラリ設定と一般的なエラーの修正

活用シーン

  1. 01LaTeX文書にラベル付きの図を描く
  2. 02フローチャートや状態機械図の作成
  3. 03プロットや回路図の作成
  4. 04レイアウトやコンパイルの問題解決

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/igbuend/grimbard --skill tikz

インストール後、Claude Code で/tikzを入力して読み込まれたか確認します。

詳細

TikZ/PGF — ベクターグラフィックスと図表

CTAN: https://ctan.org/pkg/pgf
マニュアル: texdoc tikz(約1300ページ)

セットアップ

\usepackage{tikz}
% Load libraries as needed:
\usetikzlibrary{arrows.meta, calc, positioning, decorations.pathmorphing,
  patterns, shapes.geometric, shapes.misc, fit, backgrounds,
  automata, trees, mindmap, circuits.logic.US}

基本的な描画

最小限の例
\begin{tikzpicture}
  \draw (0,0) -- (2,0) -- (2,2) -- cycle;  % triangle
\end{tikzpicture}
パス操作
操作構文例
線--(0,0) -- (1,1)
水平・垂直-|`(0,0) -
垂直・水平|-`(0,0)
曲線.. controls .. ..(0,0) .. controls (0.5,1) .. (1,0)
スムーズな曲線to[out=..,in=..](0,0) to[out=90,in=180] (2,1)
円弧arc(0,0) arc (0:90:1cm) 始点:終点:半径
円circle(0,0) circle[radius=1]
楕円ellipse(0,0) ellipse[x radius=2, y radius=1]
長方形rectangle(0,0) rectangle (2,1)
グリッドgrid(0,0) grid (3,3)
放物線parabola(0,0) parabola (2,2)
正弦・余弦sin/cos(0,0) sin (1,1)
プロットplotplot[domain=0:3] (\x, {\x^2})
閉じるcycleパスを始点に戻って閉じます
パスの操作
コマンド効果
\drawパスを線で描画します
\fillパスを塗りつぶします
\filldraw塗りつぶして線を描画します
\path非表示のパス(座標やノード用)
\clip以降の描画をパスでクリップします
\shadeグラデーションで塗りつぶします
\shadedrawグラデーションで塗りつぶし、線を描画します
\node\path node の省略形
\coordinate\path coordinate の省略形
よく使う描画オプション
\draw[
  color=blue,            % or just: blue
  line width=1pt,        % or: thin, thick, very thick, ultra thick
  dashed,                % or: dotted, dash dot, dash dot dot
  dash pattern={on 3pt off 2pt},
  rounded corners=5pt,
  ->                     % arrow tip (see arrows section)
  opacity=0.5,
  double,
  double distance=2pt,
] (0,0) -- (2,2);
よく使う塗りつぶしオプション
\fill[
  fill=blue!20,          % 20% blue
  fill opacity=0.5,
  pattern=north lines,   % requires patterns library
  pattern color=gray,
] (0,0) rectangle (2,1);

座標系

% Cartesian (default)
(2, 3)

% Polar (angle:radius)
(45:2cm)

% Relative (shift from last point)
++(1,0)    % move reference point
+(1,0)     % don't move reference point

% Named
\coordinate (A) at (1,2);
\draw (A) -- (2,3);

% Intersection (calc library)
\usetikzlibrary{calc}
($(A)!0.5!(B)$)           % midpoint of A and B
($(A)!0.5!90:(B)$)        % midpoint rotated 90°
($(A) + (1,2)$)           % A shifted by (1,2)
($(A)!1cm!(B)$)           % 1cm from A toward B

% Perpendicular coordinates (A -| B) = (x of B, y of A)
(A |- B)                   % (x of A, y of B)

% Barycentric
(barycentric cs:A=1,B=1,C=1)  % centroid

ノード

基本的なノード
\node (name) at (0,0) {Text};

% Node options
\node[
  draw,                    % draw border
  fill=yellow!20,
  rectangle,               % shape (default)
  circle,
  ellipse,
  rounded corners,
  minimum width=2cm,
  minimum height=1cm,
  inner sep=5pt,           % padding
  outer sep=2pt,           % margin
  text=red,
  font=\bfseries\small,
  align=center,            % for multi-line: use \\ in text
  text width=3cm,
  anchor=north,            % positioning anchor
  rotate=45,
] (mynode) at (1,2) {Hello\\World};
ノードのアンカー
          north west  north  north east
                 ┌──────────┐
            west │  center   │ east
                 └──────────┘
          south west  south  south east

ほかに、base、mid、text、45 や 135 のような角度アンカーがあります。

パス上のノード
\draw (0,0) -- node[above] {label} (3,0);
\draw (0,0) -- node[midway, sloped, above] {sloped text} (3,2);
\draw (0,0) -- node[pos=0.3, below] {at 30\%} (3,0);
\draw (0,0) to[out=90,in=0] node[near start, left] {A} (2,2);
よく使う形状(ライブラリが必要)
\usetikzlibrary{shapes.geometric, shapes.misc}

\node[diamond, draw] {D};
\node[star, draw, star points=5] {S};
\node[regular polygon, regular polygon sides=6, draw] {Hex};
\node[trapezium, draw] {T};
\node[cylinder, draw, shape border rotate=90] {DB};
\node[cloud, draw, cloud puffs=10] {Cloud};
\node[cross out, draw] at (0,0) {};
\node[strike out, draw] at (0,0) {};

矢印

\usetikzlibrary{arrows.meta}

% Arrow tips (arrows.meta syntax)
\draw[->] (0,0) -- (1,0);              % default
\draw[-Stealth] (0,0) -- (1,0);        % filled triangle
\draw[-Latex] (0,0) -- (1,0);          % larger filled
\draw[-{Stealth[length=5mm]}] (0,0) -- (1,0);
\draw[{Latex[red]}-{Latex[blue]}] (0,0) -- (1,0);  % colored
\draw[-{>[scale=2]}] (0,0) -- (1,0);

% Common tips: >, Stealth, Latex, To, Circle, Square, |, Hooks
% Modifiers: [length=..], [width=..], [open], [fill=..], [scale=..]

スタイル

% Define in preamble or tikzpicture options
\tikzset{
  mybox/.style = {draw, fill=blue!20, rounded corners, minimum width=2cm},
  myarrow/.style = {-Stealth, thick, red},
  highlight/.style = {fill=yellow, draw=orange, line width=2pt},
}

% Use
\node[mybox] {Box};
\draw[myarrow] (0,0) -- (1,1);

% Style with parameters
\tikzset{
  box/.style = {draw, fill=#1!20, minimum width=1.5cm},
  box/.default = blue,
}
\node[box] {Blue};        % default
\node[box=red] {Red};     % override

% every node/.style applies to all nodes
\begin{tikzpicture}[every node/.style={font=\small}]

Foreach ループ

% Basic
\foreach \x in {0,1,2,3}
  \draw (\x, 0) circle (0.3);

% With step
\foreach \x in {0,0.5,...,3}
  \fill (\x,0) circle (1pt);

% Multiple variables
\foreach \x/\y in {0/A, 1/B, 2/C}
  \node at (\x, 0) {\y};

% Counter
\foreach \x [count=\i] in {a,b,c,d}
  \node at (\i, 0) {\x};

% Evaluate
\foreach \x [evaluate=\x as \y using \x*\x] in {1,...,5}
  \fill (\x, \y/5) circle (2pt);

% Remember
\foreach \x [remember=\x as \lastx (initially 0)] in {1,...,5}
  \draw (\lastx, 0) -- (\x, 0);

スコープと変換

\begin{tikzpicture}
  \draw (0,0) -- (1,0);
  
  \begin{scope}[shift={(2,0)}, rotate=45, scale=0.5, red, thick]
    \draw (0,0) -- (1,0) -- (1,1) -- cycle;
  \end{scope}
  
  % Transformations
  % shift={(x,y)}, xshift=1cm, yshift=2cm
  % rotate=45, rotate around={45:(1,1)}
  % scale=2, xscale=2, yscale=0.5
  % xslant=0.5, yslant=0.5
\end{tikzpicture}

クリッピング

\begin{tikzpicture}
  \clip (0,0) circle (1.5cm);
  % Everything below is clipped to the circle
  \fill[blue!30] (-2,-2) rectangle (2,2);
  \draw[step=0.5, gray] (-2,-2) grid (2,2);
\end{tikzpicture}

レイヤー(backgrounds ライブラリ)

\usetikzlibrary{backgrounds}

\begin{tikzpicture}
  \node[draw, fill=white] (A) {Foreground};
  
  \begin{pgfonlayer}{background}
    \fill[yellow!30] (A.south west) rectangle (A.north east);
  \end{pgfonlayer}
\end{tikzpicture}

ピクチャ

\tikzset{
  myshape/.pic = {
    \draw (-0.5,-0.5) rectangle (0.5,0.5);
    \draw (0,0) circle (0.3);
  }
}

\begin{tikzpicture}
  \pic at (0,0) {myshape};
  \pic[rotate=45, scale=1.5] at (2,0) {myshape};
\end{tikzpicture}

デコレーション

\usetikzlibrary{decorations.pathmorphing, decorations.markings, decorations.text}

% Wavy/zigzag lines
\draw[decorate, decoration={zigzag, amplitude=2mm, segment length=5mm}]
  (0,0) -- (4,0);
\draw[decorate, decoration={snake, amplitude=1mm}]
  (0,0) -- (4,0);
\draw[decorate, decoration={coil, aspect=0.5}]
  (0,0) -- (4,0);
\draw[decorate, decoration={random steps, segment length=3mm}]
  (0,0) -- (4,0);

% Brace
\draw[decorate, decoration={brace, amplitude=5pt}]
  (0,0) -- (3,0) node[midway, above=5pt] {label};

% Text along path
\draw[decorate, decoration={text along path, text={Hello World}}]
  (0,0) .. controls (1,1) .. (3,0);

% Markings (arrows along path)
\draw[decoration={markings, mark=at position 0.5 with {\arrow{>}}},
  postaction={decorate}]
  (0,0) -- (3,0);

パターン

\usetikzlibrary{patterns}

\fill[pattern=north east lines] (0,0) rectangle (2,1);
\fill[pattern=crosshatch dots, pattern color=blue] (0,0) circle (1);

% Available: north east lines, north west lines, horizontal lines,
% vertical lines, crosshatch, dots, crosshatch dots,
% fivepointed stars, sixpointed stars, bricks, checkerboard

よく使う図表のパターン

フローチャート
\usetikzlibrary{arrows.meta, shapes.geometric, positioning}

\tikzset{
  startstop/.style = {rectangle, rounded corners, draw, fill=red!20,
    minimum width=3cm, minimum height=1cm},
  process/.style = {rectangle, draw, fill=blue!20,
    minimum width=3cm, minimum height=1cm},
  decision/.style = {diamond, draw, fill=green!20,
    minimum width=3cm, minimum height=1cm, aspect=1.5},
  io/.style = {trapezium, trapezium left angle=70, trapezium right angle=110,
    draw, fill=orange!20, minimum width=3cm, minimum height=1cm},
  arrow/.style = {thick, -Stealth},
}

\begin{tikzpicture}[node distance=1.5cm]
  \node[startstop] (start) {Start};
  \node[io, below=of start] (input) {Input $x$};
  \node[decision, below=of input] (decide) {$x > 0$?};
  \node[process, below left=of decide] (neg) {$y = -x$};
  \node[process, below right=of decide] (pos) {$y = x$};
  \node[io, below=2cm of decide] (output) {Output $y$};
  \node[startstop, below=of output] (stop) {Stop};

  \draw[arrow] (start) -- (input);
  \draw[arrow] (input) -- (decide);
  \draw[arrow] (decide) -| node[near start, above] {No} (neg);
  \draw[arrow] (decide) -| node[near start, above] {Yes} (pos);
  \draw[arrow] (neg) |- (output);
  \draw[arrow] (pos) |- (output);
  \draw[arrow] (output) -- (stop);
\end{tikzpicture}
ツリー
\begin{tikzpicture}[
  level 1/.style={sibling distance=4cm},
  level 2/.style={sibling distance=2cm},
  every node/.style={draw, circle, minimum size=8mm},
  edge from parent/.style={draw, -Stealth},
]
  \node {1}
    child { node {2}
      child { node {4} }
      child { node {5} }
    }
    child { node {3}
      child { node {6} }
      child { node {7} }
    };
\end{tikzpicture}
グラフ/ネットワーク
\usetikzlibrary{positioning}

\begin{tikzpicture}[
  vertex/.style={draw, circle, fill=blue!20, minimum size=8mm},
  edge/.style={thick},
  >=Stealth,
]
  \node[vertex] (a) at (0,2)  {A};
  \node[vertex] (b) at (2,3)  {B};
  \node[vertex] (c) at (4,2)  {C};
  \node[vertex] (d) at (2,0)  {D};

  \draw[edge] (a) -- node[above left]  {3} (b);
  \draw[edge] (b) -- node[above right] {2} (c);
  \draw[edge] (a) -- node[below left]  {7} (d);
  \draw[edge] (c) -- node[below right] {1} (d);
  \draw[edge] (b) -- node[right]       {4} (d);
\end{tikzpicture}
有限オートマトン/状態機械
\usetikzlibrary{automata, positioning}

\begin{tikzpicture}[shorten >=1pt, node distance=2.5cm, on grid, auto,
  every state/.style={draw, minimum size=1cm}]

  \node[state, initial]           (q0) {$q_0$};
  \node[state, right=of q0]      (q1) {$q_1$};
  \node[state, accepting, right=of q1] (q2) {$q_2$};

  \path[->]
    (q0) edge              node {a}   (q1)
    (q0) edge [loop above] node {b}   ()
    (q1) edge              node {a,b} (q2)
    (q1) edge [bend left]  node {b}   (q0)
    (q2) edge [loop above] node {a}   ();
\end{tikzpicture}
マインドマップ
\usetikzlibrary{mindmap}

\begin{tikzpicture}[mindmap, grow cyclic,
  every node/.style={concept, execute at begin node=\hskip0pt},
  concept color=blue!40,
  level 1/.append style={level distance=4cm, sibling angle=90},
  level 2/.append style={level distance=2.5cm, sibling angle=45},
]
  \node {Main Topic}
    child[concept color=red!40] { node {Sub 1}
      child { node {Detail A} }
      child { node {Detail B} }
    }
    child[concept color=green!40] { node {Sub 2}
      child { node {Detail C} }
    }
    child[concept color=orange!40] { node {Sub 3} }
    child[concept color=purple!40] { node {Sub 4} };
\end{tikzpicture}
ブロック図/システム図
\usetikzlibrary{positioning, arrows.meta, fit}

\begin{tikzpicture}[
  block/.style={draw, rectangle, fill=blue!10, minimum height=1cm,
    minimum width=2cm, align=center},
  sum/.style={draw, circle, minimum size=6mm},
  >=Stealth, thick,
]
  \node[sum] (sum) {};
  \node[block, right=1.5cm of sum] (ctrl) {Controller\\$G_c(s)$};
  \node[block, right=1.5cm of ctrl] (plant) {Plant\\$G(s)$};
  \node[block, below=1cm of plant] (fb) {Sensor\\$H(s)$};

  \draw[->] ++(-1.5,0) -- node[above] {$R(s)$} (sum);
  \draw[->] (sum) -- node[above] {$E(s)$} (ctrl);
  \draw[->] (ctrl) -- (plant);
  \draw[->] (plant) -- ++(2,0) node[above left] {$Y(s)$};
  \draw[->] (plant.east) ++(1,0) |- (fb);
  \draw[->] (fb) -| (sum) node[near end, left] {$-$};

  \node at (sum) {$\Sigma$};
\end{tikzpicture>
ベン図
\begin{tikzpicture}
  \fill[red!20]  (0,0)   circle (1.5cm);
  \fill[blue!20] (1.5,0) circle (1.5cm);
  
  \begin{scope}
    \clip (0,0) circle (1.5cm);
    \fill[purple!30] (1.5,0) circle (1.5cm);
  \end{scope}
  
  \draw (0,0) circle (1.5cm) node[left=1cm]  {$A$};
  \draw (1.5,0) circle (1.5cm) node[right=1cm] {$B$};
  \node at (0.75, 0) {$A \cap B$};
\end{tikzpicture}
タイムライン
\begin{tikzpicture}
  \draw[thick, -Stealth] (0,0) -- (10,0);
  
  \foreach \x/\year/\event in {
    1/2020/Founded,
    3/2021/v1.0,
    5.5/2022/IPO,
    8/2023/Global%
  }{
    \draw (\x, -0.15) -- (\x, 0.15);
    \node[above=3pt] at (\x, 0.15) {\footnotesize \year};
    \node[below=3pt, align=center, text width=1.5cm] at (\x, -0.15) {\footnotesize \event};
  }
\end{tikzpicture}
回路(論理ゲート)
\usetikzlibrary{circuits.logic.US}

\begin{tikzpicture}[circuit logic US, every circuit symbol/.style={thick}]
  \node[and gate] (and1) at (2,1) {};
  \node[or gate]  (or1)  at (4,0.5) {};

  \draw (0,1.25) node[left] {$A$} -- (and1.input 1);
  \draw (0,0.75) node[left] {$B$} -- (and1.input 2);
  \draw (and1.output) -- (or1.input 1);
  \draw (0,0)    node[left] {$C$} -- (or1.input 2);
  \draw (or1.output) -- ++(1,0) node[right] {$Y$};
\end{tikzpicture}
可換図式(数学)
\usetikzlibrary{positioning}

\begin{tikzpicture}[>=Stealth, node distance=2cm]
  \node (A)              {$A$};
  \node (B) [right=of A] {$B$};
  \node (C) [below=of A] {$C$};
  \node (D) [right=of C] {$D$};

  \draw[->] (A) -- node[above] {$f$} (B);
  \draw[->] (A) -- node[left]  {$g$} (C);
  \draw[->] (B) -- node[right] {$h$} (D);
  \draw[->] (C) -- node[below] {$k$} (D);
\end{tikzpicture}
プロット(組み込み、pgfplots 不要)
\begin{tikzpicture}[scale=0.8]
  \draw[->] (-0.5,0) -- (4.5,0) node[right] {$x$};
  \draw[->] (0,-0.5) -- (0,3.5) node[above] {$y$};
  
  \draw[blue, thick, domain=0:4, samples=100]
    plot (\x, {sqrt(\x)});
  
  \foreach \x in {1,...,4}
    \draw (\x, 0.1) -- (\x, -0.1) node[below] {\x};
  \foreach \y in {1,2,3}
    \draw (0.1, \y) -- (-0.1, \y) node[left] {\y};
\end{tikzpicture}

Positioning ライブラリ

\usetikzlibrary{positioning}

% Relative positioning (preferred over manual coordinates)
\node[right=of A] (B) {B};       % default separation
\node[right=1.5cm of A] (B) {B}; % custom distance
\node[above right=1cm and 2cm of A] (B) {B};  % separate x/y

% Available: above, below, left, right,
% above left, above right, below left, below right

Fit ライブラリ

\usetikzlibrary{fit}

% Draw a box around multiple nodes
\node[draw, dashed, fit=(A) (B) (C), inner sep=5pt, label=above:Group] {};

Calc ライブラリの主な操作

\usetikzlibrary{calc}

% Midpoint
($(A)!0.5!(B)$)

% Point 1cm from A toward B
($(A)!1cm!(B)$)

% Projection of C onto line AB
($(A)!(C)!(B)$)

% Rotated point: midpoint of AB rotated 90° around A
($(A)!0.5!90:(B)$)

% Vector addition
($(A) + (1,2)$)

% Scalar multiply
($2*(A) - (B)$)

% let syntax for complex calculations
\draw let \p1 = ($(B)-(A)$),
          \n1 = {veclen(\x1,\y1)}
      in (A) circle (\n1);

図の外部化(キャッシュ)

% In preamble — compiles each tikzpicture once, reuses PDF
\usetikzlibrary{external}
\tikzexternalize[prefix=tikz-cache/]

% To skip a figure:
\tikzexternaldisable
\begin{tikzpicture} ... \end{tikzpicture}
\tikzexternalenable

--shell-escape フラグが必要です:pdflatex --shell-escape main.tex

よくある問題

問題原因解決方法
"Dimension too large"座標が大きすぎますscale を使用するか、座標を小さくします
矢印の先端が表示されないarrows.meta ライブラリを読み込んでいません\usetikzlibrary{arrows.meta}
ノードのテキストが切れるノードが小さすぎますminimum width、text width を追加します
positioning が機能しないライブラリを読み込んでいません\usetikzlibrary{positioning}
ノードが重なる相対指定ではなく at を使用していますpositioning ライブラリを使って right=of を指定します
foreach のエラー本体に特殊文字があります本体を {} で囲みます
パスが閉じないcycle を忘れていますパスの末尾に -- cycle を追加します
コンパイルが遅い図が複雑ですexternal ライブラリを使ってキャッシュします
ノード内の数式$ が競合しますノードのテキスト内では通常どおり $...$ を使用します
色が見つからないxcolor がありませんTikZ は xcolor を読み込みます。追加の色を使うには、tikz より前に \usepackage[dvipsnames]{xcolor} を使用します

ヒント

  • ノードのレイアウトに座標をハードコードせず、positioning ライブラリを使用します
  • 複数の図でスタイルを再利用するには、プリアンブルで \tikzset を使用します
  • 表示を統一するには、every node/.style と every path/.style を使用します
  • オプションの適用範囲を限定するには、\begin{scope} を使用します
  • 交差点を見つけるには、name path=X と name intersections(intersections ライブラリ)を使用します
  • スタンドアロンの図には、\documentclass[tikz]{standalone} を使用します
  • 拡大インセットには、spy ライブラリを使用します
  • データ駆動型の図には、pgfplotstable を使用します(pgfplots スキルを参照)

コメント