我有下面这张图。我怎样才能轻松地将整个 tikzpicture 移动到其他地方?
\begin{tikzpicture}
\foreach \x in{0,...,4}
{ \draw (0,\x ,4) -- (4,\x ,4);
\draw (\x ,0,4) -- (\x ,4,4);
\draw (4,\x ,4) -- (4,\x ,0);
\draw (\x ,4,4) -- (\x ,4,0);
\draw (4,0,\x ) -- (4,4,\x );
\draw (0,4,\x ) -- (4,4,\x );
}
\end{tikzpicture}
我所说的简单应该是
\begin{tikzpicture, origin=1.0,1.0}
\foreach \x in{0,...,4}
{ \draw (0,\x ,4) -- (4,\x ,4);
\draw (\x ,0,4) -- (\x ,4,4);
\draw (4,\x ,4) -- (4,\x ,0);
\draw (\x ,4,4) -- (\x ,4,0);
\draw (4,0,\x ) -- (4,4,\x );
\draw (0,4,\x ) -- (4,4,\x );
}
\end{tikzpicture}
答案1
您可以使用scope
带有指示[shift={(1,1)}]
翻译的选项的环境您的代码变成这样:
\begin{tikzpicture}
\begin{scope}[shift={(1,1)}]
\foreach \x in{0,...,4}
{ \draw (0,\x ,4) -- (4,\x ,4);
\draw (\x ,0,4) -- (\x ,4,4);
\draw (4,\x ,4) -- (4,\x ,0);
\draw (\x ,4,4) -- (\x ,4,0);
\draw (4,0,\x ) -- (4,4,\x );
\draw (0,4,\x ) -- (4,4,\x );
}
\end{scope}
\end{tikzpicture}
答案2
有点类似Raven 的回答,但可能不那么残酷,而且更准确(因为节点有宽度)。从技术上讲,它扩展了边界框,但在文档中(独立文档除外),它确实会移动图片。这个东西在叠加时效果不好。
\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\tikzset{shift entire picture/.style n args={2}{execute at end picture={
\pgfmathtruncatemacro{\tmpx}{sign(#1)}
\pgfmathtruncatemacro{\tmpy}{sign(#2)}
\ifnum\tmpx=1
\ifnum\tmpy=1
\path[use as bounding box] ([xshift=-#1,yshift=-#2]current bounding box.south west) rectangle
(current bounding box.north east);
\else
\path[use as bounding box] ([xshift=-#1]current bounding box.south west) rectangle
([yshift=-#2]current bounding box.north east);
\fi
\else
\ifnum\tmpy=1
\path[use as bounding box] ([yshift=-#2]current bounding box.south west) rectangle
([xshift=-#1]current bounding box.north east);
\else
\path[use as bounding box] (current bounding box.south west) rectangle
([xshift=-#1,yshift=-#2]current bounding box.north east);
\fi
\fi}}}
\begin{document}
\begin{tikzpicture}[shift entire picture={3cm}{-5cm}]
\foreach \x in{0,...,4}
{ \draw (0,\x ,4) -- (4,\x ,4);
\ifnum\x=0
\draw (\x ,0,4) -- (\x ,4,4);
\else
\draw (\x ,0,4) -- (\x ,0.1,4) (\x ,0.9,4) -- (\x ,1.1,4)
(\x ,1.9,4) -- (\x ,2.1,4) (\x ,2.9,4) -- (\x ,3.1,4)
(\x ,3.9,4) -- (\x ,4,4);
\fi
\draw (4,\x ,4) -- (4,\x ,0);
\ifnum\x=0
\draw (\x ,4,4) -- (\x ,4,0);
\else
\draw (\x,4,0) -- (\x,4,0.1) (\x,4,0.9) -- (\x,4,1.1)
(\x,4,1.9) -- (\x,4,2.1) (\x,4,2.9) -- (\x,4,3.1)
(\x,4,3.9) -- (\x,4,4);
\fi
\draw (4,0,\x ) -- (4,4,\x );
\draw (0,4,\x ) -- (4,4,\x );
}
\end{tikzpicture}
\end{document}
答案3
这可能不是最优雅的解决方案,但它有效:
\begin{tikzpicture}
% Create "shifting node" that shifts all following coordinates by (5,6)
\node at (-5,-6) {};
\foreach \x in{0,...,4}
{ \draw (0,\x ,4) -- (4,\x ,4);
\draw (\x ,0,4) -- (\x ,4,4);
\draw (4,\x ,4) -- (4,\x ,0);
\draw (\x ,4,4) -- (\x ,4,0);
\draw (4,0,\x ) -- (4,4,\x );
\draw (0,4,\x ) -- (4,4,\x );
}
\end{tikzpicture}
其基本原理是,在图片“移动”后,在原点位置添加一个不可见节点。这样,该区域内的一些内容就不会被裁剪掉tikz
(我猜想,当仅使用shift
整个图像的选项时,就会发生这种情况)。
shift
@TeXnician 在评论中对 -option 进行了进一步说明:
[该shift
选项] 确实会移动坐标原点(变换每个坐标)。由于边界框被修剪,您在普通情况下仍然不会注意到它(但有时它很方便)。