tikz:绘制彼此相邻的特定宽度的矩形,中间有垂直线

tikz:绘制彼此相邻的特定宽度的矩形,中间有垂直线

我怎样才能以比给定的代码更清晰的方式(意味着:使用相对位置)创建以下结果:

相邻的矩形

  • 每个矩形都应具有特定的(最小)宽度。
  • 补充:我想使用两个数组。一个定义宽度,另一个定义文本内容。

示例代码:

\documentclass{article}
\usepackage{tikz}

\begin{document}
 \begin{tikzpicture}
    \def\bh{0.7} %box height
    \def\sepw{1pt} %seperator line width
    \def\ta{1}
    \def\tb{1.6}
    \def\tc{0.9}
    \def\td{0.7}
    \def\te{1.9}
    \draw [line width = \sepw] (0,0) -- (0, 1);
    \draw (0,0.5 - 0.5 * \bh ) rectangle (\ta, 0.5 + 0.5 * \bh ) node[pos=.5] {A};
    \draw [line width = \sepw] (\ta,0) -- (\ta, 1);
    \draw (\ta,0.5 - 0.5 * \bh ) rectangle (\ta+\tb, 0.5 + 0.5 * \bh ) node[pos=.5] {B};
    \draw [line width = \sepw] (\ta+\tb,0) -- (\ta+\tb, 1);
    \draw (\ta+\tb,0.5 - 0.5 * \bh ) rectangle (\ta+\tb+\tc, 0.5 + 0.5 * \bh ) node[pos=.5] {A};
    \draw [line width = \sepw] (\ta+\tb+\tc,0) -- (\ta+\tb+\tc, 1);
    \draw (\ta+\tb+\tc,0.5 - 0.5 * \bh ) rectangle (\ta+\tb+\tc+\td, 0.5 + 0.5 * \bh ) node[pos=.5] {A};
    \draw [line width = \sepw] (\ta+\tb+\tc+\td,0) -- (\ta+\tb+\tc+\td, 1);
    \draw (\ta+\tb+\tc+\td,0.5 - 0.5 * \bh ) rectangle (\ta+\tb+\tc+\td+\te, 0.5 + 0.5 * \bh ) node[pos=.5] {B};
  \end{tikzpicture}
\end{document}

答案1

这里有两个解决方案。第一个是通过移动coordinate来定位节点。第二个是通过chain(TikZ 库)。

第一种解决方案

\documentclass[border=2pt]{standalone}    
\usepackage{tikz}
\begin{document}    
\begin{tikzpicture}
  \coordinate (p);
  \foreach \n/\w in {A/1,B/2,A/1,A/1,B/2}{
    \node[draw,minimum height=1cm,minimum width=\w cm,anchor=west,outer sep=0pt]
    (n) at (p) {\n};
    \draw[thick] ([yshift=-1mm]n.south west) -- ([yshift=1mm]n.north west);
    \coordinate (p) at (n.east);
  }
\end{tikzpicture}      
\end{document}

第二种解决方案

\documentclass[border=2pt]{standalone}    
\usepackage{tikz}
\usetikzlibrary{chains}
\begin{document}    
\begin{tikzpicture}
  [start chain=going right,node distance=-.5\pgflinewidth]
  \foreach \n/\w in {A/1,B/2,A/1,A/1,B/2}{
    \node[on chain,draw,minimum height=1cm,minimum width=\w cm,anchor=west,outer sep=0pt]
    (n) {\n};
    \draw[thick] ([yshift=-1mm]n.south west) -- ([yshift=1mm]n.north west);
  }
\end{tikzpicture}      
\end{document}

答案2

使用循环内的常规tikz命令\foreach,这很容易。您只需指定字母和单元格宽度(以厘米为单位)。

\documentclass[border=2pt]{standalone}    
\usepackage{tikz}
\begin{document}    
\begin{tikzpicture}
  \newcounter{shiftx} \setcounter{shiftx}{0}
  \foreach \n/\w in {A/1,B/2,A/1,A/1,B/2}{
    \node[xshift=\theshiftx cm,draw,minimum height=1cm,minimum width=\w cm,anchor=west,outer sep=0pt]{\n}; 
    \draw[xshift=\theshiftx cm,thick] (0,.6cm)--(0,-.6cm);
    \addtocounter{shiftx}{\w}
  }
\end{tikzpicture}      
\end{document}

在此处输入图片描述

答案3

A matrix of nodesmatrix库)解决方案

\documentclass[tikz,border02mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
 \matrix (m) [matrix of nodes, 
    nodes={minimum size=1cm,draw, anchor=center},
    column 2/.style={nodes={minimum width=2cm}},
    column 5/.style={nodes={minimum width=2cm}},
    column sep=0pt,
    ]
    { A & B & A & A & B\\};
\foreach \i in {1,...,5}
    \draw[thick] ([yshift=-1mm]m-1-\i.south west)--([yshift=1mm]m-1-\i.north west);
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容