generated at
TikZ
LaTeX作図package
非常に高度な図表を作図できる

install
$ tlmgr install pgf
pgf packageの一部として配布されている

構文

描画環境
\begin{tikzpicture}環境内に書き込む
\tikzを使うとインラインでも書ける
$ \tikz command;
; でコマンドを区切る
コマンド中のスペース・改行は無視される

例示コードの注意点
以下、特に断りがない限り header.tex を先頭につけて実行しているものとする
TikZJaxの仕様
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
\path (:command1) (:command2) ...
:command Operation (TikZ)などが入る
他の命令は、制御構文を除くと全てこれのaliasかsub commandでできている
TikZは略記法で構成されている面が強いtakker
どの記法がどの記法の略記なのかを知ると、記法に馴染みやすいし応用も効く

名前はofficial manualに倣った
(:coordinate) move-to operation
新しいpathの始点を (:coordinate) に打つ
--(:coordinate) line-to operation
現在座標から :coordinate まで線を引く
rectangle (:coordinate) rectangle operation
現在座標と :cordinate を対角線上に配置した長方形を引く
circle[radius=:radius] circle operation
今の座標を中心に、半径 :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]
出ていくときと入ってくるときの角度を指定して描く曲線
Bezier曲線より設定しやすい
等角度で入出するときは bend left=:angle bend right=:angle を入れる
別の角度にしたいときは、 in=:angle , out=:angle で設定する
何も指定しないときはline-to operationと等価
grid (:coordinate)
方眼を引く
graph :グラフを書く
plot :函数描画
pic :特殊な描画コマンド
やや特殊な命令
node (:name) at (:coordinate) {:node_contents}
:coordinate にテキスト :node_contents を配置し、この座標に :name と名付ける
:name :coordinate は省略可
:coordinate を省略すると現在座標になる
(後述)
:<animation attribute>
などなど
Syntax for Path Specifications - PGF/TikZ Manualにだいたい網羅されている
\usetikzlibrary{} \usepackage{} で追加ライブラリを取り込むことで、他のOperation (TikZ)を使える様になる
opticsなど
<defs>みたいなこともできるらしい

path を実際に描画したり塗りつぶしたりしたいときは、 [] に描画オプションを入れる
\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 命令の直後に座標を置くときは、空でもいいので [] を挟まないと文法エラーになるようだ
-- 命令などなら [] 不要
tikz覚書 p.4に関連する記述有り

描画オプションを指定した\pathはaliasが用意されている
\draw= \path[draw] 枠線のみ
\fill= \path[fill] 塗りつぶしのみ
\filldraw= \path[fill,draw] 枠線と塗りつぶし
\pattern= \path[pattern]
\shade= \path[shade]
\shadedraw \path[shade,draw]
\clip= \path[clip] 指定した領域のみ表示
\useasboundingbox= \path[use as bounging box]

official manualの"Part III TikZ ist kein Zeichenprogramm"に\path記法の仕組みが解説されている

options(HTMLでいう属性に相当)は [] の中に書く
, 区切りであり、option nameには空白を入れられることに注意
例えば \path[use as bounging box] use as bounging box が一つのoptionである



\coordinateで座標変数を定義する
\nodeでもできるが、ラベルが描画されてしまう
また \node は大きさがあるため、配置が微妙に異なる
等価なコマンド
coordinate = node[coordinate] = node[shape=coordinate]
\coordinate = \path coordinate = \path node[coordinate] = \node[coordinate]
$ \coordinate (:name) at (:coordinate);
変数の意味は\nodeと同じ

座標の記法
VisualTikZの6.2 Coordinatesも参照
構文
(: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
よくわからないtakker
xy polar
よくわからないtakker
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}
\usetikzlibrary{}や外部パッケージで導入する座標系
任意の点から\nodeへ接線を引く
接線を引ける \node shape=coordinate shape=circle のみ
透視投象の座標系 three point perspective (略記: ttp )
円筒座標 xyz cylindrical
球座標 xyz spherical
etc.
他の座標から計算
標準機能およびcalc (TikZ)を使った四則演算
2点間の中点
calc を使う場合: ($(A)!0.5!(B)$)
重心座標を使う場合: (barycentric cs:A=1,B=1)
++ で一つ前の座標を基準に動く
+ \pathの最初の座標を基準に動く
相対位置指定
基準の位置と受け手側の配置位置、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 などで間隔を調整できる
positioning (TikZ)なしでもこの記法を使えるが、非推奨。
positioning (TikZ)を使うことが推奨されている
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}
ある程度はpositioning (TikZ)なしで設定できるっぽいな
TikZ の使い方 (圏論編) p.16にわかりやすい図がある
positioning (TikZ)を使うとより凝った指定ができる?
VisualTikZの7.7 Node labelsも参照
交点に設定

