【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ができます。