我用pgf-umlcd 手册按照手册创建 uml 图。但是,当我使用\unidirectionalAssociation
命令时,会出现问题。
我的代码在这里 -
\documentclass{article}
\usepackage{pgf-umlcd}
\usepackage[a4paper,textwidth=8in]{geometry}
\begin{document}
\begin{tikzpicture}
\begin{class}{VocabularyObserver}{-3, -6}
\attribute{\# vocabulary : Vocabulary}
\operation{\# update(word : String) : void}
\end{class}
\begin{class}[text width = 12 cm]{Vocabulary}{-3,-10}
\attribute{- vocabularyObservers : List<VocabularyObserver>}
\attribute{+ MAXWORD : int}
\attribute{+ wordCounter : int}
\attribute{+ wordList : String[]}
\operation{+ addWord() : Boolean}
\operation{+ addObservers(vocabularyObserver : VocabularyObserver) : void}
\operation{- notifyAllObservers(word : String) : void}
\end{class}
\unidirectionalAssociation{uses}{VocabularyObserver}{0..*}{Vocabulary}
\end{tikzpicture}
\end{document}
编译错误在这里-
*geometry* driver: auto-detecting
*geometry* detected driver: pdftex
! Package pgf Error: No shape named uses is known.
See the pgf package documentation for explanation.
Type H <return> for immediate help.
...
l.21 ...ses}{VocabularyObserver}{0..*}{Vocabulary}
?
我怎么解决这个问题?
答案1
正如 TeXnician 所说,您收到错误的原因是您的参数\unidirectionalAssociation
顺序错误。您应该
\unidirectionalAssociation{<name of first class>}{<text above>}{<text below>}{<name of second class>}
但你所拥有的是
\unidirectionalAssociation{<text above>}{<name of first class>}{<text below>}{<name of second class>}
将文本放在线顶部的原因是 的定义\unidirectionalAssociation
,它似乎在设计时只考虑了水平线,因为两个节点分别放置在above
和 上below
。
更通用的方法是
\renewcommand{\unidirectionalAssociation}[4]{
\draw [umlcd style, ->] (#1) -- (#4)
node[near end, auto]{#2}
node[near end, auto,swap]{#3};
}
其中above
被 替换为auto
, 被below
替换为auto,swap
。根据此定义,它也适用于其他方向的线,如下例所示。(这两个demo
类纯粹是为了演示这一点。)
\documentclass{article}
\usepackage{pgf-umlcd}
\usepackage[a4paper,textwidth=8in]{geometry}
\renewcommand{\unidirectionalAssociation}[4]{
\draw [umlcd style, ->] (#1) -- (#4)
node[near end, auto]{#2}
node[near end, auto,swap]{#3};
}
\begin{document}
\begin{tikzpicture}
\begin{class}{demo}{-10, -6}
\attribute{foo}
\operation{bar}
\end{class}
\begin{class}{demo2}{-12, -10}
\attribute{foo2}
\operation{bar2}
\end{class}
\begin{class}{VocabularyObserver}{-3, -6}
\attribute{\# vocabulary : Vocabulary}
\operation{\# update(word : String) : void}
\end{class}
\begin{class}[text width = 12 cm]{Vocabulary}{-3,-10}
\attribute{- vocabularyObservers : List<VocabularyObserver>}
\attribute{+ MAXWORD : int}
\attribute{+ wordCounter : int}
\attribute{+ wordList : String[]}
\operation{+ addWord() : Boolean}
\operation{+ addObservers(vocabularyObserver : VocabularyObserver) : void}
\operation{- notifyAllObservers(word : String) : void}
\end{class}
\unidirectionalAssociation{VocabularyObserver}{uses}{0..*}{Vocabulary}
\unidirectionalAssociation{demo}{bar}{baz}{VocabularyObserver}
\unidirectionalAssociation{demo2}{here}{there}{VocabularyObserver}
\end{tikzpicture}
\end{document}