tikz uml,仅具有方法或数据成员的类

tikz uml,仅具有方法或数据成员的类

我从 tikz uml 文档中得到了这个例子。

在此处输入图片描述

如果我使用umlsimpleclass下面的两个矩形,则不会绘制类名。我的问题是... 有没有办法只包含数据成员部分或方法部分?文档似乎没有提供任何内容。

更新:完整视图如下

在此处输入图片描述

对于数据成员,语法是visibility_attribute name : type,而方法的语法是visibility_atribute name([name : type, name : type, ... ]) : return_type。这就是区别,我想要实现的是删除两个部分中的一个,只保留一个。而不是同时执行它们或不执行它们。

我想要实现的就是下图中的“ParentClass”之类的东西。

在此处输入图片描述

一些可供破解的工作代码:

\documentclass[tikz,14pt,border=10pt]{standalone}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{tikz-uml}
\usepackage{xcolor}
\usepackage{pgf-umlsd}
\usetikzlibrary{er}
\usetikzlibrary{decorations}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{positioning,calc}

\tikzstyle{int}=[draw, fill=blue!20, minimum size=3em]
\tikzstyle{init} = [pin edge={to-,thin,black}]

\begin{document}
\begin{tikzpicture} 
\umlclass{namespace::A} {
 + n : uint \\ \umlstatic{-- i : int} \\
    \# r : const float} {
    + setA(i : int) : void \\ \umlvirt{\# getA() : A}
}   
\end{tikzpicture}
\end{document}

答案1

据我了解,你想要的是下面这样的东西,没有底部的空矩形。

第一个代码块的输出

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz-uml}
\begin{document}
\begin{tikzpicture} 
\umlclass{foo}{bar}{}   
\end{tikzpicture}
\end{document}

从本质上讲,这只是一个结构,就像

\node [
   fill=\tikzumlNoteDefaultFillColor,
   draw,
   rectangle split,
   rectangle split parts=3,
   every one node part/.style={font=\bfseries}
   ] {\nodepart{one} foo \nodepart{two} bar \nodepart{three} };

看起来像

上面的 \node 是什么样子的

因此,最快的解决方法实际上是这样的:

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz-uml}
\begin{document}
\begin{tikzpicture} 
\umlclass[rectangle split parts=2]{foo}{bar}{}   
\end{tikzpicture}
\end{document}

上述代码的输出


另一种方法是根据 创建一个新宏\umlclass,其中rectangle split parts设置为2,第三个\nodepart被删除。tikz-uml不过这可以做很多事情。在\umlfoobar下面定义的宏中,我做了尽可能少的更改。它适用于给定的示例,但我不知道是否存在可能中断的情况。

最后一个代码块的输出

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tikz-uml}
\tikzset{tikzuml foobar style/.style={rectangle split, rectangle split parts=2, rectangle split part align={center, left, left}, minimum height=2em, node distance=2em}}

