最佳实践 - 如何绘制大量矩形(=相似)形状?

最佳实践 - 如何绘制大量矩形(=相似)形状?

这是一个相当普遍的询问,目的是找出如何以最佳方式完成此任务,并且花费的时间越少越好。如何使用 TikZ 绘制包含大量矩形(或其他类似形状)的图形?

  • 你会将每个位设为一个节点吗?

  • 您会使用一些高级节点定位方法,或者使用矩阵吗?

下面这个例子的解释不多:这是一张实验室机器的草图。显然有很多矩形,并且几乎同样多的节点“指向”它们。节点右侧(目前不可见)是相同的矩形,只是旋转了 90°,就像剖面图一样。

图片

MWE 01(下图),首次尝试

\documentclass[
11pt,
a4paper
]
{scrreprt}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}

\usepackage{
    tikz,
    setspace,
    hyperref
}

\usetikzlibrary{
    external,
    calc,
    matrix,
    fit,
    positioning,
    calc
}

\tikzset{
descr/.style={
draw,
rectangle,
text width=3.5cm,
minimum height=1cm, %use this because text width makes the text bottom-aligned
align=center}
}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small]
%the following foreach loop would obviously increase the speed of drawing the shapes on the left, but it is, as far as I can see, entirely without any coherent content -> a foreach loop is unfortunately of no use, also because of different lengths and heights
%\foreach \y in {0,2,4,6,8}{
%\node[draw, rectangle, text width=5cm, text height=1cm, align=center] at (0,\y) {aaa};}
\node[draw, rectangle, text width=4cm, text height=1.5cm, align=center] (node01) at (0,2) {node1};
\node[draw, rectangle, text width=4cm, text height=2cm, align=center] (node02) at (0,4) {node2};
\node[draw, rectangle, text width=5cm, text height=1.5cm, align=center] (node03) at (0,6) {node3};
\node[draw, rectangle, text width=3cm, text height=1.5cm, align=center] (node04) at (0,8) {node4};
\node[draw, rectangle, text width=4cm, text height=1.5cm, align=center] (node05) at (0,10) {node5};
%
%the 'descriptive' nodes on the right can be placed in this matter and the loop enables a somewhat uniform 'descriptive' look
%but with using a loop, I am clueless as to how I can name each node then
\begin{scope}[xshift=6cm]
\foreach \x/\y in {{Stuff and lots of other words}/0,Word/2,Text/4,aaa/6,bbb/8}{
    \node[descr] at (0,\y) {\x};}
\end{scope}
\end{tikzpicture}
\end{center}
\end{document}

MWE 01 的图片

在此处输入图片描述

答案1

我不知道我是否可以声称这是“最佳实践”,但这里是顶部显示的绘图部分的替代方案。注意:节点的实际宽度和高度是文本宽度和高度加上内部分隔符。此外,我抛弃了很多未使用的代码,因为我很固执。在大量 \draws 或 \nodes 之间进行选择或将它们集中在一起纯粹是出于审美考虑。

\documentclass[11pt,a4paper]{scrreprt}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{center}
\begin{tikzpicture}[font=\small,thick]
\path
  (0,2) node[draw, text width=5cm, text height=2cm, inner sep=0pt] (node01) {}% mostly for the anshors
  (node01.south) node[above] {node1};% an easier way to align text to bottom, or top etc.
\draw
  ($(node01.west)!0.25!(node01.east)$) circle[radius=5mm]
  (node01.center) circle[radius=5mm]
  ($(node01.west)!0.75!(node01.east)$) circle[radius=5mm];
\path
  (node01.north) node[draw, above, text width=4cm, text height=1cm, inner sep=0pt] (node02) {}
  (node02.north) node[draw, above,  text width=1cm, text height=3cm, inner sep=0pt] (node03) {}
  (node03.north) node[draw, below, text width=2cm, text height=1cm, inner sep=0pt] (node04) {}
  (node04.center) node[draw, text width=7mm, text height=7mm, inner sep=0pt] (node05) {};
% Note: without the calc below the line would stop at (node03.north)
\draw[very thin, dash dot] (node01.south)+(0,-5mm) -- ($(node03.north)+(0,5mm)$);
\end{tikzpicture}
\end{center}
\end{document}

图表

至于循环,我一直认为除非你需要循环,否则不要使用循环。这在一定程度上可以追溯到我从事 Cray 编程的日子,当时少于 8 个周期的循环比重复代码更糟糕。

相关内容