summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/rigidnotation/rigidnotation.dtx
blob: 565ac2bb50f6a5e698f0e4b1d8447cce0b05b987 (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
% \iffalse meta-comment
%
% Copyright (C) 2024 Philippe Nadeau
% --------------------------------
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% 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.3c or later is part of all distributions of LaTeX
% version 2008-05-04 or later.
%
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}
%<package>\ProvidesPackage{rigidnotation}[2024/05/13 1.0.0 Rigid Notation]
%
%<*driver>
\documentclass{ltxdoc}
% This package is required to build its own documentation!
\usepackage{rigidnotation}
\NewRigidNotation{Vel}{v}
\NewRigidNotation{Acc}{a}
\setlength{\fboxsep}{0pt}
\setlength{\fboxrule}{0.1pt}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{rigidnotation.dtx}
\end{document}
%</driver>
% \fi
%
%
% \def\oargument#1{\texttt{[\meta{#1}]}}
% \def\margument#1{\texttt{\{\meta{#1}\}}}
%
%
% \changes{v1.0}{2024-05-13}{Initial version}
%
% \GetFileInfo{rigidnotation.sty}
%
% \DoNotIndex{}
%
% \title{The \textsf{rigidnotation} package\thanks{This document
% corresponds to \textsf{rigidnotation}~\fileversion,
% dated \filedate.}}
% \author{Philippe Nadeau \\ \texttt{philippe.nadeau [at] robotics.utias.utoronto.ca}}
% \date{\filedate}
%
% \maketitle
%
% \begin{abstract}
% This package provides \LaTeX\ macros to easily and concisely typeset vectors and matrices in a flexible way such as to follow the RIGID notation convention. The package enables the user to define custom commands that can then be used in any math-mode environment to efficiently and rigorously typeset the notational elements commonly used in robotics research (and many other fields) for position vectors, rotation matrices, pose matrices, etc.
% \end{abstract}
%
% \section{Introduction}
% A useful typesetting system should reduce to a minimum the number of keystrokes required by the user while enabling a flexible usage of the system and cater to various needs. This is the philosophy behind this package.
%
% This package makes it easy to typeset vectors and matrices following the RIGID convention, which is compliant with the \textit{ISO 800000 Standard on Quantities and Units}. To do so, this package defines a \textit{command factory} that can be used to concisely generate multiple commands, each one dedicated to a user-defined quantity (e.g. position vector, velocity vector, rotation matrix). The user can then call the generated commands within math-mode environments to typeset a concise representation of the quantity.
%
% For instance, |\Pos[\dot]{s}{b}{w}\Tran| produces
% \begin{equation*} 
%  \resizebox{1cm}{!}{$\Pos[\dot]{s}{b}{w}\Tran$}
% \end{equation*}
% which very concisely represents the time-derivative of the transposed position vector of subject $s$ with respect to basis $b$ when expressed in coordinate system $w$. When typing the keystrokes, the user can think: \textit{position of $s$ with respect to $b$ expressed in $w$, transposed}. Except for the accent, the order in which the elements are typed respects the order in which they can be spoken, making it easier to remember the order of the arguments.  
%
% The \textit{subject} of a quantity is the elements to which the quantity pertains (e.g. the orientation of the subject, the velocity of the subject). The \textit{basis} is the element relative to which the quantity is observed/measured/defined (e.g. the velocity of the car/subject relative to the train/basis). A vector quantity is commonly pictured as an arrow pointing from the basis to the subject. To facilitate computations, quantities are \textit{expressed} in a \textit{coordinate system} such that they can be represented as a set of numbers. Sometime it makes sense to specify only a subset of the four elements: quantity, subject, basis, and coordinate system. This package allows the user to specify only the necessary elements. A \textit{concise mode} can also be enabled such that when the basis and coordinate system are the same, only the basis is denoted.
%
% \section{Usage}
% The typical usage of the package involves defining quantities of interest in the preamble, like 
% \begin{verbatim}
%   \usepackage{rigidnotation}
%   \NewRigidNotation{Vel}{v} % Velocity
%   \NewRigidNotation{Acc}{a} % Acceleration
%   % Whether the concise notation is desired
%   \SetConciseNotation{\BooleanTrue}
% \end{verbatim}
% in which quantities representing velocities and accelerations are defined. Then, in any math-mode environment, variations of the following commands can be used:
% \begin{verbatim}
%   \begin{equation*}
%     1:\Vel \quad 
%     2:\Vel[\hat] \quad
%     3:\Vel[\ddot]{s} \quad 
%     4:\SetConciseNotation \Vel{s}{b}{b} \quad
%     5:\Vel{s}{b}{c} \quad
%     6:\UnsetConciseNotation \Vel{s}{b}{b} \quad
%     7:\Acc{s}{b}\Tran \quad 
%     8:\Acc{s}{b}^2 \quad
%     9:\Rot{s}{b}\Inv \quad 
%   \end{equation*}
% \end{verbatim}
% to produce
% \begin{equation*}
%     1:\Vel \quad 
%     2:\Vel[\hat] \quad
%     3:\Vel[\ddot]{s} \quad 
%     4:\SetConciseNotation \Vel{s}{b}{b} \quad
%     5:\Vel{s}{b}{c} \quad
%     6:\UnsetConciseNotation \Vel{s}{b}{b} \quad
%     7:\Acc{W}{b}\Tran \quad 
%     8:\Acc{W}{b}^2 \quad
%     9:\Rot{W}{b}\Inv
%   \end{equation*}
% where the concise notation was enabled for items 4 and 5 but disabled for element 6 (notice the difference). Also note that command |\Rot| was used even though it was not defined in the preamble. This is because the package comes with a few pre-defined quantities: |\Pos|, |\Rot|, and |\Pose|.
%
% \subsection{Commands}
% In the following, arguments surrounded by square brackets are optional while those surrounded by curly brackets are mandatory like \oargument{optional} and \margument{mandatory} respectively.
%
% \vspace{5mm}\DescribeMacro{\NewRigidNotation}
% \hspace{-6.5mm} \textbf{Arguments}:
% \begin{enumerate}
%   \item \margument{name}: Name of the command to create.
%   \item \margument{symbol}: Symbol used to represent the quantity.
% \end{enumerate}
% \textbf{Description}:\\
% Define a new command that can be used to typeset a physical or mathematical quantity according to the RIGID convention.
% For instance, |\NewRigidNotation{Pos}{p}| can be used to create a new command |\Pos| for typesetting position vectors, with the |p| symbol being used to represent the quantity. After it has been defined, the new command can be used with the following arguments:
% \begin{enumerate}
%   \item \oargument{accent}: Command to put an accent over the letter (e.g. |\hat|).
%   \item \oargument{subject}: The symbol of the \textit{subject} (e.g. vector endpoint).
%   \item \oargument{basis}: The symbol of the \textit{basis} (e.g. vector origin).
%   \item \oargument{coord}: Symbol for the coordinate system potentially used.
% \end{enumerate}
% Hence, this is a kind of meta-command or command factory: |\NewRigidNotation| creates a new command that can be used with arguments. 
%For instance, calling |\NewRigidNotation{Pos}{p}| defines a new command |\Pos| that can be used with zero or more arguments, as in  |\Pos[\dot]{a}{b}{c}|, in which |a| is the subject symbol, |b| is the basis symbol, |c| is the coordinate system, and a dot ($\cdot$) will be placed above the |p| in the notation, producing $\Pos[\dot]{a}{b}{c}$.
%
% \vspace{5mm}\DescribeMacro{\SetConciseNotation}
% \hspace{-6.5mm} \textbf{Arguments}:
% \begin{enumerate}
%   \item \oargument{bool}: Either |\BooleanTrue| or |\BooleanFalse|.
% \end{enumerate}
% \textbf{Description}:\\
% Sets whether to use the concise notation that omits to state the coordinate system when the basis and coordinate systems are the same. Calling |\SetConciseNotation| with no argument will enable the concise notation, which is the same as |\SetConciseNotation{\BooleanTrue}|. Note that, although the argument is optional, curly brackets are used.
% Also note that the concise notation can be enabled/disabled multiple times throughout the document.
%
% \vspace{5mm}\DescribeMacro{\UnsetConciseNotation}
% \hspace{-6.5mm} \textbf{Arguments}: None.\\
% \textbf{Description}:\\
% Informs the package that the concise notation should not be used. This is equivalent to |\SetConciseNotation{\BooleanFalse}| and is the default behaviour.
%
% \vspace{5mm}\DescribeMacro{\Tran}
% \hspace{-6.5mm} Macro that typesets a $\cdot^\mathsf{T}$ in the right super-script position to denote the transpose\
% as in $\Pos{a}{b}{c}\Tran$.
%
% \vspace{5mm}\DescribeMacro{\Inv}
% \hspace{-6.5mm} Macro that typesets a $\cdot^{-1}$ in the right super-script position to denote the inverse\
% as in $\Pos{a}{b}{c}\Inv$.
%
% \vspace{5mm}\DescribeMacro{\Herm}
% \hspace{-6.5mm} Macro that typesets a $\cdot^\mathsf{H}$ in the right super-script position to denote the Hermitian\
% as in $\Rot{a}{b}\Herm$.
%
% \vspace{5mm}\DescribeMacro{\Conj}
% \hspace{-6.5mm} Macro that typesets a $\cdot^\ast$ in the right super-script position to denote the complex conjugate\
% as in $\Pos\Conj$.
%
% \MaybeStop{}
%
% \section{Implementation}
%
% \begin{macro}{\SetConciseNotation}
% Defines whether the concise notation is used or not.
% When used the concise notation will ommit mentionning
% the reference frame when the source and reference are the same.
%
% To use the concise notation, either call |\SetConciseNotation|
% with no argument or with |\SetConciseNotation{\BooleanTrue}|.
%    \begin{macrocode}
\usepackage{xparse}
\usepackage{mathtools}

