使用以下代码,我手动使 \foreach 循环从节点 B 的中心位置开始。
我怎样才能自动从节点 B 的中心启动 \foreach 循环,这样当我想编辑代码来更改其位置时,\foreach 循环将从新位置开始,而无需重新定义其在 (3,\X) 的起始位置。
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{frame}[t]
\frametitle{}
\newsavebox{\recBBr}
\savebox{\recBBr}{
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=red!60!white] (0,0) rectangle +(1,1.24*1.68);
\end{tikzpicture}}
\begin{tikzpicture}[scale=.8, transform shape]
\draw [line width=.4mm, black, dashed](0,0) -- (11,0) node [pos=.28] (B) {\Large B};
\begin{scope}[on background layer]
\foreach \X in {0,1.5,3,4.5}
{\node[blue, yshift=.64cm] (y-\X) at (3,\X){\usebox{\recBBr}};}
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
答案1
选项1
使用坐标,找到通过 的垂直线和通过 的水平线(B|-0,\X)
之间的交点:(B)
(0,\X)
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{frame}[t]
\frametitle{}
\newsavebox{\recBBr}
\savebox{\recBBr}{% <-- important so you don't get extra space in the box
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=red!60!white] (0,0) rectangle +(1,1.24*1.68);
\end{tikzpicture}}
\begin{tikzpicture}[scale=.8, transform shape]
\draw [line width=.4mm, black, dashed](0,0) -- (11,0) node [pos=.28,font=\Large] (B) {B};
\begin{scope}[on background layer]
\foreach \X in {0,1.5,3,4.5}
{\node[blue,yshift=.64cm] (y-\X) at (B|-0,\X){\usebox{\recBBr}};}
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
选项 2
foreach
在以 开始的路径内使用B
:
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{frame}[t]
\frametitle{}
\newsavebox{\recBBr}
\savebox{\recBBr}{% <-- important so you don't get extra space in the box
\begin{tikzpicture}[scale=.6]
\draw [thick, black, fill=red!60!white] (0,0) rectangle +(1,1.24*1.68);
\end{tikzpicture}}
\begin{tikzpicture}[scale=.8, transform shape]
\draw [line width=.4mm, black, dashed](0,0) -- (11,0) node [pos=.28,font=\Large] (B) {B};
\begin{scope}[on background layer]
\path
(B)
\foreach \X in {1,...,4} {
node[blue,yshift=.64cm] (y-\X) {\usebox{\recBBr}}
++(0,1.5)
};
\end{scope}
\end{tikzpicture}
\end{frame}
\end{document}
请注意,这为您提供了更自然的节点命名(y-N)
,而且感觉更易读。++(0,1.5)
表示自上一个坐标以来的偏移,这感觉更直观,并且比手动提供y
循环列表中的值更容易更改。
在这两种情况下,框都有一个额外的初始空间,这会导致矩形框中出现不必要的 x 偏移。已通过注释换行符将其删除。
答案2
如果问题仅仅是绘制您在问题中显示的图像,那么我会按照以下方式绘制您的图像:
- 首先画红色的框
- 然后确定相对于爱心盒南坐标的起始虚线
- 在绘图中方便使用
chains
和positioning
库 - 不需要绘制红色框
savebox
,而是定义红色框的样式更简单:
\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{verbatim}
\usepackage{tikz}
\usetikzlibrary{chains,
positioning}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[
node distance = 2mm and 32mm,
start chain = going below,
box/.style = {draw, thick, fill=red!60!white,
minimum width=6mm, minimum height=12mm,
inner sep=0pt, outer sep=0mm,
on chain}
]
\foreach \i in {1,...,4}
\node (n\i) [box] {};
\coordinate[left=of n4.south] (a);
\draw [line width=.4mm, dashed]
(a) -- ++ (11,0);
\node[font=\Large] at (n4.south) {B};
\end{tikzpicture}
\end{frame}
\end{document}