
続くTikZで画像に描画する、私は絵に描くことに成功した例をたくさん持っていますティックZ。
しかし、今tikzpicture
はいくつかのグラフィックがあり、それぞれに描画したいのですが、TikZで画像に描画する(0, 0) を中心とする画像に対してのみ機能します。2 番目の画像に描画しようとすると、座標が期待どおりに動作しないためです。
\documentclass[final, 12pt]{standalone}
\usepackage{tikz}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {0.\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {0.\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
%\helplines
\end{scope}
\node[anchor=south west,inner sep=0] (image2) at (image1.south east)
{\rule{3cm}{3cm}};
\begin{scope}[x={(image2.south east)},y={(image2.north west)}]
\helplines
% draw more stuff, but coordinates do not map as desired
\end{scope}
\end{tikzpicture}
\end{document}
結果:
画像の左下に原点、右上に点 (1, 1) がある変換された座標系が欲しいです。これは (0, 0) にある画像には有効ですが、すぐ右にある画像には有効ではありません。なぜ有効ではないのでしょうか。また、どうすれば目的の効果が得られるのでしょうか。
答え1
これは、 を介して単位ベクトルをスケーリングしていますx=... , y=....
が、2 番目の画像の引数は直交ベクトルではないためです。image2
は別の場所にあり、座標の種類(x0,0)
ではありません(0,y0)
。代わりに、非ゼロのエントリを持つベクトルです。そのため、座標は水平/垂直ではなく対角になります。
同じことを行うには、対応するx
またはy
コンポーネントをゼロにする必要があります。例
\documentclass[final, 12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {\tiny .\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {\tiny .\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\color{red}\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
\helplines
\end{scope}
\node[anchor=south west,inner sep=0] (image2) at (image1.south east)
{\color{blue}\rule{3cm}{3cm}};
\begin{scope}[
x={($(image2.north east)-(image2.north west)$)},
y={($(image2.north west)-(image2.south west)$)},
shift={(image2.south west)}]
\helplines
\end{scope}
\end{tikzpicture}
\end{document}
ここでは、ライブラリを使用してcalc
画像の幅と高さを取得しました。ただし、画像もスコープに含めることで、すべてがローカルで同じように定義されるようにすることで、これらすべてを回避できます。次に、それらのスコープを移動します。例:
\documentclass[tikz]{standalone}
\newcommand{\helplines}[0]{
\draw[help lines,semithick,xstep=.1,ystep=.1] (0,0) grid (1,1);
\foreach \x in {0,1,...,9} { \node [anchor=north] at (\x/10,0) {\tiny .\x}; }
\foreach \y in {0,1,...,9} { \node [anchor=east] at (0,\y/10) {\tiny .\y}; }
}
\begin{document}%
\begin{tikzpicture}%
\node[anchor=south west,inner sep=0] (image1) at (0, 0)
{\color{red}\rule{3cm}{3cm}};
\begin{scope}[x={(image1.south east)},y={(image1.north west)}]
% draw stuff
\helplines
\end{scope}
\begin{scope}[shift={(image1.south east)}] % Shift the scope
\node[anchor=south west,inner sep=0] (image2) at (0,0) %still at the origin of this scope
{\color{blue}\rule{3cm}{3cm}};
\begin{scope}[x={(image2.south east)},y={(image2.north west)}]
\helplines
\end{scope}
\end{scope}
\end{tikzpicture}
\end{document}
これにより、同一の出力が得られます。