LaTeX绘制UML类图备忘

匿名 (未验证) 提交于 2019-12-02 23:03:14

  这几天编辑LaTeX文档时需要绘制UML类图,这里把方法代码记录下来,以备忘。
 
  绘制UML类图,我们将使用宏包pgf-umlcd。示例代码中有两个例子,已经运行检测过,下面列出代码、效果图与说明。
 
环境:Ubuntu 16.04 64位桌面版
工具:TeXstudio

% 51CTO陆巍的博客LaTeX绘制UML类图示例 \documentclass[oneside, AutoFakeBold]{article}  \usepackage{geometry}          % 用于页面设置 % 设置为A4纸,并按照MSOffice的默认尺寸设置四周边距 \geometry{   a4paper,   left = 3.17cm,   right = 3.17cm,   top = 2.54cm,   bottom = 2.54cm }  \usepackage{xeCJK} % 设置字体。注意顺序,第一个定义的就是默认字体 \setCJKfamilyfont{song}{方正书宋简体} \newcommand{\song}{\CJKfamily{song}} \setCJKfamilyfont{kaiti}{方正楷体简体} \newcommand{\kaiti}{\CJKfamily{kaiti}} \setCJKfamilyfont{heiti}{方正黑体简体} \newcommand{\heiti}{\CJKfamily{heiti}}  % 支持绘制UML \usepackage[simplified]{pgf-umlcd}  \begin{document}  \centerline{\huge UML绘制示例} \quad\\\\ \heiti\large 示例一:\normalsize \begin{center}     \begin{tikzpicture}       \begin{class}[text width = 3cm]{CheckWriter}{0, 0}         \operation{+ writeCheck()}       \end{class}        \begin{class}[text width = 3cm]{Payroll}{4.5, 0}       \end{class}        \begin{class}[text width = 3cm]{Employee}{9, 0}         \operation{+ calculatePay()}         \operation{+ postPayment()}       \end{class}        \begin{class}[text width = 3cm]{Employee Database}{4.5, -2}         %\implement{Employee}         \operation{+ getEmployee()}         \operation{+ putEmployee()}       \end{class}        \unidirectionalAssociation{Payroll}{}{}{CheckWriter}       \unidirectionalAssociation{Payroll}{}{}{Employee}       \unidirectionalAssociation{Payroll}{}{}{Employee Database}       %\draw[umlcd style dashed line, ->](Employee Database.east) -- ++(2.9, 0) -- (Employee.south);       \draw[umlcd style dashed line, ->](Employee Database) -| (Employee);     \end{tikzpicture}     \\ 图4.1 耦合在一起的薪水支付应用模型   \end{center}   \quad\\\\   \large 示例二:\normalsize   \begin{center}       \begin{tikzpicture}         \begin{interface}[text width = 2.7cm]{CheckWriter}{0, -2}           \operation{+ writeCheck()}         \end{interface}          \begin{class}[text width = 2.7cm]{Mock CheckWriter}{0, 0}           \inherit{CheckWriter}         \end{class}          \begin{class}[text width = 2.7cm]{PayrollTest}{4, 0}         \end{class}          \begin{interface}[text width = 2.7cm]{Employee}{8, -2}           \operation{+ calculatePay()}           \operation{+ postPayment()}         \end{interface}          \begin{class}[text width = 2.7cm]{Mock Employee}{8, 0}           \inherit{Employee}         \end{class}          \begin{class}[text width = 2.7cm]{Payroll}{4, -2}         \end{class}          \begin{interface}[text width = 2.7cm]{Employee Database}{4, -4.5}           \operation{+ getEmployee()}           \operation{+ putEmployee()}         \end{interface}          \begin{class}[text width = 2.7cm]{Mock Employee Database}{4, -8}           \inherit{Employee Database}         \end{class}          \unidirectionalAssociation{PayrollTest}{}{}{Mock CheckWriter}         \unidirectionalAssociation{PayrollTest}{}{}{Mock Employee}         \unidirectionalAssociation{PayrollTest}{}{}{Payroll}         \unidirectionalAssociation{Payroll}{}{}{CheckWriter}         \unidirectionalAssociation{Payroll}{}{}{Employee}         \unidirectionalAssociation{Payroll}{}{}{Employee Database}         \draw [umlcd style dashed line, ->] (Employee Database) -| (Employee);         \draw [->](PayrollTest.north) -- ++(0, 0.3) -- ++(6, 0) |- (Mock Employee Database);       \end{tikzpicture}       \heiti\\ 图4.2 使用Mock Objects测试方法,解除了耦合的薪水支付应用模型\song     \end{center} \end{document}

效果如下:


 
  这份代码还是比较容易看懂的,绘制过程大概就是先绘制类,再绘制边线。说明如下:
  1. 绘制类时先绘制父类,否则表示继承关系的线指示会出问题,,其他类型的线也同理;
  2. 在第一个例子中,绘制虚线时列出了两种方式,大家注意观察比较。注释掉的方法实际上就是使用节点来转折;
  3. 注意画线时节点的坐标,这个坐标是相对坐标,是相对于上一个节点的坐标。例如第二个例子中的“-- ++(0, 0.3) -- ++(6, 0) |-”,(0, 0.3)表示的是相对于PayrollTest.North的坐标位置,而(6, 0)则是相对于(0, 0.3)的位置。
  4. 节点的四个方向是用英文的东南西北表示,不要弄成上下左右了;
  5. 注意“-|”与“|-”的区别,请在使用中自行体会。
  6. 示例代码中没有列出类属性的代码,方式与添加方法的差不多,只不过命令不一样,例如:
\attribute{owner : String}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!