TikZ
非常に高度な図表を作図できる
install
$ tlmgr install pgf
構文
描画環境
$ \tikz command;
;
でコマンドを区切る
コマンド中のスペース・改行は無視される
例示コードの注意点
以下、特に断りがない限り header.tex
を先頭につけて実行しているものとする
header.tex\documentclass[margin=0pt]{standalone}
\def\pgfsysdriver{pgfsys-ximera.def}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\draw [help lines] (0,0) grid (3,3);
は
方眼を引くコマンド
grid.tikz(tex)\begin{document}
\tikz \draw [ultra thick] (0,0) grid (3,3);
\quad
\tikz \draw [help lines,ultra thick] (0,0) grid (3,3);
\end{document}
help lines
は組み込みのスタイル
$ \tikzset{help lines/.style={gray,very thin}}
を実行したのと等価
TODO: 変数を示す記法を :variable
から <variable>
に変える
manualも後者を採用している
\path (:command1) (:command2) ...
他の命令は、制御構文を除くと全てこれのaliasかsub commandでできている
TikZは略記法で構成されている面が強い

どの記法がどの記法の略記なのかを知ると、記法に馴染みやすいし応用も効く
名前はofficial manualに倣った
新しいpathの始点を (:coordinate)
に打つ
現在座標から :coordinate
まで線を引く
現在座標と :cordinate
を対角線上に配置した長方形を引く
今の座標を中心に、半径 :radius
の円を引く
circle
には楕円を書くoptionもある
arc[start angle=<start>,end angle=<end>,delta angle=<delta>,radius=<radius>]
arc operation現在座標は中心ではなく端点になることに注意
start angle
, end angle
, delta angle
は、どれか1つを略してもいい
全部あるときは delta angle
が無視される
radius
のかわりに x radius
, y radius
を設定してもいい
楕円になる
略記法に arc(<start>:<end>:<radius>)
があるが、分かりづらいので推奨されていない
to[options]
出ていくときと入ってくるときの角度を指定して描く曲線
等角度で入出するときは bend left=:angle
か bend right=:angle
を入れる
別の角度にしたいときは、 in=:angle
, out=:angle
で設定する
grid (:coordinate)
graph
:グラフを書く
plot
:函数描画
pic
:特殊な描画コマンド
やや特殊な命令
node (:name) at (:coordinate) {:node_contents}
:coordinate
にテキスト :node_contents
を配置し、この座標に :name
と名付ける
:name
と :coordinate
は省略可
:coordinate
を省略すると現在座標になる
(後述)
:<animation attribute>
などなど
path
を実際に描画したり塗りつぶしたりしたいときは、 []
に描画オプションを入れる
pathを定義するだけ
draw
などの描画方法(actionと呼ぶ?)を []
に指定することで初めて見た目に反映される
[]
は \path ...
のどこに入れてもいいし、複数入れてもいい
ただし、 draw
は circle
の前に設定しないと描画されない
circles.tikz(tex)\begin{document}
\begin{tikzpicture}
\path[draw,radius=1] (0,0) circle node{(0,0)};
\path (2,0) [draw,radius=0.5] circle node{(2,0)};
\path (4,0) [draw] circle [radius=0.3] node{(4,0)};
\path[draw,radius=0.5] (0,2) circle[] (2,2) circle[] (4,2) circle [radius=0.2] (5,2) circle -- (6,0) circle;
\end{tikzpicture}
\end{document}
同じ \path
内でoptionsを書くとき
最初に書いたoptionの値がdefault value扱いされる
circle
命令の直後に座標を置くときは、空でもいいので []
を挟まないと文法エラーになるようだ
--
命令などなら []
不要
描画オプションを指定した
\pathはaliasが用意されている
\fill=
\path[fill]
塗りつぶしのみ
\clip=
\path[clip]
指定した領域のみ表示
official manualの"Part III TikZ ist kein Zeichenprogramm"に
\path記法の仕組みが解説されている
options(HTMLでいう属性に相当)は []
の中に書く
,
区切りであり、option nameには空白を入れられることに注意
例えば \path[use as bounging box]
は use as bounging box
が一つのoptionである
また \node
は大きさがあるため、配置が微妙に異なる
等価なコマンド
coordinate
= node[coordinate]
= node[shape=coordinate]
\coordinate
= \path coordinate
= \path node[coordinate]
= \node[coordinate]
$ \coordinate (:name) at (:coordinate);
座標の記法
構文
(:coordinate_system cs::key-values)
いくつかの座標系は略記法がある
標準で用意されている座標系
xy平面座標
(canvas cs:x=2cm,y=1.5cm)
略記: (2cm,1.5cm)
3次元直交座標
(xyz cs:x=2cm,y=1.5cm,z=3.4cm)
略記: (2cm,1.5cm,3.4cm)
(canvas polar cs:angle=30,radius=1cm)
略記: (30:1cm)
(barycentric cs:A=1,B=1)
node name
しか指定できないみたい
xyz polar
よくわからない

