节点的左上角和右下角矩形角处都有文本

节点的左上角和右下角矩形角处都有文本

我喜欢为以下节点定义样式:

在此处输入图片描述

到目前为止我已成功完成以下代码:

\documentclass[border=3mm,tikz]{standalone}
    \usetikzlibrary{positioning,shapes.multipart}

\makeatletter
\def\tikzsavelastnodename#1{\let#1=\tikz@last@fig@name}
\makeatother

\tikzset{%
    node distance=0mm and 15mm,
element/.style={%
    draw, fill=white,
    text width=24mm, minimum height=11mm, 
    node contents={},
    append after command={\pgfextra{\tikzsavelastnodename\tikzsavednodename}},#1
                },
subelement/.style={%
    append after command =
    {node[fill=cyan!30,minimum size=3mm, inner sep=2pt, outer sep=0.4pt,
          anchor=north west] 
          at (\tikzsavednodename.north west) {#1}}
                            },
subtext/.style={%
    append after command =
    {node[text width=24mm-3mm, %draw, 
          inner sep=0pt, outer sep=2pt,
          align=center, anchor=south east] 
          at (\tikzsavednodename.south east) {#1}}
                            },
             }

\begin{document}
    \begin{tikzpicture}
\linespread{0.8}
\node [element, 
       subelement=HB,
       subtext=max. two line long text];
\end{tikzpicture}
    \end{document}

这段代码实现了我想要的效果,但它有一个严重的缺点:文本宽度在样式中是固定的。我喜欢将它作为两种样式的一个选项:elementsubtext。我还喜欢为所有样式选项设置通用界面,类似如下:

\node [element=<text width> / <top left text> / <bottom right text>];

为此,我正在研究如何将这三种风格(elementsubelementsubtext)合并为一种,但我很快意识到,在这方面我需要在 TikZ 编程方面比我更熟练的人的帮助。

编辑: 鼓励敲击评论我尝试使用以下方法构建解决方案path picture

    \documentclass[border=3mm,tikz]{standalone}
    \usetikzlibrary{chains, positioning}
    \newcommand\ppbb{path picture bounding box}

    \begin{document}
        \begin{tikzpicture}[
element/.style args = {#1:#2:#3}{draw, fill=white, 
    text width=#1, 
    minimum height=11mm, outer sep=0pt, node contents={}, 
    path picture={% 
        \node[fill=cyan!30, minimum size=3mm, inner sep=2pt, outer sep=0.4pt, 
              below right=\ppbb.north west] {#2}; 
        \node[text width=#1-3mm, inner sep=0pt, outer sep=2pt, align=right, font=\small, 
              above right=of \ppbb.south east] {#3}; }
                                }% end of style
                            ]
    \linespread{0.8}
    \node (d1) [element=21mm:D1:short text];
    \node (d2) [element=22mm:D2:two line long text];
    \node (d3) [element=15mm:D3:narrow node];
    \node (d4) [element=18mm:D4:two line long text];
        \end{tikzpicture}
    \end{document}

此解决方案无效。错误是:

Unknown function `path` (in 'path picture bounding box.north west)

我不知道我在这个 MWE 中做错了什么。

答案1

同时我提出了以下解决方案:

\documentclass[border=3mm,tikz]{standalone}
\usetikzlibrary{chains, positioning}
\newcommand\ppbb{path picture bounding box}

\begin{document}
    \begin{tikzpicture}[
    node distance = 0mm,
      start chain = going right,
element/.style args={#1:#2:#3}{name=local name,%
    draw, fill=white,
    text width=#1, minimum height=11mm, outer sep=0pt,
    node contents={},
    append after command={node[fill=cyan!30,
        minimum size=3mm, inner sep=2pt, outer sep=0.4pt,
        anchor=north west]
        at (local name.north west) {#2}},
    append after command={node[text width=#1-3mm,
%        minimum height=9mm, text depth=0.5ex, % <-- this doesn't work as expected
        inner sep=0pt, outer sep=2pt,
        align=right, font=\small, anchor=south east]
          at (local name.south east) {#3}},
    on chain
                                },
                            ]
\linespread{0.8}
\node (d1) [element=21mm:D1:short text];
\node (d2) [element=22mm:D2:two line long text];
\node (d3) [element=15mm:D3:narrow node];
\node (d4) [element=18mm:D4:two line long text];
    \end{tikzpicture}
\end{document}

在此处输入图片描述

这个解决方案非常接近我想要的。它的缺点是格式化右下角的节点。我也尝试按照建议构建解决方案敲击在上述评论中,但到目前为止,我无法解决错误(请参阅我对问题的编辑)。我仍在寻找更好的解决方案 :)

相关内容