stealth- で線の始端に矢印
-stealth で線の終端に矢印
stealth-stealth で線の両端に矢印
矢印は stealth 以外にもいろいろ選べる
VisualTikZTikZ の使い方 (圏論編)の9 Arrow Tipsにある

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
改行可能
\tikzstyle{}というコマンドもあるが非推奨https://tasusu.github.io/tikz.html#id4
スタイルには引数を設定できる
#1 , #2 , ...が変数名
1変数までは設定無しに使用可能
2変数以上使いたいときは、 /.style /.style 2 args に置き換える
2 は使いたい変数の数を入れる
スタイルの適用範囲を制限・一括適用するのに\begin{scope}を使う
scope (TikZ)を使うと略記できるが、正直いらないと思う


計算
実数の計算はdefaultで可能
座標内で括弧 () を使った計算式を書くときは、計算式全体を {} で囲む
$ \draw (1, 1) -- ({(3 + 4) / 2}, -5 / 2)
それ以外は囲む必要なし
座標同士の計算をするときはcalc (TikZ)を読み込む


calc (TikZ)onlyamsmathと干渉するので注意
対策として、 onlyamsmath のoptionにnodollardollarを入れる
from onlyamsmath のmanualの Acknowledgement

回転させたラベルをつける
例:中央まわり90°回転
$ node [left, rotate=90, anchor=south]
nodeのanchorがdefaultで east なので、文字列の真ん中に来るよう anchor=south を指定する必要がある

TikZ環境全体の回転拡大縮小
\begin{tikzpicture} scale 及び rotate optionsで指定できるが、線幅や文字の大きさ・配置は変化しない
\begin{tikzpicture} \end{tikzpicture} 全体を\scalebox{}{}\rotatebox{}{}で囲むと、文字等も含めて回転させられる
\resizebox{}{}{}を使うと、横幅だけでなく縦幅の倍率も変えられる

xcolorにある色をdefaultで使える
色合成や数値指定の書式は xcolor に従う

opticsindicatorを引けるようになる
\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ではない

繰り返し処理
pgfforのコマンドである\foreachを使う

拡張機能の詰め合わせ
うーん、まあ使うほどでもないかな

実用的な図
平面座標上に円や寸法、数式を書き込む
\picの挙動がわからん
angles (TikZ)で使うやつ?
角度の記号を出すやつ?
ほかにも描画できるっぽい?
"$\alpha$" が認識されないと怒っていたのは、quotes (TikZ)を入れれば解決する
VisualTikZをみて解決した
2024-01-22 10:07:18 \tikzset{}を使ってスタイルをまとめた
2d-rotate.tikz(tex)
\usetikzlibrary{angles,quotes}

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)
\usepackage{amsmath}
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}

条件分岐
\pgfmathparseの使い方も載っている
何故かエラーを吐くことが多くて疲れたtakker
目的の条件式を作っても計算してくれない
文法を理解していないのかも
整数と比較するときは、 int() で整数型に明示的に変換しないといけなかったようだ
プログラムあるあるだけどさあ……こんなの気づかないって。

References
滅茶苦茶長い
officical manualをLWarpでHTMLに変換したもの
animationを使ったTIkZの図もSVGに変換されててすごいtakker
原理から解説しているもの
広範囲に渡る、TikZを使ったpackagesの紹介
作例集もある
うまい書き方の模索も少し載っている
Beamer向け解説
有向グラフの作り方
\tikzで数式にマーカーを引く
ページ末尾に基礎的な解説あり
原理的な話はない

事例集
topicごとに見れる
平面幾何問題の図

#2024-12-19 15:16:33
#2024-10-07 00:03:24
#2024-06-03 23:21:59
#2024-03-13 18:56:13
#2024-02-01 22:47:34
#2024-01-29 13:26:31
#2024-01-28 09:21:25
#2024-01-27 19:32:39
#2024-01-22 16:34:14
#2024-01-21 20:18:16
#2024-01-19 13:03:06
#2023-12-31 14:27:13
#2023-07-04 06:15:26
#2023-06-17 15:06:22
#2023-06-15 09:57:38
#2023-06-14 21:00:11
#2021-04-25 14:11:48