具有不同样式的 Tikz 矩形

具有不同样式的 Tikz 矩形

我想要画这个图:

在此处输入图片描述

我试过:

\documentclass{memoir}

\usepackage{tikz}
\usetikzlibrary{shapes.misc, positioning}

\begin{document}

\begin{tikzpicture}
    \node[rounded rectangle, rounded rectangle right arc=none, draw,minimum height=1cm,minimum width=2cm] (part1) at (0,0) {123};
    \node[rounded rectangle, rounded rectangle left arc=none, draw,minimum height=1cm,minimum width=2cm] (part2) at (2.5,0) {456};
    \node[draw=none] [above=0.5ex of part1] {Label on top};
    \node[draw=none] [below=0.5ex of part1] {Label at bottom};
    
    \node[draw=none] [above=0.5ex of part2] {Label on top};
    \node[draw=none] [below=0.5ex of part2] {Label at bottom};
    
    \node[draw=none] (add) at (1,-2){Add togerher};
    \draw (part1) to [bend right=45] (add);
    \draw (part2) to [bend left=45] (add);
\end{tikzpicture}

\end{document}

答案1

一种可能性是为青色“半椭圆”或“矩形”创建一个\pic。这样,您可以旋转其中一个,并且两个都会具有相同的样式。您可以在其中为每个标签上方或下方的标签输入参数。

例如:

\documentclass[border=2mm,tikz]{standalone}

\tikzset
{%
  pics/half/.style 2 args={% #1: label below; #2: label above
    code={
      \path[pic actions] (-0.25,-0.75) -- (1.5,-0.75) arc (-90:90:1 and 0.75) -- (0.25,0.75);
      \node at (1,-1) {\strut#1};
      \node at (1, 1) {\strut#2};
      \coordinate (-center) at (1,0);
    }},
    my node/.style={draw,circle,inner sep=1pt},
  cyan fill/.style={draw,fill=cyan!30}
}

\begin{document}
\begin{tikzpicture}[line cap=round]
% pics
\pic[cyan fill,rotate=180] (L)  at (-0.5,0) {half={Label above}{Label below}};
\pic[cyan fill]            (R)  at  (0.5,0) {half={Label below}{Label above}};
% nodes inside
\node[my node] (123) at (L-center) {123};
\node           (23) at (R-center) {23};
\node[my node]   (4) at ([xshift=2pt]23.east) {4};
% other nodes
\node[inner sep=0] (this) at (-1.2,-2) {\strut Label for this};
\node[inner sep=0] (that) at (1.2,-2)  {\strut Label for that};
\node          (together) at (0,-3)    {Add together};
% arrows
\draw[-latex] (123)        to[out=180,in=180,looseness=2] (this.west);
\draw[-latex] (4)          to[out=0  ,in=0  ,looseness=2] (that.east);
\draw[-latex] (this.south) to[out=300,in=150] (together.150);
\draw[-latex] (that.south) to[out=240,in=30]  (together.30);
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑:我按照要求将最后两支箭弯曲了。

相关内容