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