具有 UML 注释形状的 TikZ 节点

具有 UML 注释形状的 TikZ 节点

我想知道如何绘制看起来像 ** UML 注释 ** 的节点。有什么建议吗?

在此处输入图片描述

答案1

正如tikz 手册,目前 TikZ 不支持 UML [p. 791]。因此你可能会发现元UML有趣的是,尤其是他们的笔记

如果您已经安装了 MetaUML,只需将以下代码保存为name.mp并在控制台中输入mptopdf name.mp

input metauml;
input TEX;

beginfig(5);
  Note.A("This class implements the formula:", 
          TEX("$\sum_1^n f(x)\cdot dx$"));
  drawObjects(A);
endfig;

end

您将获得此结果name.pdf

MetaUML 中的注释示例

因此,您必须在外部定义您的 UML 结构,然后将 pdf 包含在您的文档中。

玩得开心!

答案2

根据 Meter 回复,已创建umlnote.sty

\ProvidesPackage{umlnote}

\usepackage{pgf,tikz}
\usetikzlibrary{calc,arrows}
    
\pgfdeclareshape{umlnote} {
    \inheritsavedanchors[from=rectangle]
    \inheritanchorborder[from=rectangle]
    \inheritanchor[from=rectangle]{center}
    \inheritanchor[from=rectangle]{north}
    \inheritanchor[from=rectangle]{north east}
    \inheritanchor[from=rectangle]{north west}
    \inheritanchor[from=rectangle]{south}
    \inheritanchor[from=rectangle]{south east}
    \inheritanchor[from=rectangle]{south west}
    \inheritanchor[from=rectangle]{west}
    \inheritanchor[from=rectangle]{east}
    \backgroundpath{
        \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
        \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
        \pgf@xc=\pgf@xb \advance\pgf@xc by-5pt
        \pgf@yc=\pgf@yb \advance\pgf@yc by-5pt
        \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
        \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
        \pgfpathclose
        \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
        \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
    }
}

% Key to add font macros to the current font
\tikzset{add font/.code={\expandafter\def\expandafter\tikz@textfont\expandafter{\tikz@textfont#1}}} 

% Define default style for this node
\tikzset{umlnote/port labels/.style={font=\sffamily}}
\tikzset{every umlnote node/.style={draw, inner sep=2mm, outer sep=0pt,font=\scriptsize}}

用于

\usepackage{umlnote}
... in document
\begin{tikzpicture}
  \node[umlnote,minimum width=2cm, text width=2cm] at (0,0) (note1) {This some uml comment blah blah blah...};
\end{tikzpicture}

结果

umlnote 示例

答案3

\pgfdeclareshape{umlnote}
    {
        \inheritsavedanchors[from=rectangle]
        \inheritanchorborder[from=rectangle]
        \inheritanchor[from=rectangle]{center}
        \inheritanchor[from=rectangle]{north}
        \inheritanchor[from=rectangle]{north east}
        \inheritanchor[from=rectangle]{north west}
        \inheritanchor[from=rectangle]{south}
        \inheritanchor[from=rectangle]{south east}
        \inheritanchor[from=rectangle]{south west}
        \inheritanchor[from=rectangle]{west}
        \inheritanchor[from=rectangle]{east}
        \backgroundpath{
            \southwest \pgf@xa=\pgf@x \pgf@ya=\pgf@y
            \northeast \pgf@xb=\pgf@x \pgf@yb=\pgf@y
            \pgf@xc=\pgf@xb \advance\pgf@xc by-5pt
            \pgf@yc=\pgf@yb \advance\pgf@yc by-5pt
            \pgfpathmoveto{\pgfpoint{\pgf@xa}{\pgf@ya}}
            \pgfpathlineto{\pgfpoint{\pgf@xa}{\pgf@yb}}
            \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yb}}
            \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
            \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@ya}}
            \pgfpathclose
            \pgfpathmoveto{\pgfpoint{\pgf@xc}{\pgf@yb}}
            \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
            \pgfpathlineto{\pgfpoint{\pgf@xb}{\pgf@yc}}
            \pgfpathlineto{\pgfpoint{\pgf@xc}{\pgf@yc}}
        }
    }

从文档中复制

相关内容