TikZ
LaTeXの作図package
非常に高度な図表を作図できる
install
$ tlmgr install pgf
pgf packageの一部として配布されている
構文
基本https://texwiki.texjp.org/?TikZ#oee3a550
描画環境
\begin{tikzpicture}環境内に書き込む
\tikzを使うとインラインでも書ける
$ \tikz command;
;でコマンドを区切る
コマンド中のスペース・改行は無視される
例示コードの注意点
以下、特に断りがない限りheader.texを先頭につけて実行しているものとする
TikZJaxの仕様
code:header.tex
\documentclassmargin=0pt{standalone}
\def\pgfsysdriver{pgfsys-ximera.def}
\usepackagesvgnames{xcolor}
\usepackage{tikz}
\draw [help lines] (0,0) grid (3,3);は方眼を引くコマンド
code: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.icon
どの記法がどの記法の略記なのかを知ると、記法に馴染みやすいし応用も効く
Operation (TikZ)
名前は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
https://tikz.dev/tikz-paths#sec-14.7
現在座標を端点とした円弧を描く
現在座標は中心ではなく端点になることに注意
start angle,end angle,delta angleは、どれか1つを略してもいい
全部あるときはdelta angleが無視される
radiusのかわりにx radius,y radiusを設定してもいい
楕円になる
略記法にarc(<start>:<end>:<radius>)があるが、分かりづらいので推奨されていない
to[options]
to (TikZ)
出ていくときと入ってくるときの角度を指定して描く曲線
Bezier曲線より設定しやすい
等角度で入出するときはbend left=:angleかbend right=:angleを入れる
別の角度にしたいときは、in=:angle, out=:angleで設定する
何も指定しないときはline-to operationと等価
TikZ 備忘録 | Chanomic Blog#665dc9711280f000003a0ae3
grid (:coordinate)
方眼を引く
graph:グラフを書く
\path graph
plot:函数描画
\path plot
pic:特殊な描画コマンド
\pic
やや特殊な命令
node (:name) at (:coordinate) {:node_contents}
:coordinateにテキスト:node_contentsを配置し、この座標に:nameと名付ける
:nameと:coordinateは省略可
:coordinateを省略すると現在座標になる
\node
(後述)
:<animation attribute>
要animation (TikZ)
などなど
Syntax for Path Specifications - PGF/TikZ Manualにだいたい網羅されている
\usetikzlibrary{}や\usepackage{}で追加ライブラリを取り込むことで、他のOperation (TikZ)を使える様になる
opticsなど
<defs>みたいなこともできるらしい
14.22 Interacting with the Soft Path subsystem | Syntax for Path Specifications - PGF/TikZ Manual
pathを実際に描画したり塗りつぶしたりしたいときは、[]に描画オプションを入れる
\pathだけを引いても、表示上は何も起きない。
pathを定義するだけ
drawなどの描画方法(actionと呼ぶ?)を[]に指定することで初めて見た目に反映される
[]は\path ...のどこに入れてもいいし、複数入れてもいい
ただし、drawはcircleの前に設定しないと描画されない
code:circles.tikz(tex)
\begin{document}
\begin{tikzpicture}
\pathdraw,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)};
\pathdraw,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記法の仕組みが解説されている
https://tikz.dev/tikz-paths
options(HTMLでいう属性に相当)は[]の中に書く
,区切りであり、option nameには空白を入れられることに注意
例えば\path[use as bounging box]はuse as bounging boxが一つのoptionである
TikZで四角形を書く
TikZのnode
\coordinateで座標変数を定義する
\nodeでもできるが、ラベルが描画されてしまう
また\nodeは大きさがあるため、配置が微妙に異なる
等価なコマンド
coordinate=node[coordinate]=node[shape=coordinate]
\coordinate=\path coordinate=\path node[coordinate]=\node[coordinate]
$ \coordinate (:name) at (:coordinate);
変数の意味は\nodeと同じ
座標の記法
Specifying Coordinates - PGF/TikZ Manual
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しか指定できないみたい
sec-13.2.2 | Specifying Coordinates - PGF/TikZ Manual
xyz polar
楕円座標?
よくわからないtakker.icon
xy polar
よくわからないtakker.icon
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はどちらか一方のみ指定できる
垂線の足13.3.1 Intersections of Perpendicular Lines | Specifying Coordinates - PGF/TikZ Manual
(perpendicular cs:horizontal line through={(2,1)}, vertical line through={(3,4)}
略記:(2,1 -| 3,4)もしくは(3,4 |- 2,1)
code:perpendicular.tikz(tex)
\begin{document}
\begin{tikzpicture}
\path (2,1) coordinate (A) nodebelow {A};
\path (3,2) coordinate (B) noderight {B};
\draw (A) -- (perpendicular cs:horizontal line through={(A)},vertical line through={(B)});
\drawdashed (B) -- (B |- A);
\nodebelow at (A -| B) {C};
\end{tikzpicture}
\end{document}
\usetikzlibrary{}や外部パッケージで導入する座標系
calc (TikZ)
接線tangent13.2.4 Tangent Coordinate Systems | Specifying Coordinates - PGF/TikZ Manual
任意の点から\nodeへ接線を引く
接線を引ける\nodeはshape=coordinateとshape=circleのみ
perspective (TikZ)
透視投象の座標系three point perspective(略記:ttp)
3d (tikz)
円筒座標xyz cylindrical
球座標xyz spherical
etc.
他の座標から計算
標準機能およびcalc (TikZ)を使った四則演算
2点間の中点
calcを使う場合:($(A)!0.5!(B)$)
重心座標を使う場合:(barycentric cs:A=1,B=1)
sec-13.5 | Specifying Coordinates - PGF/TikZ Manual
座標計算のネスト | TikZ 備忘録 | Chanomic Blog
【LaTeX】【tikz入門】calcライブラリの構文4選 - コードDE描画
sec-13.4 | Specifying Coordinates - PGF/TikZ Manual
https://tikz.dev/tikz-coordinates#sec-13.4.1
++で一つ前の座標を基準に動く
+で\pathの最初の座標を基準に動く
相対位置指定
基準の位置と受け手側の配置位置、2点の位置関係から決定する
標準機能
code:relative-position.tikz(tex)
\begin{document}
\begin{tikzpicture}
\fill (1,1) coordinate (A) circleradius=1pt;
\nodeabove at (A) {B};
\fill (2,1) coordinate (C) circleradius=1pt;
\nodeanchor=south at (C) {B};
\end{tikzpicture}
\end{document}
above系は、基準となる座標のどこに配置するかを決める
aboveなら基準の上に来る
above=.3cmなどで間隔を調整できる
positioning (TikZ)なしでもこの記法を使えるが、非推奨。
positioning (TikZ)を使うことが推奨されている
anchorは配置するオブジェクトの基準点を決める
defaultは多分オブジェクトによって違う
標準機能だけで相対指定する
code:relative-position2.tikz(tex)
\begin{document}
\begin{tikzpicture}
\tikzset{ball/.style={draw, circle},indicator/.style={thick,<->}};
\def\l{2cm}
\nodeball (A) at (0,0) {A};
\nodeball (B) at (0,\l) {B};
\drawindicator (A) --nodeauto,swap{edge} (B);
\drawindicator (A.west) --nodeauto{\l} (B.west);
\nodeball,right=\l/2 (C) at (A) {C};
\nodeball,above=\l at (C) (D) {D};
\drawindicator (C.center) --nodeauto,swap{\l} (D);
\nodeball,right=\l/2 (E) at (C) {E};
\nodeball,above=\l at (E.center) (F) {F};
\drawindicator (E.center) --nodeauto,swap{\l} (F);
\nodeball,right=\l/2 (G) at (E) {G};
\nodeball,above=\l,anchor=south at (G.north) (H) {H};
\drawindicator (G) --nodeauto,swap{\l} (H);
\nodeball,right=\l/2 (I) at (G) {I};
\nodeball,above=\l,anchor=center at (I.center) (J) {J};
\drawindicator (I.east) --nodeauto{\l} (J.east);
\drawdashed (B.north) -- (J.north);
\end{tikzpicture}
\end{document}
ある程度はpositioning (TikZ)なしで設定できるっぽいな
TikZ の使い方 (圏論編) p.16にわかりやすい図がある
positioning (TikZ)を使うとより凝った指定ができる?
sec-17.5.3 | Nodes and Edges - PGF/TikZ Manual
nodeの配置 | PGF/TikZをオススメする記事 #LaTeX - Qiita
VisualTikZの7.7 Node labelsも参照
intersection (TikZ)
交点に設定
sec-13.3.2 | Specifying Coordinates - PGF/TikZ Manual
矢印
svg:marker
stealth-で線の始端に矢印
-stealthで線の終端に矢印
stealth-stealthで線の両端に矢印
矢印はstealth以外にもいろいろ選べる
VisualTikZやTikZ の使い方 (圏論編)の9 Arrow Tipsにある
graphs (tikz)
code: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
\tikzset{}でglobalに定義するhttps://tikz.dev/tikz-scopes#sec-12.4
改行可能
\tikzstyle{}というコマンドもあるが非推奨https://tasusu.github.io/tikz.html#id4
スタイルには引数を設定できる
#1, #2, ...が変数名
1変数までは設定無しに使用可能
2変数以上使いたいときは、/.styleを/.style 2 argsに置き換える
pgf./handlers/.style:2:args | Key Management - PGF/TikZ Manual
2は使いたい変数の数を入れる
スタイルの適用範囲を制限・一括適用するのに\begin{scope}を使う
12.3 Using Scopes to Structure a Picture | Hierarchical Structures: Package, Environments, Scopes, and Styles - PGF/TikZ Manual
scope (TikZ)を使うと略記できるが、正直いらないと思う
名前には全角文字も使えるhttps://qiita.com/seekworser/items/0ef417ab788e0786d59a#node
計算
実数の計算はdefaultで可能
座標内で括弧()を使った計算式を書くときは、計算式全体を{}で囲む
例
$ \draw (1, 1) -- ({(3 + 4) / 2}, -5 / 2)
urvanov-syntax-highlighter-64a2f8cc08512973551151 | LaTeXで図を直接描けるTikZの使い方3|グラフの描き方 – あーるえぬ
それ以外は囲む必要なし
詳細:TikZ 備忘録 | Chanomic Blog#665dc9711280f000003a0af8
座標同士の計算をするときはcalc (TikZ)を読み込む
calc (TikZ)はonlyamsmathと干渉するので注意
Undefined control sequence.\next ->\@nilが発生する
https://tex.stackexchange.com/questions/99526/bug-in-pgfplots-or-other-packages
対策として、onlyamsmathのoptionにnodollardollarを入れる
from onlyamsmathのmanualのAcknowledgement
回転させたラベルをつける
例:中央まわり90°回転
$ node left, rotate=90, anchor=south
nodeのanchorがdefaultでeastなので、文字列の真ん中に来るようanchor=southを指定する必要がある
TikZ環境全体の回転拡大縮小
https://texwiki.texjp.org/?TikZ#oee3a550
\begin{tikzpicture}のscale及びrotate optionsで指定できるが、線幅や文字の大きさ・配置は変化しない
\begin{tikzpicture} \end{tikzpicture}全体を\scalebox{}{}や\rotatebox{}{}で囲むと、文字等も含めて回転させられる
\resizebox{}{}{}を使うと、横幅だけでなく縦幅の倍率も変えられる
https://medemanabu.net/latex/scalebox-resizebox/
色
xcolorにある色をdefaultで使える
色合成や数値指定の書式はxcolorに従う
opticsでindicatorを引けるようになる
\usetikzlibrary{optics, calc}が必要
code:tex
\draw ultra thick (1,1) -- (1,2) -- (2,2) -- (2,1) -- cycle;
\draw (1,2) todim arrow={label=2cm} (2,2);
https://gyazo.com/2e0de2c61e345eb21aef2410c2abe6d4
2024-01-29 13:25:30 曲芸。非推奨
opticsは光学レンズの図を描くためのpackage。矢印を描くためのpackageではない
繰り返し処理
pgfforのコマンドである\foreachを使う
Repeating Things: The Foreach Statement - PGF/TikZ Manual
繰り返し処理 | TikZ - TeX Wiki
tikz-ext
拡張機能の詰め合わせ
https://www.ctan.org/pkg/tikz-ext
うーん、まあ使うほどでもないかな
実用的な図
tikzの素人がつくる数学の図版 - hohei’s diary
平面座標上に円や寸法、数式を書き込む
\picの挙動がわからん
angles (TikZ)で使うやつ?
角度の記号を出すやつ?
ほかにも描画できるっぽい?
"$\alpha$"が認識されないと怒っていたのは、quotes (TikZ)を入れれば解決する
VisualTikZをみて解決した
2024-01-22 10:07:18 \tikzset{}を使ってスタイルをまとめた
code:2d-rotate.tikz(tex)
\usetikzlibrary{angles,quotes}
code: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,->}};
\drawaxis (-1,0) -- (4,0) noderight {$x$};% x軸
\drawaxis (0,-1) -- (0,4) nodebelow left {$y$};% y軸
\coordinate (O) at (0,0);
\draw (O) nodebelow left {O};% 原点
\drawdomain=-0.5:2 plot (\x, {2*\x}) noderight {$y=2x$};
\drawdomain=-1:4 plot (\x, {-2/3*\x+2}) noderight {$2x+3y=6$};
\drawdomain=-1:4 plot (\x, {3/2}) noderight {$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}
code: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);% 切り抜き
\drawthick,-> (-2,0) -- (4,0) noderight {$x$};% x軸
\drawthick,-> (0,-2) -- (0,6) nodebelow left {$y$};% y軸
\draw (0,0) nodebelow left {O};% 原点
\drawdotted (0,0) circleradius=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) noderight {$\mathrm{A}(5,12)$};% 点
\fill (B) circle (2pt) noderight {$\mathrm{B}(4,-3)$};% 点
\fill (C) circle (2pt) noderight {$\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) noderight {$\hat{\bm{a}}$};
\draw thick,-> (O)--(4/5,-3/5) noderight {$\hat{\bm{b}}$};
\drawdomain=-2:3,color=black plot (\x, {3/11*\x}) noderight {$3x-11y=0$};
\end{tikzpicture}
\end{document}
code:parabora.tikz(tex)
\usepackage{amsmath}
code:parabora.tikz(tex)
\begin{document}
\begin{tikzpicture}domain=-2:4,samples=200,>=latex,thick
% \clip (0,0) rectangle (5,5);% 切り抜き
\drawthick,-> (-2,0) -- (5,0) noderight {$x$};% x軸
\drawthick,-> (0,-1) -- (0,9) nodebelow right {$y$};% y軸
\draw (0,0) nodebelow left {O};% 原点
\drawdomain=4.2:-2.5,color=black plot (\x, {1/2*\x*\x}) noderight {$C:y=\frac{1}{2}x^2$};
\drawdomain=-0.5:4.4,color=black plot (\x, {2*\x}) noderight {$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);
\drawdashed (4,8) -- (4,0) nodebelow {4};% y軸
\draw (Q) noderight {Q};
\draw (R) nodeabove left {R};
\draw (P) nodeleft {P};
\draw (2,0) nodebelow {$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}
条件分岐
https://code-de-byouga.hatenablog.com/entry/tikz-loop-if#ifコマンドとpgfmathコマンドを使って複雑な条件分岐
\iffnums, \ifodd
\pgfmathparseの使い方も載っている
何故かエラーを吐くことが多くて疲れたtakker.icon
https://texwiki.texjp.org/?TeX%20のエラーメッセージ#q1246b30
目的の条件式を作っても計算してくれない
文法を理解していないのかも
整数と比較するときは、int()で整数型に明示的に変換しないといけなかったようだ
プログラムあるあるだけどさあ……こんなの気づかないって。
pgfmathのことは、Mathematical and Object-Oriented Engines - PGF/TikZ Manualにある
References
official manual
滅茶苦茶長い
PGF/TikZ Manual - Complete Online Documentation
officical manualをLWarpでHTMLに変換したもの
animationを使ったTIkZの図もSVGに変換されててすごいtakker.icon
原理から解説しているもの
TikZ の使い方 (圏論編)
TikZへの入門
TikZ 備忘録 | Chanomic Blog
私家版:PGF/TikZ を利用したパッケージ一覧 #LaTeX - Qiita
広範囲に渡る、TikZを使ったpackagesの紹介
tikz覚書
作例集もある
うまい書き方の模索も少し載っている
TikZ - TeX Wiki
PGF/TikZをオススメする記事 #LaTeX - Qiita
TikZ — Tasuku Soma's webpage
Beamer向け解説
有向グラフの作り方
\tikzで数式にマーカーを引く
ページ末尾に基礎的な解説あり
原理的な話はない
事例集
VisualTikZ
topicごとに見れる
https://tikz.net
連続体の説明に使えそうな芋みたいな図
examples
tikzの素人がつくる数学の図版 - hohei’s diary
平面幾何問題の図
#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
#2023-06-02
#2023-05-01
#2021-04-25 14:11:48