次の例を考えてみましょう。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\starty{3}
\def\length{1};
\coordinate(a1) at (1, \starty);
\coordinate(b1) at ($(a1) + (0, -\length)$);
\coordinate(a2) at (2, \starty - \length);
\coordinate(b2) at ($(a2) + (0, \length)$);
\draw[red, ->](a1) -- (b1);
\draw[red, ->](b2) -- (a2);
\draw (0, 0) -- (3, 0);
\draw (0, 0) -- (0, 3);
\end{tikzpicture}
\end{document}
結果は、単純な算術検査から得られるはずの、水平方向にずれた 2 つの矢印になります。
しかし、
\def\length{1};
による
\def\length{1cm};
結果は予想外です:
この不一致の原因は何ですか? また、この例をどのように修正すればよいですか?
答え1
問題は、単位付きと単位なしの式を加算/結合することです。けZは単位付きと単位なしの表現を区別します。この答え。 あなたが持っている場合
\path (x,y) coordinate (p);
とx
がy
無次元の場合、点p
は にありますx*(x unit vector)+y*(y unit vector)
。これらの単位ベクトルの初期値はそれぞれ(1cm,0)
とです(0,1cm)
が、 などを使用して変更できますx=(1cm,0.2cm)
。(単位を指定しない場合、これらの変更は注意が必要です。 を使用するとx={({cos(20)},{(sin(20)})},y={({cos(20+90)},{(sin(20+90)})}
、回転した座標系だけが得られないためです。むしろ、y=...
が解析されるときに、再定義された が既に使用されています。これが、 などのパッケージが回転座標系を定義するために単位を添付するx unit vector
理由です。)tikz-3dplot
あなたが持っている場合
\path (x,y) coordinate (p);
ここでx
、およびy
が単位を繰り上げると、点は右上にp
なります(もちろん回転などの変換は除きます)。単位ベクトルの初期値については、x
y
\path (1,2) coordinate (p);
そして
\path (1cm,2cm) coordinate (p);
同じ結果になるが、一般的にはそうではない。また、1つの座標に単位を付け、もう1つに単位を付けないということもできる。例えば、
\path (1cm,2) coordinate (p);
1cm
は、右に 2 倍シフトした点につながりますy unit vector
。
さて、あなたの質問に戻りますが、TiけZミックス
\path (a+b,y) coordinate (p);
a
が単位を持ち、が持たない場合b
、Tiけpt
Zはに単位を付加しますb
。例えば、
\path (1cm+1,2) coordinate (p);
p
x
の座標は になります1cm+1pt
が、
\path (1+1,2) coordinate (p);
x
座標は 2 倍になりますx unit vector
。
これを説明するために、MWE の座標と私がpt
無次元式に追加した座標を比較し、一致することを示します。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\subsection*{No units}
\begin{tikzpicture}
\def\starty{3}
\def\length{1};
\coordinate(a1) at (1, \starty);
\coordinate(b1) at ($(a1) + (0, -\length)$);
\coordinate(a2) at (2, \starty - \length);
\coordinate(b2) at ($(a2) + (0, \length)$);
\draw[red, ->](a1) -- (b1);
\draw[red, ->](b2) -- (a2);
\draw (0, 0) -- (3, 0);
\draw (0, 0) -- (0, 3);
\end{tikzpicture}
\subsection*{Mix of expressions with and without units}
\begin{tikzpicture}
\def\starty{3}
\def\length{1cm};
\coordinate(a1) at (1, \starty);
\coordinate(b1) at ($(a1) + (0, -\length)$);
\coordinate(a2) at (2, \starty - \length);
\coordinate(b2) at ($(a2) + (0, \length)$);
\draw[red, ->](a1) -- (b1);
\draw[red, ->](b2) -- (a2);
\draw (0, 0) -- (3, 0);
\draw (0, 0) -- (0, 3);
\draw[<->,blue] (2,3pt-1cm) -- ++ (1,0) -- (2,3pt);
\end{tikzpicture}
\end{document}