考虑这个例子:
\documentclass[tikz,border=5pt]{standalone}
\usetikzlibrary{calc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% RED
\draw (0.0, 0.0) -- (0.0, 2.0) -- (1.0, 3.0);
\node at(0,0){black};
% BLACK
\draw[color=red] ($(0.0, 0.0) + (1.5, 0.0)$) -- ($(0.0, 2.0) + (1.5, 0.0)$) -- ($(1.0, 3.0) + (1.5, 0.0)$);
\node[color=red] at ($(0,0) + (1.5, 0.0)$){red};
\end{tikzpicture}
\end{document}
我将所有红色元素移动 (1.5, 0.0)。如果我有一些非常复杂或多次重复的绘图怎么办!
是否可以将所有黑色元素分组到名为 Black 的对象中,将红色元素分组到名为 Red 的对象中,然后简单地将 Red 放置在 Black.east 处并将锚点放在 west 处?
答案1
将形状(或任何其他形状)封闭起来scope
并移动scope
到所需的量:
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
% RED
\draw (0,0) node[below] {black} -- (0,2) -- (1,3);
% BLACK
\begin{scope}[xshift=15mm]
\draw[red] (0,0) node[below] {red} -- (0,2) -- (1,3);
\end{scope}
\end{tikzpicture}
\end{document}
答案2
仅从一个点控制位置的另一种选择是使用相对坐标
\draw[style_options] (initial_x,initial_y) -- ++(x_shifting,y_shifting) ...;
要像创建对象一样创建它,您可以使用代码定义
\def\my_objet_name#arg1#arg2... {definition_objet_tikz_instrutions}
该定义允许在其他语句(如foreach语句)中附加绘图指令,更改初始坐标和更改颜色,在文本中插入计数器变量;在范围环境中,您还可以移动,旋转和缩放对象,使用变换形状强制节点旋转和缩放。
梅威瑟:
\documentclass[tikz,border=5pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% BLACK
\draw
(0,0)%Initial point
node[anchor=north]{black}
-- ++(0,2)%Relative point 1 shifted xshift=0, yshift=2 the coordinate is (0,2)
-- ++(1,1)%Relative point 2 shifted xshift=1, yshift=1 from point 1 (0,2 )the coordinate is (1,3)
;
\draw[red]
(1.5,0)%Initial point
node[anchor=north]{red}
-- ++(0,2)%Relative point 1 shifted xshift=0, yshift=2 the coordinate is (1.5,2)
-- ++(1,1)%Relative point 2 shifted xshift=1, yshift=1 from point 1 (0,2 )the coordinate is (2.5,3)
;
\def\myobjet(#1)#2{% 1:Initial coordinate, #2:Styles
\draw[#2]
(#1)%Initial point
node[anchor=north]{#2}
-- ++(0,2)
-- ++(1,1)
;
}
%drawing the objet
\myobjet(3,0){blue}
%The objet anidate in other drawing automated instructions
\foreach \x [evaluate=\x as \xn using {\x*20}]in {0,...,5}{
\myobjet(4.5+\x,0-\x*0.5){blue!\xn!red}%Shifted and color change
}
%Second objet anidating the first in a scope environmet for another transformations
\def\myobjetB(#1)#2[#3][#4]{% 1:Shifting using scope 2: color 3: rotation 4:scale
\begin{scope}[shift={(#1)}, rotate=#3,scale=#4, transform shape]
\myobjet(0,0){#2}
\end{scope}
}
%The second objet anidate in other automated drawing instruction
\foreach \x [evaluate=\x as \xn using {\x*20}]in {0,...,5}{
\myobjetB(0+\x*1.5,-3-\x*0.3){blue!\xn!green}[-\xn][0.3+0.2*\x]%Shifted, color changed, rotated, scaled...
}
\end{tikzpicture}
\end{document}