我有两个\scalebox
,每个都放置在scope
以下环境中:
\documentclass[border=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\newcommand{\hexlattice}[1]{\begin{tikzpicture}[hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,rotate=30}]
\foreach \j in {0,...,#1}{%
\foreach \i in {-1,...,#1}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {};} }
\end{tikzpicture}}
\begin{document}
\begin{tikzpicture}[scale=1.1,every node/.style={minimum size=1cm},on grid]
\begin{scope}[yshift=-120]
\draw[black, dashed, thin, xshift=30mm, yshift=0.5mm,xslant=-.8] (1,4) rectangle (7,6);
\scalebox{0.8}[0.4]{\hexlattice{5}}
\end{scope}
\begin{scope}[yshift=0]
\draw[black, dashed, thin, xshift=30mm, xslant=-.8] (1,4) rectangle (7,6);
\scalebox{0.8}[0.4]{\hexlattice{5}}
\end{scope}
\end{tikzpicture}
\end{document}
但是,第二次\scalebox
编译后没有任何结果,如下所示:
你能告诉我我在这里做错了什么吗?!
答案1
我想礼貌地说服你不是嵌套tikzpicture
没有 es 的环境\savebox
。我已经看到太多这种情况导致无法控制的结果。如果你真的想嵌套tikzpicture
,你可以把它放在\savebox
。然而,在很多情况下,根本不需要使用这个\savebox
技巧。对于 MWE,您只需使用 即可pic
。这允许您控制网格的颜色和其他参数,而\savebox
当您将其与 一起使用时, 不允许您访问这些参数\usebox
。
除此之外,我使用正交投影(而不是倾斜)来得出
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz-3dplot}
\usetikzlibrary{shapes,3d}
\begin{document}
\tdplotsetmaincoords{70}{30}
\begin{tikzpicture}[tdplot_main_coords,scale=1.1,
hexa/.style= {shape=regular polygon,regular polygon
sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,rotate=30},
hexlattice/.pic={
\foreach \j in {0,...,#1}{%
\foreach \i in {-1,...,#1}{%
\node[hexa] (h\i;\j) at ({(\i-(1+pow(-1,\j))*1/4)*sin(60)},{\j*0.75}) {};} }
}]
\begin{scope}[canvas is xy plane at z=0,transform shape]
\path[clip,postaction={draw,dashed}] (1,4) rectangle (7,6);
\pic[scale=0.5] at (2,2) {hexlattice=12};
\end{scope}
\begin{scope}[canvas is xy plane at z=4,transform shape]
\path[clip,postaction={draw,dashed}] (1,4) rectangle (7,6);
\pic[scale=0.5] at (2,2) {hexlattice=12};
\end{scope}
\end{tikzpicture}
\end{document}
在这里您可以更改view angle
,并且如您所见,pic
可以通过重新缩放 s scale=0.5
,也就是说,无需诉诸\scalebox
es。
答案2
如果您添加一些非 TikZ 命令(例如,将其添加\scalebox
到 a 中),tikzpicture
则应使用 a \node
。否则,您可能会得到奇怪的结果,因为 TikZ 在正常图片代码区域有特殊设置(即 nullfont)。
下面看起来还不错,您可能需要调整定位。
\documentclass[border=5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{shapes}
\newcommand{\hexlattice}[1]{\begin{tikzpicture}[hexa/.style= {shape=regular polygon,regular polygon sides=6,minimum size=1cm, draw,inner sep=0,anchor=south,rotate=30}]
\foreach \j in {0,...,#1}{%
\foreach \i in {-1,...,#1}{%
\node[hexa] (h\i;\j) at ({(\i-\j/2)*sin(60)},{\j*0.75}) {};} }
\end{tikzpicture}}
\begin{document}
\begin{tikzpicture}[scale=1.1,every node/.style={minimum size=1cm},on grid]
\begin{scope}[yshift=-120]
\draw[black, dashed, thin, xshift=30mm, yshift=0.5mm,xslant=-.8] (1,4) rectangle (7,6);
\node [anchor=south west] at (0,4) {\scalebox{0.8}[0.4]{\hexlattice{5}}};
\end{scope}
\begin{scope}[yshift=0]
\draw[black, dashed, thin, xshift=30mm, xslant=-.8] (1,4) rectangle (7,6);
\node [anchor=south west] at (0,4) {\scalebox{0.8}[0.4]{\hexlattice{5}}};
\end{scope}
\end{tikzpicture}
\end{document}