\makeatletter
%
\newcommand{\umlfoobar}[3][]{%
  \pgfkeys{/tikzuml/class/.cd, x/.initial=\tikzumlDefaultX, y/.initial=\tikzumlDefaultX, width/.initial=\tikzumlClassDefaultWidth, type/.initial=\tikzumlClassDefaultType,%
                               tags/.initial={}, style/.style={},%
                               template/.initial={}, name/.initial=tikzumlEmpty,%
                               draw/.initial=\tikzumlDefaultDrawColor,%
                               fill template/.initial=\tikzumlClassTemplateFillColorDefaultFillColor,%
                               fill/.initial=\tikzumlClassDefaultFillColor,%
                               text/.initial=\tikzumlDefaultTextColor,%
                               simple/.is if=tikzumlclassSimpleStyle, circle/.is if=tikzumlclassCircleShape, no coords/.is if=tikzumlclassWithoutCoords,%
                               simple=false, circle=false, no coords=false,%
                               .unknown/.code={%
                                 \let\keyname=\pgfkeyscurrentname%
                                 \let\keyvalue=\pgfkeyscurrentvalue%
                                 \ifthenelse{\equal{\keyname}{above}\OR%
                                             \equal{\keyname}{left}\OR%
                                             \equal{\keyname}{below}\OR%
                                             \equal{\keyname}{right}\OR%
                                             \equal{\keyname}{above left}\OR%
                                             \equal{\keyname}{above right}\OR%
                                             \equal{\keyname}{below left}\OR%
                                             \equal{\keyname}{below right}}{%
                                   \IfSubStr{\keyvalue}{ of }{%
                                     \pgfkeys{/tikzuml/class/.cd, no coords}%
                                   }{}%
                                 }{}%
                                 \ifx\keyvalue\pgfkeysnovalue%
                                   \pgfkeys{/tikzuml/class/.cd, style/.append style/.expand once={\keyname}}%
                                 \else%
                                   \pgfkeys{/tikzuml/class/.cd, style/.append style/.expand twice={\expandafter\keyname\expandafter=\keyvalue}}%
                                 \fi%
                                 %\errmessage{TIKZUML ERROR : in umlclass, invalid option \keyname}%
                               }%
  }%
  \pgfkeys{/tikzuml/class/.cd,#1}%
  %
  \iftikzumlclassSimpleStyle%
    \iftikzumlclassCircleShape%
      \pgfkeys{/tikzuml/class/.cd, width/.initial=\tikzumlSimpleInterfaceDefaultWidth}%
    \fi%
  \fi%
  %
  \pgfkeys{/tikzuml/class/.cd, x/.get=\tikzumlClassX, y/.get=\tikzumlClassY, width/.get=\tikzumlClassMinimumWidth,% 
                               type/.get=\tikzumlClassTypeTmp, tags/.get=\tikzumlClassTagsTmp, template/.get=\tikzumlClassTemplateFillColorParam,%
                               name/.get=\tikzumlClassName,%
                               draw/.get=\tikzumlClassDrawColor, fill/.get=\tikzumlClassFillColor,%
                               text/.get=\tikzumlClassTextColor, fill template/.get=\tikzumlClassTemplateFillColor%
  }%
  %
  \ifthenelse{\equal{\tikzumlClassTypeTmp}{class}\OR\equal{\tikzumlClassTypeTmp}{abstract}}{%
    \def\tikzumlClassType{}%
  }{%
    \expandafter\def\expandafter\tikzumlClassType\expandafter{$\ll$\tikzumlClassTypeTmp$\gg$ \\}%
  }%
  %
  \ifthenelse{\equal{\tikzumlClassTagsTmp}{}}{%
    \def\tikzumlClassTags{}%
  }{%
    \def\tikzumlClassTags{\\ \{\tikzumlClassTagsTmp\}}%
  }%
  %
  \ifthenelse{\equal{\tikzumlClassTemplateFillColorParam}{}}{%
    \def\tikzumlClassVPadding{}%
    \def\tikzumlClassHPadding{}%
  }{%
    \def\tikzumlClassVPadding{\vspace{0.1em} \\}%
    \def\tikzumlClassHPadding{\hspace{0.5ex} $ $}%
  }%
  %
  \def\tikzumlClassName{#2}%
  %
  \begingroup%
    \def\_{_}\edef\x{\endgroup%
      \def\noexpand\tikzumlClassNodeName{\tikzumlClassName}}\x%
  %
  \ifthenelse{\equal{\tikzumlClassName}{tikzumlEmpty}}{}{%
    \def\tikzumlClassNodeName{\tikzumlClassName}%
  }%
  %
  \StrSubstitute{\tikzumlClassNodeName}{:}{@COLON@}[\tikzumlClassNodeName]%
  \StrSubstitute{\tikzumlClassNodeName}{\_}{@UNDERSCORE@}[\tikzumlClassNodeName]%
  %
  \ifthenelse{\equal{\tikzumlClassTypeTmp}{abstract}}{%
    \let\tikzumlClassNameOld\tikzumlClassName%
    \def\tikzumlClassName{{\it \tikzumlClassNameOld}}%
  }{}%
  %
  \def\tikzumlClassPos{\tikzumlClassX,\tikzumlClassY}%
  \def\tikzumlClassAttributes{#3}%
%  \def\tikzumlClassOperations{}%
  %
  \iftikzumlclassSimpleStyle%
    \iftikzumlclassWithoutCoords%
      \iftikzumlclassCircleShape%
        \node[tikzuml simpleinterface style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) {};%
        \node[anchor=south] (\tikzumlClassNodeName-label) at (\tikzumlClassNodeName.north) {\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
        };%
      \else%
        \node[tikzuml simpleclass style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) {\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
        };%
      \fi%
    \else%
      \iftikzumlclassCircleShape%
        \node[tikzuml simpleinterface style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) at (\tikzumlClassPos) {};
        \node[anchor=south] (\tikzumlClassNodeName-label) at (\tikzumlClassNodeName.north){\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
        };%
      \else%
        \node[tikzuml simpleclass style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) at (\tikzumlClassPos) {\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
        };%
      \fi%
    \fi%
  \else%
    \iftikzumlclassWithoutCoords%
      \node[tikzuml foobar style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) {\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
      \nodepart{second}%
      \begin{tabular}{l}%
      \tikzumlClassAttributes%
      \end{tabular}%
      };%
    \else%
      \node[tikzuml foobar style, draw=\tikzumlClassDrawColor, fill=\tikzumlClassFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont, minimum width=\tikzumlClassMinimumWidth, /tikzuml/class/style] (\tikzumlClassNodeName) at (\tikzumlClassPos) {\begin{tabular}{c}\tikzumlClassVPadding \tikzumlClassType \tikzumlClassHPadding \textbf{\tikzumlClassName} \tikzumlClassHPadding \tikzumlClassTags \end{tabular}%
      \nodepart{second}%
      \begin{tabular}{l}%
      \tikzumlClassAttributes%
      \end{tabular}%
      };%
    \fi%
  \fi%
  %
  \ifthenelse{\equal{\tikzumlClassTemplateFillColorParam}{}}{}{%
    \draw (\tikzumlClassNodeName.north east) node[tikzuml template style, name=\tikzumlClassNodeName-template, draw=\tikzumlClassDrawColor, fill=\tikzumlClassTemplateFillColor, text=\tikzumlClassTextColor, font=\tikzumlDefaultFont] {\tikzumlClassTemplateFillColorParam};%
  }%
  %
  % add to fit
  \ifnum\c@tikzumlPackageLevel>0%
    \edef\tikzumlPackageFitOld{\csname tikzumlPackageFit\tikzumlPackage@parent @@\tikzumlPackage@fitname\endcsname}%
    \ifthenelse{\equal{\tikzumlClassTemplateFillColorParam}{}}{%
      \expandafter\xdef\csname tikzumlPackageFit\tikzumlPackage@parent @@\tikzumlPackage@fitname\endcsname{\tikzumlPackageFitOld (\tikzumlClassNodeName)}%
    }{%
      \expandafter\xdef\csname tikzumlPackageFit\tikzumlPackage@parent @@\tikzumlPackage@fitname\endcsname{\tikzumlPackageFitOld (\tikzumlClassNodeName) (\tikzumlClassNodeName-template)}%
    }%
    \stepcounter{tikzumlPackageClassNum}%
  \fi%
  \ifnum\c@tikzumlComponentLevel>0%
    \def\tikzumlComponentFitTmp{\csname tikzumlComponentFit\tikzumlComponent@parent @@\tikzumlComponent@fitname\endcsname}%
    \ifthenelse{\equal{\tikzumlClassTemplateFillColorParam}{}}{%
      \expandafter\xdef\csname tikzumlComponentFit\tikzumlComponent@parent @@\tikzumlComponent@fitname\endcsname{\tikzumlComponentFitTmp (\tikzumlClassNodeName)}%
    }{%
      \expandafter\xdef\csname tikzumlComponentFit\tikzumlComponent@parent @@\tikzumlComponent@fitname\endcsname{\tikzumlComponentFitTmp (\tikzumlClassNodeName) (\tikzumlClassNodeName-template)}%
    }%
    \stepcounter{tikzumlComponentSubComponentNum}%
  \fi%
}
\makeatother
\begin{document}
\begin{tikzpicture} 
\umlfoobar{foo}{bar}
\umlfoobar[x=4]{foo2}{bar}
\umlclass[x=2,y=-2]{foo}{bar}{baz}
\end{tikzpicture}
\end{document}

相关内容