%This box is used to measure the width of the post-script
\newsavebox{\RN@postbox}
\newlength{\RN@postboxwd}
\newsavebox{\RN@expbox}
\newlength{\RN@expboxwd}
%Spacing before and after the symbol
\def\RN@prespace{0.08em}
\def\RN@postspace{0.23em}

\NewDocumentCommand{\SetConciseNotation}{g}
{
    \newif\IfRN@C
    \IfNoValueTF{#1}
    {
        \RN@Ctrue
    }
    {
        \IfBooleanTF{#1}
        {
            \RN@Ctrue
        }
        {
            \RN@Cfalse
        }
    }
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\UnsetConciseNotation}
% Disables the concise notation.
% This is equivalent to |\SetConciseNotation{\BooleanFalse}|.
%    \begin{macrocode}
\def\UnsetConciseNotation{\SetConciseNotation{\BooleanFalse}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\RN@makecmd}
% \textbf{Arguments}:
% \begin{enumerate}
%   \item \oargument{accent}: Command to put an accent over the letter (e.g. |\hat|).
%   \item \margument{qty}: Symbol for the quantity to represent. 
%   \item \oargument{subject}: The symbol of the \textit{subject} (e.g. vector endpoint).
%   \item \oargument{basis}: The symbol of the \textit{basis} (e.g. vector origin).
%   \item \oargument{coord}: Symbol for the coordinate system potentially used.
% \end{enumerate}
% \textbf{Description}:\\
% Defines a typesetting command from arguments. Main typesetting routine that should not be needed by the end-user. Instead, the end-user should use |\NewRigidNotation| to define typesetting commands. If the basis and coordinate system are the same, the coordinate system is omitted when the |RN@C| keyword is set.
%    \begin{macrocode}
\NewDocumentCommand{\RN@makecmd}{omggg}
{
    \IfNoValueTF{#1}{\def\RN@symb{\boldsymbol{#2}}}{\def\RN@symb{#1{\boldsymbol{#2}}}}
    \IfNoValueTF{#3}
    {%subject is not specified
        \IfNoValueTF{#4}
        {%basis is not specified
            \IfNoValueTF{#5}
            {%subject, basis and coordinate system are not specified
                \RN@symb
            }
            {%subject and basis are not specified, coordinate system is specified
                \hspace{\RN@prespace}
                \prescript{#5}{}{
                    \hspace{-\RN@prespace}
                    \RN@symb
                }
            }
        }
        {%basis is specified
            \IfNoValueTF{#5}%
            {%basis is specified, subject and coordinate system are not specified
                \hspace{\RN@prespace}
                \prescript{}{#4}{
                    \hspace{-\RN@prespace}
                    \RN@symb
                }
            }
            {%basis and coordinate system are specified, subject is not specified
                \IfEq{#4}{#5}
                {
                    \IfRN@C
                    {%basis and coordinate system are the same and RN@C is set
                        \hspace{\RN@prespace}
                        \prescript{}{#4}{
                            \hspace{-\RN@prespace}
                            \RN@symb
                        }
                    }
                    {
                        \hspace{\RN@prespace}
                        \prescript{#5}{#4}{
                            \hspace{-\RN@prespace}
                            \RN@symb
                        }
                    }
                }
                {
                    \hspace{\RN@prespace}
                    \prescript{#5}{#4}{
                        \hspace{-\RN@prespace}
                        \RN@symb
                    }
                }
            }
        }
        %If there is no subject, no space is needed on the right of the symbol
        \setlength{\RN@postboxwd}{0pt}
    }
    {%subject is specified
        \IfNoValueTF{#4}
        {%basis is not specified
            \IfNoValueTF{#5}
            {%subject is specified, basis and coordinate system are not specified
                \RN@symb
                \hspace{-\RN@postspace}
                \prescript{}{#3}{}
            }
            {%basis is not specified, subject and coordinate system are specified
                \hspace{\RN@prespace}
                \prescript{#5}{}{
                    \hspace{-\RN@prespace}
                    \RN@symb
                }
                \hspace{-\RN@postspace}
                \prescript{}{#3}{}
            }
        }
        {%basis is specified
            \IfNoValueTF{#5}
            {%subject and basis are specified, coordinate system is not specified
                \hspace{\RN@prespace}
                \prescript{}{#4}{
                    \hspace{-\RN@prespace}
                    \RN@symb
                }
                \hspace{-\RN@postspace}
                \prescript{}{#3}{}
            }
            {%subject, basis, and coordinate system are specified
                \def\RN@argfour{#4}
                \def\RN@argfive{#5}
                \ifx\RN@argfour\RN@argfive
                {
                    \IfRN@C
                    {%basis and coordinate system are the same and RN@C is set
                        \hspace{\RN@prespace}
                        \prescript{}{#4}{
                            \hspace{-\RN@prespace}
                            \RN@symb
                        }
                        \hspace{-\RN@postspace}
                        \prescript{}{#3}{}
                    }
                    \else
                    {
                        \hspace{\RN@prespace}
                        \prescript{#5}{#4}{
                            \hspace{-\RN@prespace}
                            \RN@symb
                        }
                        \hspace{-\RN@postspace}
                        \prescript{}{#3}{}
                    }
                    \fi
                }
                \else
                {
                    \hspace{\RN@prespace}
                    \prescript{#5}{#4}{
                        \hspace{-\RN@prespace}
                        \RN@symb
                    }
                    \hspace{-\RN@postspace}
                    \prescript{}{#3}{}
                }
                \fi
            }
        }
        %If there is a subject, we need to know the width
        %of the symbol to bring the exponent closer
        \sbox{\RN@postbox}{$#3$}
        \setlength{\RN@postboxwd}{\wd\RN@postbox}
    }
    %If the following command is an exponent-type command, 
    %we bring it closer to the symbol.
    %The next line has to be the last of the macro
    \@ifnextchar^{\hspace{\RN@postspace}\hspace{-\RN@postboxwd}}{}
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\NewRigidNotation}
% \textbf{Arguments}:
% \begin{enumerate}
%   \item \margument{cmd}: New command name.
%   \item \margument{sym}: Symbol for the quantity to represent.
% \end{enumerate}
% \textbf{Description}:
% Defines a new command that can be used to typeset the given symbol using the RIGID convention.
% This essentially expands |\NewRigidNotation{cmd}{sym}| into |\NewDocumentCommand{cmd}{oggg}{\RN@makecmd[#1]{sym}{#2}{#3}{#4}}| such that |\cmd| can be used afterward with up to four arguments.
%    \begin{macrocode}
\NewDocumentCommand{\NewRigidNotation}{mm}%
{%
    \expandafter\NewDocumentCommand\csname #1\endcsname{oggg}%
    {%
        \expandafter\csname RN@makecmd\endcsname[##1]{#2}{##2}{##3}{##4}%
    }%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\Tran}
% Macro that typesets a $\mathsf{T}$ in the right super-script position, closer to the quantity than normal, and with a smaller font size. 
%    \begin{macrocode}
\def\Tran{%
    \sbox{\RN@expbox}{${\scriptscriptstyle\mathsf{T}}$}%
    \setlength{\RN@expboxwd}{\wd\RN@expbox}%
    \ifnum \RN@postboxwd>0%
        \hspace{\RN@postspace}%
        \hspace{-\RN@postboxwd}%
        \ifnum \RN@postboxwd>\RN@expboxwd%
            \mathmakebox[\RN@postboxwd][l]{%
                {}^{\scriptscriptstyle\mathsf{T}}%
            }%
        \else%
            \mathmakebox[\RN@expboxwd][l]{%
                {}^{\scriptscriptstyle\mathsf{T}}%
            }%
        \fi%
    \else%
        ^{\scriptscriptstyle\mathsf{T}}%
    \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\Inv}
% Macro that typesets a $\mathsf{-1}$ in the right super-script position, closer to the quantity than normal, and with a smaller font size.
%    \begin{macrocode}
\def\Inv{%
    \sbox{\RN@expbox}{${\scriptscriptstyle{-1}}$}%
    \setlength{\RN@expboxwd}{\wd\RN@expbox}%
    \ifnum \RN@postboxwd>0%
        \hspace{\RN@postspace}%
        \hspace{-\RN@postboxwd}%
        \ifnum \RN@postboxwd>\RN@expboxwd%
            \mathmakebox[\RN@postboxwd][l]{%
                {}^{\scriptscriptstyle{-1}}%
            }%
        \else%
            \mathmakebox[\RN@expboxwd][l]{%
                {}^{\scriptscriptstyle{-1}}%
            }%
        \fi%
    \else%
        ^{\scriptscriptstyle{-1}}%
    \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\Herm}
% Macro that typesets a $\mathsf{H}$ in the right super-script position, closer to the quantity than normal, and with a smaller font size.
%    \begin{macrocode}
\def\Herm{%
    \sbox{\RN@expbox}{${\scriptscriptstyle\mathsf{H}}$}%
    \setlength{\RN@expboxwd}{\wd\RN@expbox}%
    \ifnum \RN@postboxwd>0%
        \hspace{\RN@postspace}%
        \hspace{-\RN@postboxwd}%
        \ifnum \RN@postboxwd>\RN@expboxwd%
            \mathmakebox[\RN@postboxwd][l]{%
                {}^{\scriptscriptstyle\mathsf{H}}%
            }%
        \else%
            \mathmakebox[\RN@expboxwd][l]{%
                {}^{\scriptscriptstyle\mathsf{H}}%
            }%
        \fi%
    \else%
        ^{\scriptscriptstyle\mathsf{H}}%
    \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\Conj}
% Macro that typesets a $\ast$ in the right super-script position, closer to the quantity than normal, and with a smaller font size.
%    \begin{macrocode}
\def\Conj{%
    \sbox{\RN@expbox}{${\scriptscriptstyle\ast}$}%
    \setlength{\RN@expboxwd}{\wd\RN@expbox}%
    \ifnum \RN@postboxwd>0%
        \hspace{\RN@postspace}%
        \hspace{-\RN@postboxwd}%
        \ifnum \RN@postboxwd>\RN@expboxwd%
            \mathmakebox[\RN@postboxwd][l]{%
                {}^{\scriptscriptstyle\ast}%
            }%
        \else%
            \mathmakebox[\RN@expboxwd][l]{%
                {}^{\scriptscriptstyle\ast}%
            }%
        \fi%
    \else%
        ^{\scriptscriptstyle\ast}%
    \fi%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{Default settings}
% By default, the following commonly used commands are provided. They are used to typeset position vectors, rotation matrices and pose matrices respectively. By default, the concise notation is disabled.
%    \begin{macrocode}
\NewRigidNotation{Pos}{p}
\NewRigidNotation{Rot}{R}
\NewRigidNotation{Pose}{T}
\UnsetConciseNotation
%    \end{macrocode}
% \end{macro}
%
% \Finale
\endinput