Debian Codegraph @

texmf-dist/source/latex/bondgraphs/bondgraphs.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) ⟨2014⟩ by ⟨Geert Folkertsma⟩
4 %
5 % This file may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either
7 % version 1.2 of this license or (at your option) any later
8 % version. The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.2 or later is part of all distributions of
13 % LaTeX version 1999/12/01 or later.
14 %
15 % \fi
16 %
17 %
18 % \iffalse
19 %<*driver>
20 \ProvidesFile{bondgraphs.dtx}
21 \documentclass{ltxdoc}
22 \usepackage{bondgraphs}
23 \usetikzlibrary{circuits.ee.IEC,decorations,decorations.pathmorphing}
24 \tikzstyle{mass}=[minimum width=.7cm,minimum height=.7cm,draw=black!80!white,thick,fill=black!20!white]
25 \tikzstyle{spring}=[thick,decorate,decoration={zigzag,pre length=0.1cm,post length=0.1cm,segment length=5}]
26 \tikzstyle{world}=[minimum width=1.5cm, minimum height=5mm,fill,draw,thick]
27 \usepackage[numbered]{hypdoc}
28 \usepackage{xspace}
29 \usepackage{booktabs}
30 \usepackage{microtype}
31 \usepackage{amsmath}
32 \def\bgs{\textsf{bondgraphs}\xspace}
33 \def\bgspkg{\textsf{bondgraphs} package\xspace}
34 \def\tkz{\textsf{TikZ}\xspace}
35 \def\secref#1{\hyperref[#1]{Section~\ref*{#1}: \nameref*{#1}}}
36 \def\figref#1{\hyperref[#1]{Figure~\ref*{#1}}}
37 \def\bgsrccolor{blue!40!black}
38 \def\bgsrc{\textbf{\color{\bgsrccolor}Source: }}
39 \EnableCrossrefs
40 \CodelineIndex
41 \RecordChanges
42 \begin{document}
43 \DocInput{bondgraphs.dtx}
44 \end{document}
45 %</driver>
46 % \fi
47 %
48 % \CheckSum{122}
49 %
50 % \CharacterTable
51 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
52 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
53 % Digits \0\1\2\3\4\5\6\7\8\9
54 % Exclamation \! Double quote \" Hash (number) \#
55 % Dollar \$ Percent \% Ampersand \&
56 % Acute accent \' Left paren \( Right paren \)
57 % Asterisk \* Plus \+ Comma \,
58 % Minus \- Point \. Solidus \/
59 % Colon \: Semicolon \; Less than \<
60 % Equals \= Greater than \> Question mark \?
61 % Commercial at \@ Left bracket \[ Backslash \\
62 % Right bracket \] Circumflex \^ Underscore \_
63 % Grave accent \` Left brace \{ Vertical bar \|
64 % Right brace \} Tilde \~}
65 %
66 % \changes{v0.9}{2014/11/20}{Initial .dtx version}
67 % \changes{v1.0}{2014/11/27}{Documentation completed.}
68 %
69 % \GetFileInfo{bondgraphs.sty}
70 %
71 % \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
72 % \DoNotIndex{\if,\else,\fi,\def,\ifcase,\or}
73 % \DoNotIndex{\tikzset,\pgfextra,\draw,\colorlet,\path,\node}
74 % \DoNotIndex{\pgfmathparse,\pgfdecoratedangle,\tikzinputsegmentfirst,\tikzinputsegmentlast}
75 % \DoNotIndex{\NeedsTeXFormat,\ProvidesPackage,\RequirePackage,\usetikzlibrary,\kvoptions,\SetupKeyvalOptions,\DeclareStringOption,\DeclareBoolOption,\DeclareComplementaryOption,\ProcessLocalKeyvalOptions}
76 % \DoNotIndex{\ifbondgraphs@grey,\message,\pgfmathresult,\ifbondgraphs@curly,\small,\newcommand,\tikz}
77 % \DoNotIndex{\bfseries,\mdseries,\mathbb,\bgroup,\egroup,\mdseries,\newif,\ifbondgraphs@bgelement@multiport,\ifbondgraphs@bgelement@wordbg,\pgfkeys}
78 % \DoNotIndex{\ifnum,\ensuremath,\mathbb,\textrm,\textbf,\newenvironment,\begin,\end}
79 % \DoNotIndex{\bondgraphs@bgelement@n,\bondgraphs@bondlabelcolour,\bondgraphs@labelcolour}
80 %
81 % \title{The \bgspkg{}\thanks{This document corresponds to \bgs{}~\fileversion, dated~\filedate.}}
82 % \author{Geert Folkertsma \\ \texttt{[email protected]}}
83 %
84 % \maketitle
85 %
86 % \begin{abstract}
87 % The bondgraphs package is used to draw bond graphs in LaTeX. It uses a recent version (3.0+) of TikZ for the drawing, hence, it is mainly a set of TikZ styles that makes the drawing of bond graphs easier.
88 % \end{abstract}
89 %
90 % \section{Introduction}
91 % A bond graph is a graphical representation of a physical dynamic system. The graph consists of nodes---junctions and elements---and edges---bonds---just like any other graph. This package, \bgs{}\footnote{From now on, we will write ``bondgraphs'' as a single word.}, is made to facilitate the drawing of bondgaphs in \LaTeX. For more information about bondgraphs, we suggest the original introduction by Henry Paynter (``Analysis and Design of Engineering Systems,'' ISBN~0-262-16004-8) or the PhD thesis ``Physical systems theory in terms of bond graphs'' by P.C. Breedveld, ISBN~90-9000599-4.
92 %
93 % \emph{\color{red!50!black}Note that this package relies on a very recent version of \tkz{}/pgf, namely 3.0. This version is shipped with recent (2014+) versions of \LaTeX{}.}
94 %
95 % To conclude this introduction, \figref{fig:analogies} shows two analogous physical systems and the corresponding bondgraph.
96 %
97 % \begin{figure}[h!]
98 % \centering
99 % \begin{bondgraph}
100 % \node at (0,0) (one) {1};
101 % \node[left=1 of one,label={west:$u:$}] (Se) {Se};
102 % \node[above=1 of one] (I) {I};
103 % \node[right=1 of one] (C) {C};
104 % \draw
105 % (Se) edge[e_out] (one)
106 % (one) edge[e_out] (I)
107 % edge[f_out] (C);
108 % \end{bondgraph}\hfil\begin{tikzpicture}[circuit ee IEC]
109 % \draw (0,0) to[voltage source={direction info={volt=u}}] (0,2)
110 % to [inductor={info={$I=L$}}] (2,2)
111 % to [capacitor={info'={$C$}}] (2,0)
112 % to (0,0);
113 % \end{tikzpicture}\hfil\begin{tikzpicture}
114 % \node[mass] at (0,0) (m) {$I=m$};
115 % \node[world,below=1 of m] (g) {};
116 % \draw[spring] (m.south west)++(0.2,0) -- +(0,-1);
117 % \node at ($(m.south east)+(-0.2,-.5)$) (F) {$F$};
118 % \node[right=-.2 of F] {$=u$};
119 % \draw[latex-] (m.south east)++(-0.2,0) -- (F);
120 % \draw[-latex] (F) -- ($(m.south east)+(-0.2,-1)$);
121 % \node[left=.7 of F] {$C=\frac{1}{k}$};
122 % \end{tikzpicture}
123 % \caption{Two analogous physical systems: in the electrical and mechanical domain; and the bondgraph that describes both.}
124 % \label{fig:analogies}
125 % \end{figure}
126 % \clearpage
127 % \section{Usage}
128 % The \bgspkg provides styles for complete bondgraph drawing, as well as text-mode commands to draw or typeset single bonds or elements, useful e.g. when explaining concepts or referring to elements in a complete bondgrahp.
129 % \subsection{Text-mode}
130 %
131 % \subsubsection{Bonds}\label{sec:textmode-bonds}
132 % \DescribeMacro{\bond}
133 % |\bond| \oarg{drawing options} \\
134 % The |\bond| command draws a single bond from left to right, like so: \bond{}.
135 % The drawing options can be used to specify causality and various colours and labels, as demonstrated in the table below. Note: the |diff| and |error| are redefined to grey when using the package option |grey|. \\
136 % \begin{tabular}{rllr}
137 % \textbf{Description} & ~ & \textbf{Option} & \textbf{Result} \\ \toprule
138 % Causality & effort in / flow out & |e_in| & \bond[e_in] \\
139 % ~ & ~ & |f_out| & \bond[f_out] \\
140 % ~ & effort out / flow in & |e_out| & \bond[e_out] \\
141 % ~ & ~ & |f_in| & \bond[f_in] \\ \midrule
142 % Causal stroke & differential causality & |e_out={diff}| & \bond[e_out={diff}] \\
143 % ~ & erroneous causality & |f_out={error}|& \bond[f_out={error}] \\
144 % ~ & arbitrary colour & |f_in={blue} | & \bond[f_in={blue}] \\ \midrule
145 % Labels & effort & |effort={$F_2$}| & \bond[effort={$F_2$}] \\
146 % ~ & flow & |flow={$v_2$}| & \bond[flow={$v_2$}] \\
147 % ~ & both & |flow={f},effort={e}|&\bond[flow={f},effort={e}] \\ \midrule
148 % \multicolumn{2}{l}{Multi-dimensional bonds} & |mbond| & \bond[mbond] \\
149 % \bottomrule
150 % \end{tabular}\\[12pt]
151 % Note that the bond label colour can be set with the |bondlabelcolour| package option, which is dark green by default, or grey when the |grey| package option is used.
152 %
153 % Any option that is not recognised as one of the options listed in the above table is passed on to the \tkz drawing command. This allows for example the colouring of a bond, or the changing of a causal stroke width\footnote{This is actually already used in the table, with the \texttt{f\_in={blue}} option}. A (rather extreme) example: \\[6pt]
154 % |\bond[green,ultra thick,e_in={line width=5pt,width=20pt,orange!60}]| \\ gives \bond[green,ultra thick,e_in={line width=5pt,width=12pt,orange!60}].
155 %
156 % \clearpage
157 % \subsubsection{Elements}
158 % \DescribeMacro{\bgelement}
159 % |\bgelement| \oarg{options} \marg{element} \\
160 % Like |\bond|, |\bgelement| typesets a single bondgraph element in text, which is most useful when referring to elements in a complete bondgraph drawing. In contrast to the |\bond| command, |\bgelement| does not use \tkz for drawing, but \LaTeX{} typesetting. The following table shows the various options: \\[6pt]
161 % \begin{tabular}{l l r c}
162 % \textbf{Element type} & \textbf{Extra options} & \textbf{Examples} & \textbf{Result} \\ \toprule
163 % Normal & -- & |\bgelement{R}| & \bgelement{R} \\
164 % ~ & ~ & |\bgelement{1}| & \bgelement{1} \\
165 % ~ & Multiport & |\bgelement[multiport]{C}| & \bgelement[multiport]{C} \\
166 % ~ & ~ & |\bgelement[multiport]{MTF}|& \bgelement[multiport]{MTF} \\
167 % ~ & $n$-dimensional & |\bgelement[n=3]{Se}| & \bgelement[n=3]{Se} \\
168 % ~ & ~ & |\bgelement[n=1]{Gy}| & \bgelement[n=1]{Gy} \\
169 % ~ & combination&|\bgelement[n=2,multiport]{I}|&\bgelement[n=2,multiport]{I} \\ \midrule
170 % Word-bondgraph & (none allowed) & |\bgelement[wordbg]{Sys}| & \bgelement[wordbg]{Sys} \\
171 % \bottomrule
172 % \end{tabular} \\[12pt]
173 % Note that the word-bondgraph element is exception to the no-\tkz rule: it is drawn as an elliptical \tkz node, with argument \marg{element} passed as node contents. Hence, it is possible to do something like: \\[6pt]
174 % |\bgelement[wordbg]{\bgelement[n=4]{Environment}}|, \\
175 % which gives
176 % \bgelement[wordbg]{\bgelement[n=4]{Environment}}.
177 %
178 % \clearpage
179 % \subsection{Drawing mode}
180 % While the text-mode commands have their uses, the most important part of this package is the drawing of actual bondgraphs. Because the drawing of bonds has been implemented using \tkz styles, the drawing of bondgraphs comes down to drawing nodes and connecting edges using \tkz. There are examples in the next section (\secref{sec:examples}); here only the basic structure is explained.
181 %
182 % Note that, because all drawing is done in \tkz, it is very easy to make use of \tkz{}'s advanced positioning features and other libraries. By default, the \tkz{}-libraries |shapes| and |positioning| are loaded; the example section demonstrates their use.
183 %
184 % \subsubsection{Bonds}
185 % Bonds are drawn using the |bond| style on a path or an edge. Because the text-mode |\bond| command actually draws an edge with the |bond| style appended to the optional \oarg{drawing options} (see \secref{sec:textmode-bonds}), all the options explained there are applicable in drawing mode, too. The example below shows how to draw bonds inside a |tikzpicture| environment: \\[6pt]
186 % \begin{tabular}{rl}
187 % \begin{minipage}{.7\columnwidth}
188 % \begin{verbatim}
189 % \begin{tikzpicture}
190 % \draw[bond] (0,0) -- (1,0);
191 % \draw (0,-.5) edge[bond,effort={e}] (1,-.5);
192 % \draw[bond,e_in={diff}] (0,-1) to (2,-1.5);
193 % \end{tikzpicture}
194 % \end{verbatim}
195 % \end{minipage} & \begin{minipage}{.3\columnwidth}\begin{tikzpicture}
196 % \draw[bond] (0,0) -- (1,0);
197 % \draw (0,-.5) edge[bond,effort={e}] (1,-.5);
198 % \draw[bond,e_in={diff}] (0,-1) to (1.5,-1.5);
199 % \end{tikzpicture}\end{minipage}
200 % \end{tabular}\\[12pt]
201 %
202 % \DescribeEnv{bonds}
203 % A special drawing style is defined, |bonds|, which appends the |bond| style to all |edge|s on the path. It is useful when drawing a lot of bonds in one go---just remember to use |edge| rather than |to| or |--| between nodes or coordinates:\\[6pt]
204 % \begin{tabular}{rl}
205 % \begin{minipage}{.7\columnwidth}
206 % \begin{verbatim}
207 % \begin{tikzpicture}
208 % \draw[bonds]
209 % (0,0) edge (1,0.3)
210 % edge (1,-0.3)
211 % (1,-1) edge (0,-1)
212 % (0,-1.5) -- (1,-1.5); %won't produce a bond
213 % \end{tikzpicture}
214 % \end{verbatim}
215 % \end{minipage} & \begin{minipage}{.3\columnwidth}\begin{tikzpicture}
216 % \draw[bonds]
217 % (0,0) edge (1,0.3)
218 % edge (1,-0.3)
219 % (1,-1) edge (0,-1)
220 % (0,-1.5) -- (1,-1.5);
221 % \end{tikzpicture}\end{minipage}
222 % \end{tabular}\\[12pt]
223 % Finally, a silly drawing that shows the half-arrow stroke always points down:\\[6pt]
224 % \begin{tabular}{rl}
225 % \begin{minipage}{.7\columnwidth}
226 % \begin{verbatim}
227 % \begin{tikzpicture}
228 % \foreach \a in {0,30,...,350}
229 % {\draw[bond] (0,0) -- (\a:1);}
230 % \end{tikzpicture}
231 % \end{verbatim}
232 % \end{minipage} & \begin{minipage}{.3\columnwidth}
233 % \begin{tikzpicture}
234 % \foreach \a in {0,30,...,350}
235 % {\draw[bond] (0,0) -- (\a:1);}
236 % \end{tikzpicture}
237 % \end{minipage}
238 % \end{tabular}\\[12pt]
239 % There are two more things to mention on the subject of bonds drawing: \begin{enumerate}
240 % \item the package option |curly| will change the straight half-arrow into a curly one;
241 % \item some people prefer the half-arrow stroke to point left or down; this can be tweaked at the top of the generated \texttt{.sty} file (see \secref{sec:implementation-bonds}), which \emph{might} become a package option in the future.
242 % \end{enumerate}
243 %
244 % \subsubsection{Elements}
245 % Elements and junctions are drawn as \tkz nodes, with the |bgelement| style. This style does two things: it typesets the label text in bold (|\bfseries|) and defines a special label style for element labelling. Some examples:\\[6pt]
246 % \begin{tabular}{rl}
247 % \begin{minipage}{.7\columnwidth}
248 % \begin{verbatim}
249 % \begin{tikzpicture}
250 % \node[bgelement] at (0,0) {C};
251 % \node[bgelement] at (0,-0.5) {Se};
252 % \end{tikzpicture}
253 % \end{verbatim}
254 % \end{minipage} & \begin{minipage}{.3\columnwidth}
255 % \begin{tikzpicture}
256 % \node[bgelement] at (0,0) {C};
257 % \node[bgelement] at (0,-0.5) {Se};
258 % \end{tikzpicture}
259 % \end{minipage} \\
260 % \begin{minipage}{.7\columnwidth}
261 % \begin{verbatim}
262 % \begin{tikzpicture}
263 % \node[bgelement,label=east:$v_x$] at (0,0) {1};
264 % \node[bgelement,label=south:m] at (0,-0.5) {I};
265 % \end{tikzpicture}
266 % \end{verbatim}
267 % \end{minipage} & \begin{minipage}{.3\columnwidth}
268 % \begin{tikzpicture}
269 % \node[bgelement,label=east:$v_x$] at (0,0) {1};
270 % \node[bgelement,label=south:m] at (0,-0.5) {I};
271 % \end{tikzpicture}
272 % \end{minipage} \\
273 % \end{tabular}\\[12pt]
274 % Note that the element label colour can be set with the |labelcolour| package option, which is blue by default, or grey when the |grey| package option is used.
275 %
276 % \emph{\bfseries\color{red!70!black}Important note:} the |bgelement| \tkz style is \emph{not} the same as the |\bgelement| text-mode macro. There \emph{is} a |multiport| option/style that works by a lucky stroke, typesetting the node text in blackboard bold font, but it is recommended to instead use the |\bgelement| macro inside the node text for more advanced elements:\\[6pt]
277 % |\tikz \node {\bgelement[multiport,n=2]{RS}} ;| gives \tikz[baseline=(the_elm.base)] \node (the_elm) {\bgelement[multiport,n=2]{RS}} ;
278 %
279 % \clearpage
280 % \subsection{The \texttt{bondgraph} environment}
281 % \DescribeEnv{bondgraph}
282 % The \bgspkg currently defines only one environment: the |bondgraph| environment. It is actually an alias for the |tikzpicture| environment, with two convenient options passed to it automatically: \begin{enumerate}
283 % \item every node gets the style |bgelement|;
284 % \item every edge gets the style |bond| appended to it.
285 % \end{enumerate}
286 % Any options passed to the |bondgraph| environment are passed along to the |tikzpicture| environment. Note that while this environment is a convenient shorthand to quickly draw bondgraphs, the fact that the styles get appended to \emph{every} node and edge mean that it is only suitable for pure bondgraphs---no signal lines, no block diagrams, et cetera, since they would be warped by the bondgraph-specific styles. \\[12pt]
287 % \begin{tabular}{rl}
288 % \begin{minipage}{.7\textwidth}
289 % \begin{verbatim}
290 % \begin{bondgraph}
291 % \node (Se) {Se};
292 % \node[right=1 of Se] (R) {R};
293 % %use `edge': `to' or `--' has no bond style
294 % \draw (Se) edge (R);
295 % \end{bondgraph}
296 % \end{verbatim}
297 % \end{minipage} & \begin{minipage}{.3\textwidth}
298 % \begin{bondgraph}
299 % \node (Se) {Se};
300 % \node[right=1 of Se] (R) {R};
301 % \draw (Se) edge (R);
302 % \end{bondgraph}
303 % \end{minipage}
304 % \end{tabular}
305 %
306 % \clearpage
307 % \section{Examples}\label{sec:examples}
308 % This section contains some basic and more advanced examples of the \bgspkg usage.
309 %
310 % \subsection{Various bonds}
311 % Example picture, showing bonds from and to several nodes, using the \texttt{bond} style and causality (\texttt{\{e,f\}\_\{in,out\}}) options, including bond colours and causal stroke colours:
312 %
313 % \begin{tikzpicture}
314 % \node (a) at (0,0) {a};
315 % \node (b) at (1,0) {b};
316 % \node (c) at (1,1) {c};
317 % \node (d) at (2,0) {d};
318 % \node (e) at (1,-1) {e};
319 % \node (f) at (2,0.9) {f};
320 %
321 % \draw[bond,e_in] (a) -- (b);
322 % \draw[bond,e_out] (b) -- (c);
323 % \draw[bond,e_out={diff}] (d) -- (b);
324 % \draw[bond,red,f_out={blue}] (b) -- (e);
325 % \draw[mbond] (b) -- (f);
326 % \end{tikzpicture}\\
327 % \bgsrc{}
328 % {\color{\bgsrccolor}\begin{verbatim}
329 % \begin{tikzpicture}
330 % \node (a) at (0,0) {a};
331 % \node (b) at (1,0) {b};
332 % \node (c) at (1,1) {c};
333 % \node (d) at (2,0) {d};
334 % \node (e) at (1,-1) {e};
335 % \node (f) at (2,0.9) {f};
336 %
337 % \draw[bond,e_in] (a) -- (b);
338 % \draw[bond,e_out] (b) -- (c);
339 % \draw[bond,e_out={diff}] (d) -- (b);
340 % \draw[bond,red,f_out={blue}] (b) -- (e);
341 % \draw[mbond] (b) -- (f);
342 % \end{tikzpicture}
343 % \end{verbatim}}
344 %
345 % \subsection{bgelement nodes}
346 % Example showing the |bgelement| node style and again bonds between them:
347 %
348 % \begin{tikzpicture}
349 % \node[bgelement] (Se) at (0,0) {Se};
350 % \node[bgelement] (one) at (2,0) {1};
351 % \node[bgelement] (C) at (4,0) {C};
352 %
353 % \draw[bond,f_in] (Se) -- (one);
354 % \draw[f_in={orange},bond] (one) -- (C);
355 % \end{tikzpicture}\\
356 % \bgsrc{}
357 % {\color{\bgsrccolor}\begin{verbatim}
358 % \begin{tikzpicture}
359 % \node[bgelement] (Se) at (0,0) {Se};
360 % \node[bgelement] (one) at (2,0) {1};
361 % \node[bgelement] (C) at (4,0) {C};
362 %
363 % \draw[bond,f_in] (Se) -- (one);
364 % \draw[f_in={orange},bond] (one) -- (C);
365 % \end{tikzpicture}
366 % \end{verbatim}}
367 %
368 % \subsection{Large motor model}
369 %
370 % Large motor model, with a scope that sets the |bgelement| style for each node; and uses a short-hand |edge| for all the bonds, with the |bonds| style that sets the |bond| style for each edge. Uses the |positioning| \tkz{}-library.
371 %
372 % \begin{tikzpicture}[scale=0.7]
373 % \begin{scope}[every node/.style={bgelement}]
374 % \node (Se) at (0,0) {Se};
375 % \node[right=1 of Se] (i) {1};
376 % \node[above=1 of i] (Iel) {I};
377 % \node[below=1 of i] (Rel) {R};
378 % \node[right=1 of i] (GY) {GY};
379 % \node[right=1 of GY] (w) {1};
380 % \node[above=1 of w] (Im) {I};
381 % \node[below=1 of w] (Rm) {R};
382 % \node[right=1 of w] (TF) {TF};
383 % \node[right=1 of TF] (ww) {1};
384 % \node[above=1 of ww,multiport] (Iw) {I};
385 % \end{scope}
386 %
387 % \draw[bonds]
388 % (Se) edge [e_out] (i)
389 % (i) edge [e_in] (Iel)
390 % edge [e_in] (Rel)
391 % edge [e_in] (GY)
392 % (GY) edge [e_out] (w)
393 % (w) edge [e_out] (Im)
394 % edge [e_in] (Rm)
395 % edge [e_in] (TF)
396 % (TF) edge [mbond,e_in] (ww)
397 % (ww) edge [mbond,e_in={diff}] (Iw);
398 % \end{tikzpicture}\\
399 % \bgsrc{}
400 % {\color{\bgsrccolor}\begin{verbatim}
401 % \begin{tikzpicture}[scale=0.7]
402 % \begin{scope}[every node/.style={bgelement}]
403 % \node (Se) at (0,0) {Se};
404 % \node[right=1 of Se] (i) {1};
405 % \node[above=1 of i] (Iel) {I};
406 % \node[below=1 of i] (Rel) {R};
407 % \node[right=1 of i] (GY) {GY};
408 % \node[right=1 of GY] (w) {1};
409 % \node[above=1 of w] (Im) {I};
410 % \node[below=1 of w] (Rm) {R};
411 % \node[right=1 of w] (TF) {TF};
412 % \node[right=1 of TF] (ww) {1};
413 % \node[above=1 of ww,multiport] (Iw) {I};
414 % \end{scope}
415 %
416 % \draw[bonds]
417 % (Se) edge [e_out] (i)
418 % (i) edge [e_in] (Iel)
419 % edge [e_in] (Rel)
420 % edge [e_in] (GY)
421 % (GY) edge [e_out] (w)
422 % (w) edge [e_out] (Im)
423 % edge [e_in] (Rm)
424 % edge [e_in] (TF)
425 % (TF) edge [mbond,e_in] (ww)
426 % (ww) edge [mbond,e_in={diff}] (Iw);
427 % \end{tikzpicture}
428 % \end{verbatim}}
429 %
430 % \clearpage
431 % \subsection{\texttt{bondgraph} environment}
432 %
433 % Example of the same bondgraph, twice: once manually with a \texttt{tikzpicture} environment and the \texttt{every node/.style={bgelement}}; \texttt{every edge/.append style={bond}}; once with the \texttt{bondgraph} environment that is an alias for tikzpicture but sets these styles.
434 %
435 % \begin{tikzpicture}[every node/.style={bgelement},every edge/.append style={bond}]
436 % \node[label=left:$u$] at (0,0) (Se) {Se};
437 % \node[label=below:$i$,right=1 of Se] (i) {1};
438 % \node[label=right:L,above=1 of i] (L) {I};
439 % \node[label=right:\textit{C},right=1 of i] (C) {C};
440 %
441 % \draw
442 % (Se) edge[e_out] (i)
443 % (i) edge[e_out] (L)
444 % edge[f_out] (C);
445 % \end{tikzpicture}\\
446 % \bgsrc{}
447 % {\color{\bgsrccolor}\begin{verbatim}
448 % \begin{tikzpicture}[every node/.style={bgelement},every edge/.append style={bond}]
449 % \node[label=left:$u$] at (0,0) (Se) {Se};
450 % \node[label=below:$i$,right=1 of Se] (i) {1};
451 % \node[label=right:L,above=1 of i] (L) {I};
452 % \node[label=right:\textit{C},right=1 of i] (C) {C};
453 %
454 % \draw
455 % (Se) edge[e_out] (i)
456 % (i) edge[e_out] (L)
457 % edge[f_out] (C);
458 % \end{tikzpicture}
459 % \end{verbatim}}
460 %
461 % \vskip4pt\hfil\rule{.5\textwidth}{.4pt}\hfil\vskip4pt
462 %
463 % \begin{bondgraph}
464 % \node[label=left:$u$] at (0,0) (Se) {Se};
465 % \node[label=below:$i$,right=1 of Se] (i) {1};
466 % \node[label=right:L,above=1 of i] (L) {I};
467 % \node[label=right:\textit{C},right=1 of i] (C) {C};
468 %
469 % \draw
470 % (Se) edge[e_out] (i)
471 % (i) edge[e_out] (L)
472 % edge[f_out] (C);
473 % \end{bondgraph}\\
474 % \bgsrc{}
475 % {\color{\bgsrccolor}\begin{verbatim}
476 % \begin{bondgraph}
477 % \node[label=left:$u$] at (0,0) (Se) {Se};
478 % \node[label=below:$i$,right=1 of Se] (i) {1};
479 % \node[label=right:L,above=1 of i] (L) {I};
480 % \node[label=right:\textit{C},right=1 of i] (C) {C};
481 %
482 % \draw
483 % (Se) edge[e_out] (i)
484 % (i) edge[e_out] (L)
485 % edge[f_out] (C);
486 % \end{bondgraph}
487 % \end{verbatim}}
488 %
489 % \subsection{Bondgraph in text}
490 %
491 % Bondgraph in a line of text looks like this: \begin{bondgraph}[baseline=(Se.base)]
492 % \node(Se){Se};
493 % \node[right=1 of Se] (C) {C};
494 % \draw (Se) edge[e_out={orange}] (C);
495 % \end{bondgraph} and here the sentence continues. Notice the use of \tkz{}'s |baseline| option to align the text with the baseline of the \bgelement{Se}-element. \\[6pt]
496 % \bgsrc{}
497 % {\color{\bgsrccolor}\begin{verbatim}
498 % Bondgraph in a line of text looks like this: \begin{bondgraph}[baseline=(Se.base)]
499 % \node(Se){Se};
500 % \node[right=1 of Se] (C) {C};
501 % \draw (Se) edge[e_out={orange}] (C);
502 % \end{bondgraph} and here the sentence continues.
503 % Notice the use of \tkz{}'s |baseline| option to align the text with
504 % the baseline of the \bgelement{Se}-element.
505 % \end{verbatim}}
506 % \subsection{Bondgaphs mixed with block diagrams}
507 % This is a drawing of a simple mechanical system (a force actuator on a mass), where the flow is integrated to find the position, which is then used as input for a PD-controller.
508 %
509 % \tikzstyle{block}=[minimum width=7mm,minimum height=7mm,draw,thick]
510 % \tikzstyle{plusminus}=[circle,thick,draw,inner sep=0pt,minimum size=3mm]
511 % \tikzstyle{splitter}=[circle,fill,minimum size=2mm,inner sep=0pt]
512 % \tikzstyle{signal} = [-latex, color=red!30!black]
513 % \begin{tikzpicture}
514 % \begin{scope}[every node/.style={bgelement}]
515 % \node (one) {1};
516 % \node[left=1 of one] (MSe) {MSe};
517 % \node[right=1 of one] (I) {I};
518 % \end{scope}
519 % \draw[bonds]
520 % (MSe) edge [e_out] (one)
521 % (one) edge [e_out] (I);
522 %
523 % \node[block,below=.5 of one] (int) {$\int$};
524 %
525 % \node[plusminus,left=2 of MSe] (pp) {};
526 % \node[block,left=.5 of pp] (K) {K$_\text{P}$};
527 % \node[splitter,left=2 of K] (split) {};
528 % \node[block,above=.5 of K] (D) {K$_\text{D}$};
529 % \node[block,left=.5 of D] (ddt) {\small $\frac{\mbox{d}}{\mbox{d}t}$};
530 % \node[plusminus,left=.5 of split] (pm) {};
531 % \node[block,left=.5 of pm] (setpoint) {0};
532 %
533 % \begin{scope}[every path/.style={signal}]
534 % \draw (setpoint) -- node[near end,above]{$+$} (pm);
535 % \draw (pm) -- (split);
536 % \draw (split) |- (ddt);
537 % \draw (ddt) -- (D);
538 % \draw (D) -| node[pos=.9,right]{\small $+$} (pp);
539 % \draw (split) -- (K);
540 % \draw (K) -- node[near end,above]{\small $+$} (pp);
541 % \draw (pp) -- node[below]{$F$} (MSe);
542 % \draw (one) -- (int);
543 % \draw (int) -| node[pos=0.05,below]{$q$} node[pos=.96,left]{\small $-$} (pm);
544 % \end{scope}
545 % \end{tikzpicture}
546 %
547 % \bgsrc{}
548 % {\color{\bgsrccolor}
549 % \begin{verbatim}
550 % \tikzstyle{block}=[minimum width=7mm,minimum height=7mm,draw,thick]
551 % \tikzstyle{plusminus}=[circle,thick,draw,inner sep=0pt,minimum size=3mm]
552 % \tikzstyle{splitter}=[circle,fill,minimum size=2mm,inner sep=0pt]
553 % \tikzstyle{signal} = [-latex, color=red!30!black]
554 % \begin{tikzpicture}
555 % % The bond graph of a mechanical system
556 % \begin{scope}[every node/.style={bgelement}]
557 % \node (one) {1};
558 % \node[left=1 of one] (MSe) {MSe};
559 % \node[right=1 of one] (I) {I};
560 % \end{scope}
561 % \draw[bonds]
562 % (MSe) edge [e_out] (one)
563 % (one) edge [e_out] (I);
564 %
565 % % Position sensor integrator
566 % \node[block,below=.5 of one] (int) {$\int$};
567 %
568 % % The controller
569 % \node[plusminus,left=2 of MSe] (pp) {};
570 % \node[block,left=.5 of pp] (K) {K$_\text{P}$};
571 % \node[splitter,left=2 of K] (split) {};
572 % \node[block,above=.5 of K] (D) {K$_\text{D}$};
573 % \node[block,left=.5 of D] (ddt) {\small $\frac{\mbox{d}}{\mbox{d}t}$};
574 % \node[plusminus,left=.5 of split] (pm) {};
575 % \node[block,left=.5 of pm] (setpoint) {0};
576 %
577 % % Draw all signals
578 % \begin{scope}[every path/.style={signal}]
579 % \draw (setpoint) -- node[near end,above]{$+$} (pm);
580 % \draw (pm) -- (split);
581 % \draw (split) |- (ddt);
582 % \draw (ddt) -- (D);
583 % \draw (D) -| node[pos=.9,right]{\small $+$} (pp);
584 % \draw (split) -- (K);
585 % \draw (K) -- node[near end,above]{\small $+$} (pp);
586 % \draw (pp) -- node[below]{$F$} (MSe);
587 % % The position signal
588 % \draw (one) -- (int);
589 % \draw (int) -| node[pos=0.05,below]{$q$} node[pos=.96,left]{\small $-$} (pm);
590 % \end{scope}
591 % \end{tikzpicture}
592 % \end{verbatim}}
593 %
594 %
595 %
596 % \StopEventually{\PrintChanges \PrintIndex}
597 %
598 % \clearpage
599 % \section{Implementation}
600 %
601 % \subsection{Dependencies}
602 % The package heavily relies on \tkz, but also uses some special fonts to typeset bondgraph elements. Note that \bgs{} uses \textsf{kvoptions} to process package options; that dependency is listed below under \secref{sec:implementation-options}.
603 % \begin{macrocode}
604 %<*package>
605 \NeedsTeXFormat{LaTeX2e}
606 \ProvidesPackage{bondgraphs}[2015/03/23 v1.0.1 TikZ-based Bond graphs formatting package]
607 % \end{macrocode}
608 % \begin{macro}{TikZ}
609 % \tkz does all the drawing for the \bgspkg{}, using a number of libraries: the bond half-arrow is from |arrows|/|arrows.meta| and is drawn using |pathreplacing|; |positioning| isn't really required, but very useful when aligning bondgraph elements; |shapes| is used for the ellipse around word-bondgraph elements.
610 % \begin{macrocode}
611 \RequirePackage{tikz}[2013/12/13]
612 \usetikzlibrary{arrows,arrows.meta,decorations.pathreplacing}
613 \usetikzlibrary{positioning}
614 \usetikzlibrary{shapes}
615 % \end{macrocode}
616 % As mentioned, \textsf{amsfonts} is used for the mathbb font (multi-port elements) and \textsf{bm} is used to typeset bold math (bold mathbb symbols).
617 % \begin{macrocode}
618 \RequirePackage{amsfonts}
619 \RequirePackage{bm}
620 % \end{macrocode}
621 % \end{macro}
622 % \subsection{Options}\label{sec:implementation-options}
623 % \begin{macro}{kvoptions}
624 % The \textsf{kvoptions} package is used to parse package options.
625 % \begin{macrocode}
626 \RequirePackage{kvoptions}
627 \SetupKeyvalOptions{family=bondgraphs,prefix=bondgraphs@}
628 \DeclareStringOption[blue]{labelcolour}
629 \DeclareStringOption[green!50!black]{bondlabelcolour}
630 \DeclareBoolOption[false]{grey}
631 \DeclareComplementaryOption{colour}{grey}
632 % Options for the bond drawing
633 % TODO: curly on double line (multibond) are longer
634 \DeclareBoolOption[false]{curly}
635 \ProcessLocalKeyvalOptions{bondgraphs}
636 % \end{macrocode}
637 % \end{macro}
638 % \begin{macro}{colours}
639 % The \bgspkg{} uses colours for labels on bonds and elements, and also to allow the user to easily indicate a differential or erroneous causality, by colouring the causal stroke. Package option ``grey'' overrides all these colours to grey.
640 % \begin{macrocode}
641 \ifbondgraphs@grey
642 \message{Bondgraphs package: greyscale set}
643 \colorlet{diff}{black!60!white}
644 \colorlet{error}{black!40!white}
645 \def\bondgraphs@labelcolour{black!50!white}
646 \def\bondgraphs@bondlabelcolour{black!50!white}
647 %TODO: would be nice to have this as a style instead of a colour...
648 \else
649 \message{Bondgraphs package: using colour}
650 \colorlet{diff}{orange}
651 \colorlet{error}{red}
652 \fi
653 % \end{macrocode}
654 % \end{macro}
655 % \subsection{Bonds}\label{sec:implementation-bonds}
656 % \begin{macro}{bonds}
657 % All bonds are drawn as a (straight or curly) half-arrow with TikZ. The switch/case statement makes sure that the half-arrow stroke always points down (credits to Mark Wibrow at \url{http://tex.stackexchange.com/questions/208313/tikz-pgf-half-arrow-stroke-always-down-left}). This orientation can be changed to make the stroke always go left or down, never right, by changing the |-1| in |\pgfdecoratedangle-1| to a |+45|. TODO: make this a package option. The argument to this TikZ stile is passed to the draw command, to allow different colour/line width/etc.
658 % \changes{v1.0.1}{2015/03/23}{Better bond and bond label drawing}
659 % \begin{macrocode}
660 \tikzset{bond/.style args={#1}{
661 decoration={show path construction,
662 lineto code={
663 \draw[#1,thick]
664 \pgfextra{% +45 i.s.o. -1 for to left or to under, always
665 \pgfmathparse{int((\pgfdecoratedangle-1)/90)}
666 \ifcase\pgfmathresult
667 \ifbondgraphs@curly
668 \tikzset{-{Classical TikZ Rightarrow[length=3 2 0.8,right]}}
669 \else
670 \tikzset{-{Straight Barb[length=3 2 0.8,right]}}
671 \fi
672 \or
673 \ifbondgraphs@curly
674 \tikzset{-{Classical TikZ Rightarrow[length=3 2 0.8,left]}}
675 \else
676 \tikzset{-{Straight Barb[length=3 2 0.8,left]}}
677 \fi
678 \or
679 \ifbondgraphs@curly
680 \tikzset{-{Classical TikZ Rightarrow[length=3 2 0.8,left]}}
681 \else
682 \tikzset{-{Straight Barb[length=3 2 0.8,left]}}
683 \fi
684 \else
685 \ifbondgraphs@curly
686 \tikzset{-{Classical TikZ Rightarrow[length=3 2 0.8,right]}}
687 \else
688 \tikzset{-{Straight Barb[length=3 2 0.8,right]}}
689 \fi
690 \fi
691 } (\tikzinputsegmentfirst) -- (\tikzinputsegmentlast);
692 }
693 },
694 postaction=decorate
695 },
696 bond/.default={}
697 }
698 % \end{macrocode}
699 % \begin{macro}{mbond}
700 % A multi-bond is simply a bond with a double draw
701 % \begin{macrocode}
702 \tikzset{mbond/.style={bond={double,#1}}}
703 % \end{macrocode}
704 % \end{macro}
705 % \begin{macro}{draw[bonds]}
706 % Convenience function (style) to draw lots of bonds, using |\draw[bonds]|
707 % \begin{macrocode}
708 \tikzset{bonds/.style={every edge/.append style={bond}}}
709 % \end{macrocode}
710 % \end{macro}
711 % \begin{macro}{bond labels}
712 % Provide labelling for effort (above) and flow (below) the bond.
713 % Labels are sloped, so ``above'' is always defined from bond point-of-view
714 % \begin{macrocode}
715 \tikzset{
716 bondlabel/.style={
717 font=\small,
718 color=\bondgraphs@bondlabelcolour,
719 sloped,
720 },
721 effort/.style args={#1}{
722 edge node={node [bondlabel,above]{#1}}
723 },
724 effort/.default={},
725 flow/.style args={#1}{
726 edge node={node [bondlabel,below]{#1}}
727 },
728 flow/.default={}
729 }
730 % \end{macrocode}
731 % \end{macro}
732 % \begin{macro}{causality}
733 % The causal stroke of bonds is drawn as a thick line at the end, or start, of the bond. |e_in| / |f_out| and |f_in| / |e_out| are aliases. The optional argument gets passed on to the draw command of the stroke, e.g. to set its colour.
734 % \begin{macrocode}
735 \tikzset{
736 e_out/.style args={#1}{
737 -{|[line width=1.2pt,width=7pt,#1]}
738 },
739 e_out/.default={black},
740 f_in/.style args={#1}{
741 e_out={#1}
742 },
743 e_in/.style args={#1}{
744 {|[line width=1.2pt,width=7pt,#1]}-
745 },
746 e_in/.default={black},
747 f_out/.style args={#1}{
748 e_in={#1}
749 }
750 }
751 % \end{macrocode}
752 % \end{macro}
753 % \begin{macro}{bond}
754 % A simple command to place a bond in text. Simply draw a bond from |(0,0)| to |(1,0)|, passing on any options to the edge.
755 % \begin{macrocode}
756 \newcommand\bond[1][]{%
757 \tikz \draw(0,0) edge[bond,#1] (1,0);
758 }
759 % \end{macrocode}
760 % \end{macro}
761 % \end{macro}
762 % \subsection{Elements}\label{sec:implementation-elements}
763 % \begin{macro}{elements}
764 % Bondgraph elements in TikZ are typeset in bold. It would be nicer to pass the node text through the |\bgelement| macro that is used for typesetting bondgraph elements in text (see below), but that's not easy. Possibly work around this by using |node contents| instead, as per \url{http://tex.stackexchange.com/questions/209175/filter-tikz-node-text-through-macro}.
765 % \begin{macrocode}
766 \tikzset{
767 bgelement/.style={
768 font=\bfseries,
769 prefix after command= {\pgfextra{
770 \tikzset{every label/.style={
771 % Element label style
772 \bondgraphs@labelcolour,
773 font={\mdseries}
774 }
775 }}}
776 }
777 }
778 % \end{macrocode}
779 % Multiport elements are typeset with the mathbb font. The |\bgroup| and |\egroup| actually do nothing and this construction only works by coincidence as explained by egreg in the aforementioned tex.stackexchange topic. TODO: fix this.
780 % \begin{macrocode}
781 \tikzset{
782 multiport/.style={
783 execute at begin node=$\mathbb\bgroup,
784 execute at end node=\egroup$
785 }
786 }
787 % \end{macrocode}
788 % Word-bg elements: ellipse with text inside
789 % \begin{macrocode}
790 \tikzset{
791 wordbgelement/.style={
792 draw,ellipse,minimum size=12pt,thick,font=\mdseries
793 }
794 }
795 % \end{macrocode}
796 % \begin{macro}{bgelement}
797 % Bondgraph elements in text are typeset much more robustly. |\pgfkeys| is used to parse all options. Currently: |multiport| to typeset in blackboard bold font; |n=x| to add a subscript |x| to indicate n-dimensional elements.
798 % \begin{macrocode}
799 % Must create the if for multiport and wordbg outside of pgfkeys
800 \newif\ifbondgraphs@bgelement@multiport
801 \newif\ifbondgraphs@bgelement@wordbg
802 \pgfkeys{
803 /bgelement/.is family, %automatically cd to /bgelement when present
804 /bgelement/n/.store in=\bondgraphs@bgelement@n,
805 /bgelement/n=1,
806 %
807 /bgelement/multiport/.is if=bondgraphs@bgelement@multiport,
808 /bgelement/multiport=false,
809 /bgelement/wordbg/.is if=bondgraphs@bgelement@wordbg,
810 /bgelement/wordbg=false,
811 }
812 \newcommand\bgelement[2][]{%
813 \pgfkeys{/bgelement,multiport=false,wordbg=false,#1}%
814 \ifbondgraphs@bgelement@wordbg
815 % \end{macrocode}
816 % Word-bg elements have an ellipse around them and hence are drawn with inline \tkz.
817 % \begin{macrocode}
818 \tikz[baseline=(the_elm.base)] \node[wordbgelement] (the_elm) {#2};%
819 \else
820 \ifbondgraphs@bgelement@multiport
821 \ifnum\bondgraphs@bgelement@n=1
822 \ensuremath{\mathbb{#2}}%
823 \else
824 \ensuremath{\mathbb{#2}_{\bondgraphs@bgelement@n}}%
825 \fi
826 \else
827 \ifnum\bondgraphs@bgelement@n=1
828 \textrm{\textbf{#2}}%
829 \else
830 \textrm{\textbf{#2}$_{\bondgraphs@bgelement@n}$}%
831 \fi
832 \fi
833 \fi
834 }
835 % \end{macrocode}
836 % \end{macro}
837 % \end{macro}
838 % \subsection{Environments}\label{sec:implementation-environments}
839 % \begin{environment}{bondgraph}
840 % When drawing pure bondgraphs, it is convenient to use the |bondgraph| environment, which is an alias for tikzpicture but with the proper styles set for nodes and edges.
841 % \begin{macrocode}
842 \newenvironment{bondgraph}[1][]{%
843 \begin{tikzpicture}%
844 [#1,every node/.style={bgelement},every edge/.append style={bond}]%
845 }{%
846 \end{tikzpicture}
847 }
848 %</package>
849 % \end{macrocode}
850 % \end{environment}
851 % \Finale
852 \endinput