LaTeX繪製UML類圖備忘
阿新 • • 發佈:2018-12-05
這幾天編輯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}