【TikZ】塗りつぶし fill
塗りつぶしfill
TeXclip○ obsidian○
\draw
と同じ要領で,\fill
を使うと塗りつぶしができます。
\begin{tikzpicture} \fill[blue] (0,0) circle (2cm); \fill[blue,xshift=2.5cm,yshift=-2cm] (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle; \end{tikzpicture}
閉じた曲線の内側を塗りつぶします。色の指定をしない場合は,黒で塗られます。
閉じていない線を書いた場合,始点と終点が結ばれて閉曲線になります。
\begin{tikzpicture} \fill[blue] (0,0) -- (5,0) -- (4,3);%閉じていない \end{tikzpicture}
オプションにdrawをつけると,縁に線をかきます。
\begin{tikzpicture} \fill[cyan,draw=black,very thick] (0,0) circle (2cm); \end{tikzpicture}
線が閉じていない場合は次のようになります。
\begin{tikzpicture} \fill[cyan,draw=black,very thick] (0,0) -- (5,0) -- (4,3);%閉じていない \end{tikzpicture}
閉曲線が複数ある場合の塗りつぶしのルール
\fill
に複数の曲線を描いた場合に,領域を塗りつぶすルールが2つあります。デフォルトとeven odd rule
オプションです。両方のオプションは,ともにwinding number(巻き数)で塗りつぶすかどうかを判断しています。このルールはpattern
オプションにも適応されます。
巻き数は,大雑把に言えば,その領域が何回閉曲線で囲われたかを表す数です。
2つの例を挙げます。左がデフォルト,右がeven odd rule
オプションです。
\begin{tikzpicture} \fill[blue] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm); \fill[blue,even odd rule,xshift=5cm] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm); \end{tikzpicture}
デフォルトは,マニュアルではthe nonzero winding number ruleと呼ばれていて,巻き数が0の領域は塗りつぶさず,それ以外で塗りつぶします。even odd rule
オプションでは,巻き数が偶数では塗りつぶさず,奇数のところを塗りつぶします。
the nonzero winding number rule
下の例は,同じ形の図形をデフォルトで塗りつぶしたものです。違いは,長方形を書くときの領域の囲い方です。左が反時計回り,右が時計回りです。
\begin{tikzpicture} \fill[blue] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm); \fill[blue,xshift=5cm] (0,0) -- (0,2) -- (2,2) -- (2,0) -- cycle (2,2) circle (0.8cm); \end{tikzpicture}
この右傾の巻き数は下のようになっています。
反時計回りで囲うと巻き数は+1,時計回りで囲うと巻数は-1されます。左の図形では,長方形,円ともに反時計回りで,2つの領域が重なる部分では$+1+1=+2$になります。一方,右の図形では,長方形が時計回りになっているので,巻き数が-1になり,円と長方形が重なる領域では$+1-1=0$となります。巻き数0の場合は塗りつぶされません。
the even odd rule
次は,上と同じものをeven odd rule
で塗りつぶしたものです。
\begin{tikzpicture} \fill[blue,even odd rule] (0,0) -- (2,0) -- (2,2) -- (0,2) -- cycle (2,2) circle (0.8cm); \fill[blue,even odd rule,xshift=5cm] (0,0) -- (0,2) -- (2,2) -- (2,0) -- cycle (2,2) circle (0.8cm); \end{tikzpicture}
巻き数が偶数の2,0の領域は塗られていません。一方で,奇数の-1,1の部分が塗られていることが分かります。
例
デフォルトのルールでrectangle
とcircle
を重ねると次のようになります。rectanle
は時計回りの囲い方になっています。
\begin{tikzpicture} \fill[blue] (0,0) rectangle (3,4) (2,2) circle (0.8cm); \end{tikzpicture}
even odd rule
を使えば,円を2つかいてドーナツの形に塗ることもできます。
\begin{tikzpicture} \fill[postaction={draw=black},blue] (2,2) circle (2cm) -- (2,2) circle (0.5cm); \fill[even odd rule,blue,xshift=5cm] (2,2) circle (2cm) -- (2,2) circle (0.5cm); \end{tikzpicture}
星型(五芒星)
\usetikzlibrary{math} \begin{tikzpicture} \tikzmath{ coordinate \A; \A = ({3*cos(90-72)},{3*sin(90-72)}); coordinate \B; \B = ({3*cos(90)},{3*sin(90)}); coordinate \C; \C = ({3*cos(90+72)},{3*sin(90+72)}); coordinate \D; \D = ({3*cos(90+144)},{3*sin(90+144)}); coordinate \E; \E = ({3*cos(90+216)},{3*sin(90+216)}); } \fill[blue,even odd rule] (\A) -- (\C) -- (\E) -- (\B) -- (\D) -- cycle; %\draw[thick] (\A) -- (\C) -- (\E) -- (\B) -- (\D) -- cycle; \end{tikzpicture}
\clip
のルール
TeXclip○ obsidian○
clipの場合,巻き数がゼロでない領域がくり抜かれる,the nonzero winding number ruleに従います。even odd rule
はありません。
四角形の内側を青で塗りつぶしています。
\begin{tikzpicture} \begin{scope} \clip (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle (4,4) circle (2cm); \fill[blue] (-1,-1) rectangle (7,7); \end{scope} \draw[thick] (-1,-1) rectangle (7,7); \end{tikzpicture}
\begin{tikzpicture} \begin{scope} \clip (0,0) -- (0,4) -- (4,4) -- (4,0) -- cycle (4,4) circle (2cm); \fill[blue] (-1,-1) rectangle (7,7); \end{scope} \draw[thick] (-1,-1) rectangle (7,7); \end{tikzpicture}
TikZでベン図をかく
※patternsライブラリを使用しています。 TeXclip○ obsidian×(patternsライブラリを使用したもの)
下のようなベン図をかくには工夫が必要です。\clip
を使わない場合は,白で塗る工程を入れます。背景を入れたい場合は\clip
を使った方がいいです。
白の塗りつぶしを入れたもの。
\usetikzlibrary{patterns} \begin{tikzpicture} \fill[pattern= north west lines] (0,0) circle (2cm) (2,0) circle (1.8cm); \fill[even odd rule,white] (0,0) circle (2cm) (2,0) circle (1.8cm); \draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm); \end{tikzpicture}
\clip
を使ったもの。
\usetikzlibrary{patterns} \begin{tikzpicture} \begin{scope} \clip (2,0) circle (1.8cm); \fill[pattern= north west lines] (0,0) circle (2cm); \end{scope} \draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm); \end{tikzpicture}
\usetikzlibrary{patterns} \begin{tikzpicture} \fill[even odd rule,pattern=north west lines] (0,0) circle (2cm) (2,0) circle (1.8cm); \draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm); \end{tikzpicture}
\usetikzlibrary{patterns} \begin{tikzpicture} \begin{scope} \clip (2,0) circle (1.8cm); \fill[pattern=north west lines,even odd rule] (0,0) circle (2cm) (2,0) circle (1.8cm); \end{scope} \draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm); \end{tikzpicture}
\usetikzlibrary{patterns} \begin{tikzpicture} \begin{scope} \clip (-3,-3) -- (-3,3) -- (5,3) -- (5,-3) -- cycle (0,0) circle (2cm); \clip (-3,-3) -- (-3,3) -- (5,3) -- (5,-3) -- cycle (2,0) circle (1.8cm); \fill[pattern=north west lines,even odd rule] (-3,-3) -- (5,-3) -- (5,3) -- (-3,3) -- cycle; \end{scope} \draw[thick] (0,0) circle (2cm) (2,0) circle (1.8cm); \draw[thick] (-3,-3) -- (5,-3) -- (5,3) -- (-3,3) -- cycle; \end{tikzpicture}
\usetikzlibrary{math,patterns} \begin{tikzpicture} \tikzmath{ \radius = 2; } \begin{scope} \clip plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}); \clip plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))}); \fill[pattern=north west lines,even odd rule] plot[samples=50,domain=360:0] ({\radius*cos(\x)},{\radius*sin(\x)}) plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))}); \end{scope} \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}); \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*cos(\x)},{\radius*sin(\x)}); \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))}); \end{tikzpicture}
\usetikzlibrary{math,patterns} \begin{tikzpicture} \tikzmath{ \radius = 2; } \begin{scope} \clip plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}) plot[samples=50,domain=360:0] ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))}); \clip plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}) plot[samples=50,domain=360:0] ({\radius*cos(\x)},{\radius*sin(\x)}); \fill[pattern=north west lines] plot[samples=50,domain=0:360] ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}); \end{scope} \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(60))},{\radius*(sin(\x)+sin(60))}); \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*cos(\x)},{\radius*sin(\x)}); \draw[thick,samples=50,smooth,domain=0:360] plot ({\radius*(cos(\x)+cos(0))},{\radius*(sin(\x)+sin(0))}); \end{tikzpicture}
【TikZ】nodeの使い方
点にテキストを入れたり,点に名前を付けたいときに用います。
使い方は
\node at (座標) {};
\path (座標) node {};
の代替命令になっています。
以下のようにすると,点にテキストOを入れることができます。
\begin{tikzpicture} \fill (0,0) circle (2pt); \node[below left] at (0,0) {O}; \end{tikzpicture}
また,次のようにすると
\begin{tikzpicture} \fill (0,0) circle (2pt); \node[below left](origin) at (0,0) {O}; \node[red] at (origin) {A}; \end{tikzpicture}
OとAが重なります。はじめの\node
でOが置いてある点に(origin)
という名前を付けることができたからです。
このページに使用されている画像はTikZjaxを用いたものです。ここで表示されている画像は,TikZjaxを用いてはてなブログで表示することだできます。
※オンラインで使用するTikZjaxはライブラリが使用できません。また,plot
やsin()
, cos()
などの関数も使用できません。
オプション
いろいろなオプションがあります。
テキストの位置:above
below
left
right
フォントサイズ:font
塗りつぶし:fill
(デフォルトでは無色透明)
形の指定がなければ,矩形で塗られる。
draw
形状:shape
(rectangle
circle
coordinate
)
幅:text width
高さ:text height
最小幅:minimum width
最小高さ:mnimum height
nodeに対する余白:inner sep
テキストの位置
点に対するテキストの位置をanchor=
オプションで指定できます。above
, below
, left
, right
と,その組み合わせabove left
, above right
, below left
, below right
で指定できます。anchor=
は省略できます。
\begin{tikzpicture} \fill (-2,0) circle (2pt); \node at (-2,0) {nothing}; \fill (0,0) circle (2pt); \node[above] at (0,0) {above}; \fill (2,0) circle (2pt); \node[below] at (2,0) {below}; \fill (4,0) circle (2pt); \node[left] at (4,0) {left}; \fill (6,0) circle (2pt); \node[right] at (6,0) {right}; \fill (8,0) circle (2pt); \node[above right] at (8,0) {above right}; \fill (10,0) circle (2pt); \node[below right] at (10,0) {below right}; \end{tikzpicture}
また,点からの離れ具合を手動で指定することもできます。
\begin{tikzpicture} \fill (-2,0) circle (2pt); \node[above right=2pt] at (-2,0) {2pt}; \fill (0,0) circle (2pt); \node[above right=15pt] at (0,0) {15pt}; \end{tikzpicture}
font
フォントのサイズを指定します。font=フォントサイズ
と記述し,TeXのフォントサイズ,\scriptsize
, \footnotesize
, \small
, \lage
, \Large
, \LARGE
, \huge
, \Huge
から選べます。
\begin{tikzpicture} \node[above,font=\scriptsize] at (-2,0) {\verb|\scriptsize|}; \node[above,font=\footnotesize] at (0,0) {\verb|\footnotesize|}; \node[above,font=\small] at (2,0) {\verb|\small|}; \node[above] at (4,0) {plain}; \node[above,font=\large] at (6,0) {\verb|\large|}; \node[above,font=\Large] at (8,0) {\verb|\Large|}; \node[above,font=\LARGE] at (10,0) {\verb|\LARGE|}; \node[above,font=\huge] at (12,0) {\verb|\huge|}; \node[above,font=\Huge] at (14,0) {\verb|\Huge|}; \end{tikzpicture}
fill
塗りつぶし
\fill
のように,\node
にもfill
のオプションがあります。\node[fill=色]
で指定します。
\begin{tikzpicture} \fill (-2,0) circle (2pt); \node[above,fill=cyan] at (-2,0) {fill}; \fill (0,0) circle (2pt); \node[above,fill=cyan] at (0,0) {fill cyan}; \end{tikzpicture}
同じfill=cyan
ですが,テキストに合わせて塗りつぶされる領域が変わっています。また,形状を指定することで塗りつぶされる領域も変化します。
形状 shape
\node[shape=色]
で指定します。shape=
は省略できます。ライブラリを使用しない場合,
rectangle
circle
coordinate
の3つからえらべます。デフォルトではrectangleが指定されます。
\begin{tikzpicture} \fill (-2,0) circle (2pt); \node[fill=blue,below] at (-2,0) {rectangle}; \fill (0,0) circle (2pt); \node[fill=blue,below,shape=circle] at (0,0) {circle}; \fill (2,0) circle (2pt); \node[above] at (2,0) {(cooridnate)}; \node[fill=blue,below,shape=coordinate] at (2,0) {cooridnate}; \end{tikzpicture}
coordinate
は大きさが無い点という形状です。塗りつぶしがありませんが,テキストも表示されません。
ライブラリを使えば多角形などの形にすることもできます。
draw
nodeの外枠の線を書きます。
\begin{tikzpicture} \node[draw=blue,rectangle] at (-2,0) {rectangle}; \node[draw=blue,circle] at (0,0) {circle}; \node[draw=blue,coordinate] at (2,0) {cooridnate}; \end{tikzpicture}
幅,高さ,深さ
text width
, text height
,text depth
\begin{tikzpicture} \node[draw=blue] at (-2,0) {width}; \node[draw=blue,text width=1.5cm] at (0,0) {width}; \node[draw=blue] at (-2,-2) {height}; \node[draw=blue,text height=1.5cm] at (0,-2) {height}; \node[draw=blue] at (-2,-4) {depth}; \node[draw=blue,text depth=1.5cm] at (0,-4) {depth}; \end{tikzpicture}
最低幅minimum width
, 最低高さminimum height
\begin{tikzpicture} \node[draw=blue] at (-2,0) {width}; \node[draw=blue,minimum width=1.5cm] at (0,0) {width}; \node[draw=blue] at (-2,-2) {height}; \node[draw=blue,minimum height=1.5cm] at (0,-2) {height}; \end{tikzpicture}
text width
などと違い,中央にテキストが設置されます。
inner sep テキストに対する余白
次の3つを比較します。
\begin{tikzpicture} \fill (0,0) circle (2pt); \node[below] at (0,0) {O}; \fill (2,0) circle (2pt); \node[below,inner sep=10pt] at (2,0) {O}; \fill (4,0) circle (2pt); \node[below,inner sep=10pt] at (2,0) {ABC}; \end{tikzpicture}
テキストが点から離れている距離が違います。fill
で塗りつぶすとよくわかります。
nodeの形とテキストの余白が異なっていることが分かります。fill
の指定がない場合,色が透明になりますが,形が反映された配置になります。
nodeの形状の意味
nodeを線でつないだ場合
次のように,nodeに名前を付けて,線を引きます。
\begin{tikzpicture} \node[below,fill=blue,inner sep=10pt](O) at (0,0) {O}; \node[below,fill=blue,inner sep=10pt](A) at (2,0) {O}; \draw[very thick,->] (O)--(A); \end{tikzpicture}
nodeの中心から中心ではなく,縁から縁に伸びた線がかかれます。
nodeで決められる座標
nodeには,nodeの中央のほかに上下左右と四隅に座標が設定されます。nodeの名前を(O)と決めた場合,(O)はnodeの中央,(O.north)は(O)の上側の端という風になります。
\begin{tikzpicture} \node[below,fill=pink,inner sep=20pt](O) at (0,0) {O}; \fill (O) circle (2pt); \fill (O.north) circle (2pt); \node[above] at (O.north) {.north}; \fill (O.west) circle (2pt); \node[left] at (O.west) {.west}; \fill (O.south) circle (2pt); \node[below] at (O.south) {.south}; \fill (O.east) circle (2pt); \node[right] at (O.east) {.east}; \end{tikzpicture}
\begin{tikzpicture} \node[below,fill=pink,inner sep=20pt](O) at (0,0) {O}; \fill (O.north west) circle (2pt); \node[above left] at (O.north west) {.north west}; \fill (O.south west) circle (2pt); \node[below left] at (O.south west) {.south west}; \fill (O.south east) circle (2pt); \node[below right] at (O.south east) {.south east}; \fill (O.north east) circle (2pt); \node[above right] at (O.north east) {.north east}; \end{tikzpicture}
nodeの形状を変えると,
このように形状にそって点の位置が変わります。
曲線の点,中点のnode
(座標) node {}
\begin{tikzpicture} \draw (0,0) node[above](O) {O} -- (5,0) node[above](A){A}; \end{tikzpicture}
node
の直後の ( )
でnodeに名前を付けることができます。
\draw
やpath
で2点を結んで,`node[midway] で中点にnodeを入れることができます。
\begin{tikzpicture} \fill (0,0) circle (2pt); \fill (5,0) circle (2pt); \draw (0,0)--node[midway] {M}(5,0) ; \end{tikzpicture}
above
などと併用することができます。
線で結びたくないときは\path
を使います。
\begin{tikzpicture} \fill (0,0) circle (2pt); \fill (5,0) circle (2pt); \path (0,0) --node[midway] {M} (5,0) ; \end{tikzpicture}
内分点,外分点にnode pos
線分上または,その延長上にnodeを書くpos
というオプションがあります。pos=数値
のように,必ず数値を伴います。
\begin{tikzpicture} \coordinate (A) at (0,0); \coordinate (B) at (5,0); \fill (A) circle (2pt); \fill (B) circle (2pt); \draw (A)node[left]{0.2} -- (B)node[pos=0.2] {pos} ; \coordinate[yshift =-1.5cm] (A) at (A); \coordinate[yshift =-1.5cm] (B) at (B); \fill (A) circle (2pt); \fill (B) circle (2pt); \draw (A)node[left]{0.5} -- (B)node[pos=0.5] {pos} ; \coordinate[yshift =-1.5cm] (A) at (A); \coordinate[yshift =-1.5cm] (B) at (B); \fill (A) circle (2pt); \fill (B) circle (2pt); \draw (A)node[left]{1.1} -- (B)node[pos=1.1] {pos} ; \coordinate[yshift =-1.5cm] (A) at (A); \coordinate[yshift =-1.5cm] (B) at (B); \fill (A) circle (2pt); \fill (B) circle (2pt); \draw (A)node[above]{-0.1} -- (B)node[pos=-0.1] {pos} ; \end{document}
pos=0.5
はmidway
と同じです。
点にnode[pos=]
を指定すると,点の上にnodeができます。
つるかめ算の解き方(面積図)
TikZを使って図を書きながら,解説を作ってみました。
つるかめ算とは,簡単に言えば次のような連立方程式を解く問題です。
$$ \begin{eqnarray} x + y &=& M\\ ax + by &=& N \end{eqnarray} $$
面積図による解法
例えば,次のような問題を解いてみます。
50円切手と80円切手を合わせて12枚買うと,代金の合計は810円になりました。80円切手は何枚買いましたか。
問題文を式で表すと $$ \begin{array}{rcl} \bigcirc + \triangle &=& 12 \\ 50 \times \bigcirc + 80 \times \triangle &=& 810 \end{array} $$ となります。小学生向きに書きました。
次のように面積図を導入します。例えば,50円の切手10枚を表す面積図は
今,50円切手の枚数は $\bigcirc$ 枚と分かっていません。80円切手の枚数も同じです。とりあえず,この2つの切手の面積図は
このようになります。この2つの面積図をくっつけます。
このような形になり,下の辺の長さが切手の総数なので12,面積が代金の合計で810です。この図形の長さ$\triangle$を計算すれば答えが得られます。
解法
下の図のように,まず横の補助線を引きます。赤より下側の面積は,タテ×ヨコで計算でき,600になります。
赤線の上側の長方形に注目します。面積は210です。
タテの長さは$80-50=30$なので, $$ \triangle = 210 \div 30 = 7 $$ 80円切手は7枚と分かりました。
【TikZ】線のかき方(draw)
このページに使用されている画像はTikZjaxを用いたものです。ここで表示されている画像は,TikZjaxを用いてはてなブログで表示することだできます。
※オンラインで使用するTikZjaxはライブラリが使用できません。また,plot
やsin()
, cos()
などの関数も使用できません。
\draw
線をかくには次のように\path[draw]
と\draw
を用いた書き方があります。
下の2つは,ともに同じ図形をかきます。
\begin{tikzpicture} \draw (0,0)--(3,2); \end{tikzpicture}
\begin{tikzpicture} \path[draw] (0,0)--(3,2); \end{tikzpicture}
\draw
は\path[draw]
を簡略化した命令です。以下では,\draw
コマンドを用います。
2つの点を--
でつなぐと,直線がかけます。
3点以上がつながった線
\begin{tikzpicture} \path[draw] (0,0)--(3,2)--(4,0)--(5,4); \end{document}
--
でつながない場合
\begin{document} \begin{tikzpicture} \path[draw] (0,0)--(3,2) (4,0)--(5,4); \end{tikzpicture} \end{document}
閉じた線 cycle
を用いると,始点と終点を結びます。
\begin{document} \begin{tikzpicture} \path[draw] (0,0)--(3,2) -- (4,0)--(5,4) --cycle; \end{tikzpicture} \end{document}
あくまで連結している点の始点と終点です。
\begin{document} \begin{tikzpicture} \path[draw] (0,0)--(3,2) (4,0)--(5,4) --cycle; \end{tikzpicture} \end{document}
--
の部分を変えることで,いろいろな図形に変わります。
rectangle
長方形
対角の頂点を指定することで長方形を書きます。
\begin{tikzpicture} \draw[very thick] (0,0) rectangle (6,4); \end{tikzpicture}
grid
rectangle
と同じく,対角の頂点を指定することで長方形を書き,その中にグリッドをかきます。
\begin{tikzpicture} \draw[very thick] (0,0) grid (6,4); \end{tikzpicture}
grid
の幅を変えるオプションがあります。step
, xstep
, ystep
で指定することができます。
step=0.5
\begin{tikzpicture} \draw[very thick,step=.5] (0,0) grid (6,4); \end{tikzpicture}
xstep=0.5
\begin{tikzpicture} \draw[very thick,xstep=.5] (0,0) grid (6,4); \end{tikzpicture}
xstep=0.5,ystep=0.7
\begin{tikzpicture} \draw[very thick,xstep=.5,ystep=.7] (0,0) grid (6,4); \end{tikzpicture}
デフォルトの状態では,座標が整数値のグリッド線がかかれます。
\begin{tikzpicture} \draw[very thick] (-0.5,-0.5) grid (6.5,4.5); \fill (0,0) circle (2pt); \node[above right] at (0,0){(0,0)}; \end{tikzpicture}
step
で幅を調整した場合は,必ず原点を通り,step
の整数倍の位置にグリッド線がかかれます。
\draw
のオプションhelp lines
を入れることで,よりグリッド感が増します。
\begin{tikzpicture} \draw[very thick,help lines] (0,0) grid (6,4); \end{tikzpicture}
タテ・ヨコの線 |-
タテ・ヨコ
\begin{tikzpicture} \draw[very thick] (0,0) |- (6,2); \end{tikzpicture}
ヨコ・タテ
\begin{tikzpicture} \draw[very thick] (0,0) -| (6,2); \end{tikzpicture}
円 cicle
(座標) cicrcle (半径)
と書きます。(座標)
は円の中心になります。
\begin{tikzpicture} \draw[very thick] (0,0) circle (3cm); \end{tikzpicture}
楕円
cicrcle
とおなじ要領で書きます。楕円の場合は半径を2つ指定します。
\begin{tikzpicture} \draw[very thick] (3,0) ellipse [x radius = 2, y radius = 1]; \draw[very thick] (8,0) ellipse [x radius = 2, y radius = 1,rotate=35]; \draw[very thick] (12,0) circle [x radius = 2, y radius = 1, rotate = 35]; \end{tikzpicture}
楕円はcircle
でもかくことができます。半径を2つ指定することで楕円になります。次の文は同じ図形になります。
\begin{tikzpicture} \draw[very thick] (3,0) circle [x radius = 2, y radius = 1]; \draw[very thick] (8,0) circle (2 and 1); \end{tikzpicture}
少し曲げるto
線を少し曲げたい場合は,--
のかわりにto
を用いて,オプションに[bend left]
または,[bend right]
をつけます。
\begin{tikzpicture} \draw[very thick,dashed] (0,0) -- (5,0); \draw[very thick] (0,0) to[bend left] (5,0); \node[below right] at (0,0) {bend left}; \draw[very thick,dashed] (6,0) -- (11,0); \draw[very thick] (6,0) to[bend right] (11,0); \node[above right] at (6,0) {bend right}; \end{tikzpicture}
bend left
は,線の進行方向に足して左よりに曲がり,bend right
はその逆です。
曲がりぐあい(直線からの離れ具合)を数値で指定できます。
\begin{tikzpicture} \draw[very thick,dashed] (0,0) -- (5,0); \draw[very thick] (0,0) to[bend left = 15] (5,0); \node[below right] at (0,0) {bend left = 15}; \draw[very thick,dashed] (6,0) -- (11,0); \draw[very thick] (6,0) to[bend left=25] (11,0); \node[below right] at (6,0) {bend left=25}; \end{tikzpicture}
\draw
のオプション
太さ:line width
色:color
透明度:opacity
形状
矢印:arrows
角:rounded corners
線の端点の形状:line cap
結合部分の形状:line join
などが設定できます。
\draw[オプション]
と[ ]
で指定します。複数のオプションを指定する場合は,
\draw[オプション,オプション,...]
のように『 ,
』を用いて区切ります。
太さ
\begin{tikzpicture} \draw[ultra thin] (0,3) -- (5,3) node[right] {ultra thin}; \draw[very thin] (0,2) -- (5,2) node[right] {very thin}; \draw[thin] (0,1) -- (5,1) node[right] {thin}; \draw (0,0) -- (5,0) node[right] {plain}; \draw[thick] (0,-1) -- (5,-1) node[right] {thick}; \draw[very thick] (0,-2) -- (5,-2) node[right] {very thick}; \draw[ultra thick] (0,-3) -- (5,-3) node[right] {ultra thick}; \end{tikzpicture}
手動で太さを設定したい場合は次のように書きます。
\begin{tikzpicture} \draw[line width=2pt] (0,3) -- (5,3) node[right] {2pt}; \draw[line width=4pt] (0,2) -- (5,2) node[right] {4pt}; \draw[line width=10pt] (0,1) -- (5,1) node[right] {10pt}; \end{tikzpicture}
色
線の色を設定できます。ここであげた色の設定は,塗りつぶしの\fill
でも使用できます。color=`は省略できます。
\begin{tikzpicture} \draw[color=blue] (0,1) -- (5,1); \end{tikzpicture}
使用できる色です。
\begin{document} \begin{tikzpicture} \draw[line width=4pt,red] (0,1) -- (5,1) node[right] {red}; \draw[line width=4pt,blue] (0,0) -- (5,0) node[right] {blue}; \draw[line width=4pt,green] (0,-1) -- (5,-1) node[right] {green}; \draw[line width=4pt,yellow] (0,-2) -- (5,-2) node[right] {yellow}; \draw[line width=4pt,magenta] (0,-3) -- (5,-3) node[right] {magenta}; \draw[line width=4pt,cyan] (0,-4) -- (5,-4) node[right] {cyan}; \draw[line width=4pt,gray] (0,-5) -- (5,-5) node[right] {black}; \draw[line width=4pt,white] (0,-6) -- (5,-6) node[black,right] {white}; \draw[line width=4pt,gray] (0,-7) -- (5,-7) node[right] {gray}; \draw[line width=4pt,darkgray] (0,-8) -- (5,-8) node[right] {darkgray}; \draw[line width=4pt,lightgray] (0,-9) -- (5,-9) node[right] {lightgray}; \draw[line width=4pt,olive] (0,-10) -- (5,-10) node[right] {olive}; \draw[line width=4pt,brown] (0,-11) -- (5,-11) node[right] {brown}; \draw[line width=4pt,orange] (0,-12) -- (5,-12) node[right] {orange}; \draw[line width=4pt,lime] (0,-13) -- (5,-13) node[right] {lime}; \draw[line width=4pt,teal] (0,-14) -- (5,-14) node[right] {teal}; \draw[line width=4pt,violet] (0,-15) -- (5,-15) node[right] {violet}; \draw[line width=4pt,purple] (0,-16) -- (5,-16) node[right] {purple}; \draw[line width=4pt,pink] (0,-17) -- (5,-17) node[right] {pink}; \end{tikzpicture} \end{document}
色を混ぜる
blue!数値!green
とすると,左の色を数値のパーセントで
\begin{tikzpicture} \node[left] at (0,1){blue!100!green}; \draw[line width=4pt,blue!100!green] (0,1) -- (5,1) node[black,right] {blue100\%, green0\%}; \node[left] at (0,0){blue!90!green}; \draw[line width=4pt,blue!90!green] (0,0) -- (5,0) node[black,right] {blue90\%, green10\%}; \node[left] at (0,-1){blue!80!green}; \draw[line width=4pt,blue!80!green] (0,-1) -- (5,-1) node[black,right] {blue80\%, green0\%}; \node[left] at (0,-2){blue!70!green}; \draw[line width=4pt,blue!70!green] (0,-2) -- (5,-2) node[black,right] {blue70\%, green0\%}; \node[left] at (0,-3){blue!60!green}; \draw[line width=4pt,blue!60!green] (0,-3) -- (5,-3) node[black,right] {blue60\%, green0\%}; \node[left] at (0,-4){blue!50!green}; \draw[line width=4pt,blue!50!green] (0,-4) -- (5,-4) node[black,right] {blue50\%, green0\%}; \node[left] at (0,-5){blue!40!green}; \draw[line width=4pt,blue!40!green] (0,-5) -- (5,-5) node[black,right] {blue40\%, green0\%}; \node[left] at (0,-6){blue!30!green}; \draw[line width=4pt,blue!30!green] (0,-6) -- (5,-6) node[black,right] {blue30\%, green0\%}; \node[left] at (0,-7){blue!20!green}; \draw[line width=4pt,blue!20!green] (0,-7) -- (5,-7) node[black,right] {blue20\%, green0\%}; \node[left] at (0,-8){blue!10!green}; \draw[line width=4pt,blue!10!green] (0,-8) -- (5,-8) node[black,right] {blue10\%, green0\%}; \node[left] at (0,-9){blue!0!green}; \draw[line width=4pt,blue!0!green] (0,-9) -- (5,-9) node[black,right] {blue0\%, green0\%}; \end{tikzpicture}
透明度
opacity
で透明度(不透明度)を指定することができます。0~1までの実数を用いて表します。1:不透明,0:透明を表す。透明なので,下にある青が見えています。
\begin{tikzpicture} \fill[blue] (2.5,1.5) rectangle (5.5,-9.5); \node[left] at (0,1){opacity=1}; \draw[line width=4pt,opacity=1] (0,1) -- (5,1); \node[left] at (0,0){opacity=0.9}; \draw[line width=4pt,opacity=0.9] (0,0) -- (5,0); \node[left] at (0,-1){opacity=0.8}; \draw[line width=4pt,opacity=0.8] (0,-1) -- (5,-1); \node[left] at (0,-2){opacity=0.7}; \draw[line width=4pt,opacity=0.7] (0,-2) -- (5,-2); \node[left] at (0,-3){opacity=0.6}; \draw[line width=4pt,opacity=0.6] (0,-3) -- (5,-3); \node[left] at (0,-4){opacity=0.5}; \draw[line width=4pt,opacity=0.5] (0,-4) -- (5,-4); \node[left] at (0,-5){opacity=0.4}; \draw[line width=4pt,opacity=0.4] (0,-5) -- (5,-5); \node[left] at (0,-6){opacity=0.3}; \draw[line width=4pt,opacity=0.3] (0,-6) -- (5,-6); \node[left] at (0,-7){opacity=0.2}; \draw[line width=4pt,opacity=0.2] (0,-7) -- (5,-7); \node[left] at (0,-8){opacity=0.1}; \draw[line width=4pt,opacity=0.1] (0,-8) -- (5,-8); \node[left] at (0,-9){opacity=0}; \draw[line width=4pt,opacity=0] (0,-9) -- (5,-9); \end{tikzpicture}
線の種類
線の種類は以下の通りです。デフォルトではsolid
に設定されています。
\begin{tikzpicture} \draw[thick,solid] (0,2) -- (5,2) node[right] {solid}; \draw[thick,dashed] (0,1) -- (5,1) node[right] {dashed}; \draw[thick,dotted] (0,0) -- (5,0) node[right] {dotted}; \draw[thick,dash dot] (0,-1) -- (5,-1) node[right] {dash dot}; \draw[thick,dash dot dot] (0,-2) -- (5,-2) node[right] {dash dot dot}; \draw[thick,dash dot dot] (0,-3) -- (5,-3) node[right] {dash dot dot dot}; \draw[thick,double] (0,-4) -- (5,-4) node[right] {double}; \end{tikzpicture}
dot
を4つ以上つけるとエラーが出ます。
loosely
, densely
で間隔が調整できます。
\begin{tikzpicture} \draw[thick,loosely dashed] (0,1) -- (5,1) node[right] {loosely dashed}; \draw[thick,dashed] (0,0) -- (5,0) node[right] {dashed}; \draw[thick,densely dashed] (0,-1) -- (5,-1) node[right] {densely dashed}; \end{tikzpicture}
\begin{tikzpicture} \draw[thick,loosely dotted] (0,1) -- (5,1) node[right] {loosely dotted}; \draw[thick,dotted] (0,0) -- (5,0) node[right] {dotted}; \draw[thick,densely dotted] (0,-1) -- (5,-1) node[right] {densely dotted}; \end{tikzpicture}
\begin{tikzpicture} \draw[thick,loosely dash dot] (0,1) -- (5,1) node[right] {loosely dash dot}; \draw[thick,dash dot] (0,0) -- (5,0) node[right] {dash dot}; \draw[thick,densely dash dot] (0,-1) -- (5,-1) node[right] {densely dash dot}; \end{tikzpicture}
矢印 端点の形状arrows
atrrows=->
のようにすると矢印がかけます。arrows
は省略できます。始点と終点に矢印をつけることができます。
線の太さによる違い
\begin{tikzpicture} \draw[arrows=->] (0,1) -- (5,1) node[right] {\verb|->|}; \draw[thick, ->] (0,0) -- (5,0) node[right] {\verb|->|}; \draw[very thick,->] (0,-1) -- (5,-1) node[right]{\verb|->|}; \draw[ultra thick,->] (0,-2) -- (5,-2)node[right]{\verb|->|}; \end{tikzpicture}
形状 normal
\begin{tikzpicture} \draw[very thick,->] (0,1) -- (5,1) node[right] {\verb|->|}; \draw[very thick, <-] (0,0) -- (5,0) node[right] {\verb|<-|}; \draw[very thick,<->] (0,-1) -- (5,-1) node[right]{\verb|<->|}; \end{tikzpicture}
2重
\begin{tikzpicture} \draw[very thick,->>] (0,1) -- (5,1) node[right] {\verb|->>|}; \draw[very thick, <<-] (0,0) -- (5,0) node[right] {\verb|<<-|}; \draw[very thick,<<->>] (0,-1) -- (5,-1) node[right]{\verb|<<->>|}; \end{tikzpicture}
逆向き
\begin{tikzpicture} \draw[very thick,-<] (0,1) -- (5,1) node[right] {\verb|-<|}; \draw[very thick, >-] (0,0) -- (5,0) node[right] {\verb|>-|}; \draw[very thick,>-<] (0,-1) -- (5,-1) node[right]{\verb|>-<|}; \end{tikzpicture}
\begin{tikzpicture} \draw[very thick,-<<] (0,1) -- (5,1) node[right] {\verb|-<<|}; \draw[very thick, >>-] (0,0) -- (5,0) node[right] {\verb|>>-|}; \draw[very thick,>>-<<] (0,-1) -- (5,-1) node[right]{\verb|>>-<<|}; \end{tikzpicture}
\begin{tikzpicture} \draw[very thick,-latex] (0,1) -- (5,1) node[right] {\verb|-latex|}; \draw[very thick, latex-] (0,0) -- (5,0) node[right] {\verb|latex-|}; \draw[very thick,latex-latex] (0,-1) -- (5,-1) node[right]{\verb|latex-latex|}; \end{tikzpicture}
stealth
\begin{tikzpicture} \draw[very thick,-stealth] (0,1) -- (5,1) node[right] {\verb|-stealth|}; \draw[very thick, stealth-] (0,0) -- (5,0) node[right] {\verb|stealth-|}; \draw[very thick,stealth-stealth] (0,-1) -- (5,-1) node[right]{\verb|stealth-stealth|}; \end{tikzpicture}
矢印の先端にはオプションで色を付けることができます。
オプションに書くとき,{latex[red]}
のように中カッコをつける必要があります。
\begin{tikzpicture} \draw[very thick,-{>[red]}] (0,1) -- (5,1) node[right] {\verb|-{>[red]}|}; \draw[very thick, -{>>[red]}] (0,0) -- (5,0) node[right] {\verb|-{>>[red]}|}; \draw[very thick,-{<[red]}] (0,-1) -- (5,-1) node[right]{\verb|-{<[red]}|}; \draw[very thick,-{<<[red]}] (0,-2) -- (5,-2) node[right]{\verb|-{<<[red]}|}; \draw[very thick,-{latex[red]}] (0,-3) -- (5,-3) node[right]{\verb|-{latex[red]}|}; \draw[very thick,-{stealth[red]}] (0,-4) -- (5,-4) node[right]{\verb|-{stealth[red]}|}; \end{tikzpicture}
latex
,stealth
の矢じりを逆向きにすることができます。
\begin{tikzpicture} \draw[very thick,-{latex[reversed]}] (0,1) -- (5,1) node[right] {\verb|-{latex[reversed]}|}; \draw[very thick, -{stealth[reversed]}] (0,0) -- (5,0) node[right] {\verb|-{stealth[reversed]}|}; \end{tikzpicture}
角rounded corners
角の部分を丸くします。
\begin{tikzpicture} \draw[very thick,rounded corners = 5pt] (0,0) -- (2,2) -- (3,0) ; \draw[very thick,rounded corners = 20pt] (4,0) -- (6,2) -- (7,0) ; \end{tikzpicture}
線の端点の形状:line cap
\begin{tikzpicture} \draw[line width=6pt] (0,0) -- (2,2) -- (3,0) ; \draw[line width=6pt,line cap=butt] (4,0) -- (6,2) -- (7,0) ; \draw[line width=6pt, line cap=rect] (8,0) -- (10,2) -- (11,0) ; \draw[line width=6pt, line cap=round] (12,0) -- (14,2) -- (15,0) ; \end{tikzpicture}
線の結合部分の形状
\begin{tikzpicture} \draw[line width=6pt] (0,0) -- (2,2) -- (3,0) ; \draw[line width=6pt,line join=miter] (4,0) -- (6,2) -- (7,0) ; \draw[line width=6pt, line join=round] (8,0) -- (10,2) -- (11,0) ; \draw[line width=6pt, line join=bevel] (12,0) -- (14,2) -- (15,0) ; \end{tikzpicture}
とび出た線 shorten
オプションshorten<
またはshorten>
で指定します。
\begin{tikzpicture} \coordinate (A) at (0,0); \coordinate (B) at (5,0); \fill (A) circle (2pt); \fill (B) circle (2pt); \draw[thick,shorten <=1cm] (A) -- (B) node[above=10pt, midway]{\verb|shorten<=1cm|}; \end{tikzpicture}
shorten<=数値
で指定した分,短い線が描かれます。shorten<
は始点側,shorten>
は終点側です。以下のように,負の値を指定すると,線分が伸びます。
north west lines
\usetikzlibrary{patterns}
\begin{tikzpicture} \draw[pattern=north west lines] (0,0) rectangle (3,4); \end{tikzpicture}