【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>
は終点側です。以下のように,負の値を指定すると,線分が伸びます。