xy polar
よくわからない

node
事前に定義したnodeの座標を参照する
(node cs:name=A)
略記: (A)
(node cs:name=A,anchor=north)
略記: (A.north)
(node cs:name=A,angle=-30)
略記: (A.-30)
anchor
と anghle
はどちらか一方のみ指定できる
(perpendicular cs:horizontal line through={(2,1)}, vertical line through={(3,4)}
略記: (2,1 -| 3,4)
もしくは (3,4 |- 2,1)
perpendicular.tikz(tex)\begin{document}
\begin{tikzpicture}
\path (2,1) coordinate (A) node[below] {A};
\path (3,2) coordinate (B) node[right] {B};
\draw (A) -- (perpendicular cs:horizontal line through={(A)},vertical line through={(B)});
\draw[dashed] (B) -- (B |- A);
\node[below] at (A -| B) {C};
\end{tikzpicture}
\end{document}
接線を引ける \node
は shape=coordinate
と shape=circle
のみ
透視投象の座標系
three point perspective
(略記:
ttp
)
etc.
他の座標から計算
2点間の中点
calc
を使う場合: ($(A)!0.5!(B)$)
重心座標を使う場合: (barycentric cs:A=1,B=1)
++
で一つ前の座標を基準に動く
相対位置指定
基準の位置と受け手側の配置位置、2点の位置関係から決定する
標準機能
relative-position.tikz(tex)\begin{document}
\begin{tikzpicture}
\fill (1,1) coordinate (A) circle[radius=1pt];
\node[above] at (A) {B};
\fill (2,1) coordinate (C) circle[radius=1pt];
\node[anchor=south] at (C) {B};
\end{tikzpicture}
\end{document}
above
系は、基準となる座標のどこに配置するかを決める
above
なら基準の上に来る
above=.3cm
などで間隔を調整できる
anchor
は配置するオブジェクトの基準点を決める
defaultは多分オブジェクトによって違う
標準機能だけで相対指定する
relative-position2.tikz(tex)\begin{document}
\begin{tikzpicture}
\tikzset{ball/.style={draw, circle},indicator/.style={thick,<->}};
\def\l{2cm}
\node[ball] (A) at (0,0) {A};
\node[ball] (B) at (0,\l) {B};
\draw[indicator] (A) --node[auto,swap]{edge} (B);
\draw[indicator] (A.west) --node[auto]{\l} (B.west);
\node[ball,right=\l/2] (C) at (A) {C};
\node[ball,above=\l] at (C) (D) {D};
\draw[indicator] (C.center) --node[auto,swap]{\l} (D);
\node[ball,right=\l/2] (E) at (C) {E};
\node[ball,above=\l] at (E.center) (F) {F};
\draw[indicator] (E.center) --node[auto,swap]{\l} (F);
\node[ball,right=\l/2] (G) at (E) {G};
\node[ball,above=\l,anchor=south] at (G.north) (H) {H};
\draw[indicator] (G) --node[auto,swap]{\l} (H);
\node[ball,right=\l/2] (I) at (G) {I};
\node[ball,above=\l,anchor=center] at (I.center) (J) {J};
\draw[indicator] (I.east) --node[auto]{\l} (J.east);
\draw[dashed] (B.north) -- (J.north);
\end{tikzpicture}
\end{document}
交点に設定
stealth-
で線の始端に矢印
-stealth
で線の終端に矢印
stealth-stealth
で線の両端に矢印
矢印は stealth
以外にもいろいろ選べる
graphs.tikz(tex)\usetikzlibrary {graphs}
\begin{document}
\tikz \graph { a -> {b, c} -> d };
\end{document}
styling
CSSに相当するもの
:style_name/.style={...}
...
に、 []
内に入れているものを書く
local application
各 \path
もしくは \begin{tikzpicture}
の []
に入れる
,
区切りで定義する
style名には半角スペースも使用可能
改行できないので注意
global application
改行可能
スタイルには引数を設定できる
#1
, #2
, ...が変数名
1変数までは設定無しに使用可能
2変数以上使いたいときは、 /.style
を /.style 2 args
に置き換える
2
は使いたい変数の数を入れる
計算
実数の計算はdefaultで可能
座標内で括弧 ()
を使った計算式を書くときは、計算式全体を {}
で囲む
例
$ \draw (1, 1) -- ({(3 + 4) / 2}, -5 / 2)
それ以外は囲む必要なし
from onlyamsmath
のmanualの Acknowledgement
回転させたラベルをつける
例:中央まわり90°回転
$ node [left, rotate=90, anchor=south]
nodeのanchorがdefaultで east
なので、文字列の真ん中に来るよう anchor=south
を指定する必要がある
TikZ環境全体の回転拡大縮小
色
色合成や数値指定の書式は xcolor
に従う
\usetikzlibrary{optics, calc}
が必要
tex \draw [ultra thick] (1,1) -- (1,2) -- (2,2) -- (2,1) -- cycle;
\draw (1,2) to[dim arrow={label=2cm}] (2,2);
2024-01-29 13:25:30 曲芸。非推奨
opticsは光学レンズの図を描くためのpackage。矢印を描くためのpackageではない
繰り返し処理
拡張機能の詰め合わせ
うーん、まあ使うほどでもないかな
実用的な図
平面座標上に円や寸法、数式を書き込む
角度の記号を出すやつ?
ほかにも描画できるっぽい?
2d-rotate.tikz(tex)\begin{document}
\begin{tikzpicture}[domain=-2:4,yscale=1,samples=200,>=latex,thick]
% \clip (0,0) rectangle (5,5);% 切り抜き
\tikzset{axis/.style={thick,->}};
\draw[axis] (-1,0) -- (4,0) node[right] {$x$};% x軸
\draw[axis] (0,-1) -- (0,4) node[below left] {$y$};% y軸
\coordinate (O) at (0,0);
\draw (O) node[below left] {O};% 原点
\draw[domain=-0.5:2] plot (\x, {2*\x}) node[right] {$y=2x$};
\draw[domain=-1:4] plot (\x, {-2/3*\x+2}) node[right] {$2x+3y=6$};
\draw[domain=-1:4] plot (\x, {3/2}) node[right] {$y=\frac{3}{2}$};
\coordinate (A) at (1,2);
\coordinate (O) at (3/4,3/2);
\coordinate (B) at (2,3/2);
\coordinate (C) at (2,2/3);
\tikzset{arrow angle/.style={draw=black,->,very thick,angle eccentricity=1.4,angle radius=#1}};
\pic["$\alpha$",arrow angle=8mm] {angle=B--O--A};
\pic["$\beta$",arrow angle=9mm] {angle=C--O--B};
\end{tikzpicture}
\end{document}
2d-circle.tikz(tex)\usetikzlibrary{angles}
\usetikzlibrary{quotes}
\usepackage{amsmath}
\def\bm#1{\boldsymbol{#1}}
\begin{document}
\begin{tikzpicture}[domain=-2:4,yscale=1,samples=200,>=latex,thick]
% \clip (0,0) rectangle (5,5);% 切り抜き
\draw[thick,->] (-2,0) -- (4,0) node[right] {$x$};% x軸
\draw[thick,->] (0,-2) -- (0,6) node[below left] {$y$};% y軸
\draw (0,0) node[below left] {O};% 原点
\draw[dotted] (0,0) circle[radius=10mm];
\coordinate (O) at (0,0);
\coordinate (A) at (5/2,12/2);
\coordinate (B) at (4/2,-3/2);
\coordinate (C) at (77/65,21/65);
\fill (A) circle (2pt) node[right] {$\mathrm{A}(5,12)$};% 点
\fill (B) circle (2pt) node[right] {$\mathrm{B}(4,-3)$};% 点
\fill (C) circle (2pt) node[right] {$\mathrm{C}$};% 点
\draw (O)--(A);
\draw (O)--(B);
\draw (C)--(5/13,12/13);
\draw (C)--(4/5,-3/5);
\draw [thick,->] (O)--(5/13,12/13) node[right] {$\hat{\bm{a}}$};
\draw [thick,->] (O)--(4/5,-3/5) node[right] {$\hat{\bm{b}}$};
\draw[domain=-2:3,color=black] plot (\x, {3/11*\x}) node[right] {$3x-11y=0$};
\end{tikzpicture}
\end{document}
parabora.tikz(tex)\begin{document}
\begin{tikzpicture}[domain=-2:4,samples=200,>=latex,thick]
% \clip (0,0) rectangle (5,5);% 切り抜き
\draw[thick,->] (-2,0) -- (5,0) node[right] {$x$};% x軸
\draw[thick,->] (0,-1) -- (0,9) node[below right] {$y$};% y軸
\draw (0,0) node[below left] {O};% 原点
\draw[domain=4.2:-2.5,color=black] plot (\x, {1/2*\x*\x}) node[right] {$C:y=\frac{1}{2}x^2$};
\draw[domain=-0.5:4.4,color=black] plot (\x, {2*\x}) node[right] {$y=2x$};
\coordinate (O) at (0,0);
\coordinate (Q) at (2,2);
\coordinate (R) at (2,4);
\coordinate (P) at (1*1.2,2*1.2);
\draw[dashed] (4,8) -- (4,0) node[below] {4};% y軸
\draw (Q) node[right] {Q};
\draw (R) node[above left] {R};
\draw (P) node[left] {P};
\draw (2,0) node[below] {$t$};
\draw (R)--(2,0);
\draw (O)--(P) node [midway,left] {$X$};
\draw (P)--(Q) node [midway,below] {$Y$};
\draw [bend left,distance=20mm,dotted] (O) to node [fill=white,inner sep=0.5pt,circle] {[$\sqrt{5}$]} (R);
\draw [bend right,distance=10mm,dotted] (O) to node [fill=white,inner sep=0.5pt,circle] {[1]} (2,0);
\draw [bend left,distance=20mm,dotted] (R) to node [fill=white,inner sep=0.5pt,circle] {[2]} (2,0);
\end{tikzpicture}
\end{document}
条件分岐
何故かエラーを吐くことが多くて疲れた

目的の条件式を作っても計算してくれない
文法を理解していないのかも
整数と比較するときは、 int()
で整数型に明示的に変換しないといけなかったようだ
プログラムあるあるだけどさあ……こんなの気づかないって。
References
滅茶苦茶長い
officical manualを
LWarpでHTMLに変換したもの
animationを使ったTIkZの図もSVGに変換されててすごい

原理から解説しているもの
広範囲に渡る、TikZを使ったpackagesの紹介
作例集もある
うまい書き方の模索も少し載っている
有向グラフの作り方
原理的な話はない
事例集
topicごとに見れる
平面幾何問題の図