我想用 tikz 创建如下图所示的图形。主要想法是,有时我想在两条线之间复制节点,这很容易。有时我想将一条线上的节点的水平空间放入另一条线上的新节点。
因此,第二行上的三个节点不会超出第一行的水平空间S
。同样,第三行中间的三个节点也不会超出第二行的水平空间S
。
如果我可以在行间插入水平线就更好了,但目前我还没有这样做。
我希望有更好的解决方案来创建这些图形。我尝试使用 tikz-chains。我考虑过 tikz-trees 和矩阵,但想不出什么。如果能提供一些建议,告诉我如何以更少的麻烦创建类似的图形,我将不胜感激。使用 tikz 的解决方案会很好,但我也接受其他方法。
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{res/.style={rectangle,draw, minimum height=1.8em} }
\begin{document}
\begin{tikzpicture}[align=center, node distance=8mm]
% 1st line
\node [res, minimum width=8cm] (l0s0) {$S$};
% 2nd line
\node [res, below=of l0s0.south west, anchor=west, xshift=4mm] (l1t0) {$($};
\node [res, right=of l1t0, minimum width=5cm] (l1s0) {$S$};
\node [res, right=of l1s0, node distance=2mm] (l1t1) {$)$};
% 3rd line
\node [res, below=of l1t0.south, anchor=center] (l2t0) {$($};
\node [res, below=of l1s0.south west, anchor=west, xshift=4mm] (l2t1) {$($};
\node [res, right=of l2t1, minimum width=2cm] (l2s0) {$S$};
\node [res, right=of l2s0] (l2t2) {$)$};
\node [res, below=of l1t1.south, anchor=center] (l2t3) {$)$};
\end{tikzpicture}
\end{document}
答案1
如果您不寻找自动或递归解决方案,我认为该positioning
库足够灵活,可以帮助您。
我在下面的代码中所做的是首先定位中心节点,然后定位括号节点。我不知道这是否是你想要的。
关于线之间的水平线,有几种绘制方法。这里我使用辅助coordinate
节点和交叉点(|-
或-|
)来划定它们。
\documentclass[tikz, border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{res/.style={rectangle,draw, minimum height=1.8em} }
\begin{document}
\begin{tikzpicture}[align=center, node distance=8mm]
% 1st line
\node [res, minimum width=8cm] (l0s0) {$S$};
% 2nd line
\node [res, below=5mm of l0s0, minimum width=5cm] (l1s0) {$S$};
\node [res, left=2mm of l1s0] (l1t0) {$($};
\node [res, right=2mm of l1s0] (l1t1) {$)$};
% 3rd line
\node [res, below=5mm of l1s0, minimum width=2cm] (l2s0) {$S$};
\node [res, left= 2mm of l2s0] (l2t1) {$($};
\node [res, right=2mm of l2s0] (l2t2) {$)$};
\node [res, below=5mm of l1t0] (l2t0) {$($};
\node [res, below=5mm of l1t1] (l2t3) {$)$};
\path (l0s0) -- (l1s0) coordinate[midway] (aux);
\draw (aux-|l0s0.west) -- (aux-|l0s0.east);
\path (l2s0) -- (l1s0) coordinate[midway] (aux);
\draw (aux-|l0s0.west) -- (aux-|l0s0.east);
\end{tikzpicture}
\end{document}
答案2
\documentclass{article}
\usepackage{tikz}
%\usetikzlibrary{positioning,calc}
\tikzset{res/.style={rectangle,draw, minimum height=1.8em} }
\begin{document}
\xdef\Max{6}
\begin{tikzpicture}
\node [res,minimum width=8 cm,draw] at (0,-1) {$S$};
% change 1.15-.15*\n in 1.20-.20*\n or more
% if one wants 's box' dicrease faster
\foreach \n [evaluate=\n as \w using (1.15-.15*\n)*8 ]
in {2,...,\Max} {%
\node [res,minimum width= \w cm,draw] at (0,-\n) {$S$};
\foreach \i in {\n,...,\Max} {%
\node[draw] at (\w/2+.45,-\i) {)} ;
\node[draw] at (-\w/2-.45,-\i) {(} ;}
}
\foreach \i in {1,...,\Max} {%
\draw (-4,-\i+.5) -- (4,-\i+.5) ;}
\end{tikzpicture}
\end{document}