Debian Codegraph @

texmf-dist/doc/latex/non-decimal-units/non-decimal-units.tex

raw
1 % !TEX TS-program = LuaLaTeX-shell-escape
2 \documentclass[
3 a4paper,
4 ]{article}
5
6 \usepackage{non-decimal-units.preamble}
7
8 \title{Non-Decimal Units for \LaTeX}
9 \author{Mikkel Eide Eriksen\\\href{mailto:[email protected]}{\texttt{[email protected]}}}
10
11 \nduKeys{
12 set aligned for environment=tabular,
13 tabularray column type=U,
14 unit depth=skilling,
15 unit separator={~},
16 }
17
18 \begin{document}
19
20 \maketitle
21
22 \section{Preface} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
23
24 Many historical unit systems were non-decimal. For example, the Danish rigsdaler currency\footnote{\url{https://en.wikipedia.org/wiki/Danish_rigsdaler}} --- where 1 rigsdaler consists of 16~mark, each again consisting of 16~skilling for a total of 96 skilling per rigsdaler --- was used from 1625 to 1875, when currency was decimalised to the current system of 1 krone = 100 øre.
25
26 % https://en.wikipedia.org/wiki/Non-decimal_currency
27
28 Units for such measures as length, area, weight, and so on were also often non-decimal, and in fact remain so in the few places of the world that have not made the change to the metric system.
29
30 The non-decimal numbers were chosen due to their larger number of division factors, which simplified mental arithmetic --- eg. when sharing an amount of money or dividing goods.
31
32 This package enables creation and configuration of such units to facilitate their presentation in textual and tabular contexts, as well as simple arithmetic.
33
34 In order to do this, a \emph{unit group} consists of a number of \emph{base unit}s: for example, I can use \cs{nduValue}\nduDocBrack{danish rigsdaler}\nduDocBrack{1.2.3} to typeset the result \nduValue{danish rigsdaler}{1.2.3}, or \cs{nduValue}\nduDocBrack{british pound sterling lsd}\nduDocBrack{1.2.3} to typeset \nduValue{british pound sterling lsd}{1.2.3}.
35
36 Issues can be reported at\\\null\hfill\url{https://github.com/mikkelee/latex-units/issues}
37
38 \clearpage
39 \section{Configuration} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
41 The package is configured in the following manner:
42
43 \begin{docCommand}
44 {usepackage}
45 {\oarg{options}\brackets{non-decimal-units}}
46
47 Where \meta{options} may contain one or more of the following unit systems. See \cpageref{units:included} for details.
48
49 \begin{description}
50 \item[british] Currencies
51 \item[danish] Currencies, areas, and weights
52 \item[german] Currencies
53 \end{description}
54
55 Alternately, one may configure new units via \refCom{nduNewBaseUnit} and \refCom{nduNewUnitGroup}. Suggestions for additions are welcome!
56
57 \end{docCommand}
58
59 \begin{docCommand}
60 {nduKeys}
61 {\marg{options}}
62
63 Can be used to set options globally (in the preamble) or locally (in a group). See further documentation for possible keys/values.
64
65 \end{docCommand}
66
67 \clearpage
68 \section{Usage} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
69
70 \subsection{Formatting Values} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
72 The central macro is \docAuxCommand*{nduValue}. It formats values for display and is configurable in a number of ways.
73
74 \begin{docCommand}
75 {nduValue}
76 {\marg{unit group}\oarg{options}\marg{value}}
77
78 Formats \meta{value} according to the setup configured for the \meta{unit group}, as well as any provided \meta{options}.
79
80 If no special configuration is made, the number of decimal points and the values between them determine how many and which units are displayed. For example, empty values are skipped unless the \refKey{replace nil with} key is set.
81
82 \begin{dispExample*}{
83 title=Example usage: \docAuxCommand*{nduValue} macro
84 }
85 \nduValue{danish rigsdaler}{1.2.3}\\
86 \nduValue{danish rigsdaler}{1}\\
87 \nduValue{danish rigsdaler}{.2}\\
88 \nduValue{danish rigsdaler}{..3}\\
89 \end{dispExample*}
90 \end{docCommand}
91
92 \clearpage
93 \subsubsection{Options}
94
95 \begin{docKeys}[
96 doc name = display,
97 ]{
98 {
99 doc parameter = {=values only},
100 },
101 {
102 doc parameter = {=formatted},
103 doc description = initially \texttt{formatted},
104 },
105 {
106 doc parameter = {=symbols only},
107 },
108 }
109
110 Changes which information is included in the expansion.
111
112 Because only present values will be included, \docAuxKey*{display}=\docValue*{symbols only} can be used to list the unit symbols (though it may be preferable to use \refCom{nduHeader} or \refCom{nduSymbol}).
113
114 \begin{dispExample}
115 \nduValue{danish hartkorn}
116 [display=symbols only]
117 {0.0.0.0.0}
118
119 \nduValue{danish hartkorn}
120 [display=values only]
121 {0.0...}
122 \end{dispExample}
123
124 \end{docKeys}
125
126 \begin{docKey}
127 {format}
128 {=\marg{...}}
129 {initially \docAuxCommand*{VALUE}\docAuxCommand*{nobreakspace}\docAuxCommand*{SYMBOL}}
130
131 Sets how a given base unit should be formatted for display.
132
133 The placeholders \docAuxCommand*{VALUE} and \docAuxCommand*{SYMBOL} will be substituted when the value is typeset.
134 \end{docKey}
135
136 \begin{docKeys}
137 []
138 {
139 {
140 doc name = replace nil with,
141 doc parameter = {=\meta{...}},
142 doc description = {no default, initially empty},
143 },
144 {
145 doc name = treat zero as nil,
146 doc description = {initially not set},
147 },
148 }
149
150 The key \docAuxKey*{replace nil with} replaces nil (empty) unit values with a custom string.
151
152 The key \docAuxKey*{treat zero as nil} replaces 0 with nothing, which in turn means that setting both will replace both zero and nil with the custom string.
153
154 \end{docKeys}
155
156 \begin{docKey}
157 {unit depth}
158 {=\meta{unit name}}
159 {initially no restriction}
160
161 When calculating or displaying a value, only the units up to and including \meta{unit name} will be considered.
162
163 In this document, the depth has been globally set to \texttt{skilling}, but older historical sub-units can be included by locally setting the depth to eg. \texttt{hvid} or \texttt{penning} (or indeed not restricting it globally).
164
165 If the \meta{unit name} is not present in the selected unit group, it has no effect.
166
167 \begin{dispExample}
168 \nduValue{danish rigsdaler}
169 [unit depth=skilling]
170 {1.2.3.4.5}
171
172 \nduValue{danish rigsdaler}
173 [unit depth=penning]
174 {1.2.3.4.5}
175 \end{dispExample}
176 \end{docKey}
177
178 \begin{docKey}
179 {unit separator}
180 {=\meta{...}}
181 {initially \docAuxCommand*{nobreakspace}}
182
183 When displaying a value, this string will be inserted between each sub-unit.
184
185 \begin{dispExample}
186 \nduValue{danish hartkorn}[
187 display=values only,
188 unit separator=.
189 ]
190 {1.2.3.4}
191
192 \nduValue{danish rigsdaler}
193 [unit separator={---}]
194 {1.2.3}
195 \end{dispExample}
196
197 \end{docKey}
198
199 \begin{docKey}
200 {use numprint}
201 {}
202 {not set initially}
203
204 When typesetting numeric values, use the \href{https://ctan.org/pkg/numprint}{numprint} package. You can of course also configure the numprint settings, either in a group or locally.
205
206 \begin{dispExample}
207 \begingroup
208 \nduKeys{use numprint}
209
210 \selectlanguage{ngerman}
211 Danish/German:
212 \nduValue{danish rigsdaler}{1000}
213
214 \selectlanguage{english}
215 British/English:
216 \nduValue{british pound sterling lsd}{1000}
217 \endgroup
218 \end{dispExample}
219
220 \end{docKey}
221
222 \clearpage
223 \section{Arithmetical Operations} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224
225 Basic arithmetic functions can be used to build a result for display. This is done by converting the value to an internal representation and storing it in a global variable. The first time a variable is used, it is assumed that the value is 0.
226
227 Results can be gathered in two ways, either manually via the \docAuxCommand*{nduMath} macro, or automatically via the \docAuxKey{add to variable} and \docAuxKey{subtract from variable} keys, the latter two being especially suitable in tabular contexts.
228
229 \begin{docCommand}
230 {nduMath}
231 {\marg{unit group}\oarg{options}\marg{variable}\marg{operator}\marg{value}}
232
233 The first arguments of \docAuxCommand*{nduMath} are identical to those of the \refCom{nduValue} macro. In addition, it has \meta{variable} and \meta{operator} (one of \texttt{+ - * /}) arguments. The command does not expand to any output.
234
235 Note that mixing unit groups in the same variable is not currently supported, and will likely give incorrect results.
236
237 \begin{dispExample*}{
238 title=Example usage: \docAuxCommand*{nduMath} macro
239 }
240 \nduMath{danish rigsdaler}{example 1}{+}{0.0.10}
241 \nduMath{danish rigsdaler}{example 1}{+}{..8}
242 \nduMath{danish rigsdaler}{example 1}{+}{0.2}
243 \nduMath{danish rigsdaler}{example 1}{+}{0.5.1}
244 % there is no output, the result 1.2.3
245 % will be seen in the following example.
246 \end{dispExample*}
247
248 \end{docCommand}
249
250 \begin{docCommand}
251 {nduResult}
252 {\marg{unit group}\oarg{options}\marg{variable}}
253
254 The \docAuxCommand*{nduResult} macro takes a stored \meta{variable} and formats it via \meta{options} for display in the same way as \refCom{nduValue}.
255
256 \begin{dispExample*}{
257 title=Example usage: \docAuxCommand*{nduResult} macro
258 }
259 \nduResult{danish rigsdaler}{example 1} % = 1.2.3
260
261 And let's multiply by two:
262 \nduMath{danish rigsdaler}{example 1}{*}{3}
263 \nduResult{danish rigsdaler}{example 1} % = 4.0.9
264 \end{dispExample*}
265
266 \end{docCommand}
267
268 \begin{docCommand}
269 {nduNormalize}
270 {\marg{unit group}\oarg{options}\marg{amount}\marg{unit}}
271
272 The \docAuxCommand*{nduNormalize} reformats an \meta{amount} of \meta{unit}s according to the given \meta{options}, in the same way as \refCom{nduValue}.
273
274 \begin{dispExample*}{
275 title=Example usage: \docAuxCommand*{nduNormalize} macro
276 }
277 \nduNormalize{danish rigsdaler}{123}{skilling} % = 1.1.11
278
279 \nduNormalize{danish rigsdaler}
280 [unit depth=penning]{123}{penning} % = 0.0.10.0.3
281
282 \nduNormalize{danish rigsdaler}
283 [treat zero as nil]{100}{skilling} % = 1..4
284 \end{dispExample*}
285
286 \end{docCommand}
287
288 \clearpage
289 \subsection{Options for Arithmetical Operations}
290
291 \begin{docKeys}
292 [
293 doc parameter = {=\meta{...}},
294 ]
295 {
296 {
297 doc name = add to variable,
298 },
299 {
300 doc name = subtract from variable,
301 },
302 }
303
304 Setting either of these keys will cause all uses of \docAuxCommand*{nduValue} in the current group to be added to or subtracted from the variable with the given name. It can of course also be set on individual invocations of the command.
305
306 \begin{dispExample*}{
307 title=Example usage: \docAuxKey*{add to variable} key
308 }
309 \begingroup
310 \nduKeys{
311 replace nil with=---,
312 add to variable=example 2
313 }
314 \begin{tabular}{r r}
315 \toprule
316 & \nduHeader{danish rigsdaler} \\
317 \midrule
318 a & \nduValue{danish rigsdaler}{1.2.3} \\
319 b & \nduValue{danish rigsdaler}{100.1.} \\
320 \midrule
321 total & \nduResult{danish rigsdaler}{example 2} \\
322 % = 101.3.3
323 \bottomrule
324 \end{tabular}
325 \endgroup
326 \end{dispExample*}
327 \end{docKeys}
328
329 \begin{docKey}
330 {normalize}
331 {}
332 {initially not set}
333
334 Reformats an amount, which is useful for quick conversions.
335
336 \begin{dispExample*}{
337 title=Example usage: \docAuxKey*{normalize} key
338 }
339 100 skilling equal
340 \nduValue{danish rigsdaler}[normalize]{..100} % 1.0.4
341 \end{dispExample*}
342 \end{docKey}
343
344 \clearpage
345 \section{Tabular Data} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346
347 There are a couple of ways to display values in tabular context, centered around explicitly or implicitly setting the \docAuxKey{aligned} key, which causes \docAuxCommand{nduValue} to wrap each sub-unit in a cell of configurable width.
348
349 Additionally, all units will be included in headers and cells, whether they contain a value or not (provided \docAuxKey{unit depth} allows it). If no value is provided for the sub-unit, and no nil replacement is specified with the \refKey{replace nil with} key, the cell will be empty.
350
351 \begin{docCommand}
352 {nduHeader}
353 {\marg{unit group}\oarg{options}}
354 Convenient header showing the unit symbols. See \cref{sec:units:new} for configuration of symbols.
355 \end{docCommand}
356
357 \subsection{Options for Tabular Data}
358
359 \begin{docKey}
360 {aligned}
361 {}
362 {initially not set}
363
364 Causes each value to be wrapped in right-aligned cells of configurable width.
365 \end{docKey}
366
367 \begin{docKey}
368 {cell widths}
369 {=\meta{length}}
370 {initially \texttt{3em}}
371
372 Changes the width of each cell. One may supply a bracketed comma-separated list of widths. If the list is shorter than the number of base units in the group, the last width will be repeated. See page \cpageref{example:table:widths} for example.
373 \end{docKey}
374
375 \begin{docKeys}
376 []
377 {
378 {
379 doc name = set aligned for environment,
380 doc parameter = {=\meta{name}},
381 doc description = {initially not set},
382 },
383 {
384 doc name = tabularray column type,
385 doc parameter = {=\meta{letter}},
386 doc description = {initially not set},
387 },
388 }
389
390 The \docAuxKey{set aligned for environment} key can be set to an environment name, causing \docAuxKey*{aligned} to automatically be set for those enviroments, using \docAuxCommand*{AtBeginEnvironment}. It can be set multiple times, once for each required environment. See page \cpageref{example:table:environment} for example.
391
392 The \docAuxKey{tabularray column type} key can be used to create a column type, which automatically wraps the column contents in \docAuxCommand{nduValue}. The column type takes two arguments, a unit group and a set of key values for further configuration. Additionally, the special values \texttt{HEADER}, \texttt{RESULT}, and \texttt{SKIP} will respectively use \docAuxCommand{nduHeader}, \docAuxCommand{nduResult}, and skip the cell. See page \cpageref{example:table:tabularray} for example\footnote{Thanks to Yukai Chou for help with \href{https://github.com/lvjr/tabularray}{tabularray} integration.}.
393
394 \end{docKeys}
395
396 \begin{dispExample*}{
397 title=Example usage: \docAuxKey{set aligned for environment} key,
398 label=example:table:environment
399 }
400 \begingroup
401 \nduKeys{
402 % has been set in this document's preamble:
403 % set aligned for environment=tabular,
404 treat zero as nil,
405 replace nil with=---,
406 }
407 \begin{tabular}{r r}
408 \toprule
409 & \nduHeader{danish rigsdaler} \\
410 \midrule
411 a & \nduValue{danish rigsdaler}{1.2.3} \\
412 b & \nduValue{danish rigsdaler}{100.0.0} \\
413 c & \nduValue{danish rigsdaler}{.1.} \\
414 \bottomrule
415 \end{tabular}
416 \endgroup
417 \end{dispExample*}
418
419 \begin{dispExample*}{
420 title=Example usage: \docAuxKey*{cell widths} key,
421 label=example:table:widths
422 }
423 \begingroup
424 \nduKeys{
425 cell widths={5em, 1.5em},
426 }
427 \begin{tabular}{r r}
428 \toprule
429 & \nduHeader{danish rigsdaler} \\
430 \midrule
431 a & \nduValue{danish rigsdaler}{1.2.3} \\
432 b & \nduValue{danish rigsdaler}{100..} \\
433 c & \nduValue{danish rigsdaler}{.1.} \\
434 \bottomrule
435 \end{tabular}
436 \endgroup
437 \end{dispExample*}
438
439 \begin{dispExample*}{
440 title=Example usage: \docAuxKey{tabularray column type} key,
441 label=example:table:tabularray
442 }
443 % has been set in this document's preamble:
444 % tabularray column type=U
445 \begin{tblr}{
446 r
447 U{danish rigsdaler}{add to variable=table result 1}|
448 U{danish rigsdaler}{add to variable=table result 2}
449 }
450 \toprule
451 & HEADER & HEADER \\
452 \midrule
453 a & 1.2.3 & ..15 \\
454 b & 100.0.0 & ..10 \\
455 c & .1. & ..2 \\
456 \midrule
457 total & RESULT & SKIP \\
458 \bottomrule
459 \end{tblr}
460
461 Result from \texttt{table result 2}:
462 \nduResult{danish rigsdaler}{table result 2}
463 \end{dispExample*}
464
465 \clearpage
466 \section{Accessing Information About Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
467
468 \begin{docCommand}
469 {nduSymbol}
470 {\marg{unit name}}
471 Expands to the symbol of the given base unit.
472
473 Set by \refKey{units:symbol}.
474 \end{docCommand}
475
476 \begin{docCommand}
477 {nduFactor}
478 {\marg{unit name}\marg{unit name}}
479 Expands to the conversion between two base units.
480
481 Set by \refKey{units:factor}.
482 \begin{dispExample}
483 That is, 1 \nduSymbol{rigsdaler} consists of
484 \nduFactor{rigsdaler}{skilling} \nduSymbol{skilling}.
485 \end{dispExample}
486 \end{docCommand}
487
488 \clearpage
489 \section{Creating New Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
490
491 \label{sec:units:new}
492 If the included units are not sufficient, more can be created. Pull requests are also welcome at \url{https://github.com/mikkelee/latex-units}.
493
494 \begin{docCommand}
495 {nduNewBaseUnit}
496 {\marg{unit name}\marg{key/value pairs}}
497
498 Creates a new base unit. It must contain at least a \docAuxKey{symbol}, but a \docAuxKey{factor} is also required for the math functions (see below).
499
500 \end{docCommand}
501
502 \begin{docCommand}
503 {nduNewUnitGroup}
504 {\marg{group name}\oarg{key/value pairs}\marg{ordered base units}\oarg{control sequence}}
505
506 In order for the math functions to work, every base unit in the group must have a conversion path to the right-most base unit, eg. if a unit group consists of base units \texttt{A, B, C}, there must be defined factors for \texttt{A\rightarrow B} and either \texttt{A\rightarrow C} or \texttt{B\rightarrow C}; if only the \texttt{A\rightarrow B} and \texttt{B\rightarrow C} factors are configured, an attempt to calculate and cache \texttt{A\rightarrow C} will be made internally.
507
508 It is possible to create shortcut macros for commonly used unit groups with optional overriding options. These macros take the same arguments as the full \refCom{nduValue} macro, except without the first argument (ie. no need for the \meta{unit group}).
509
510 Including several sub units may make the math results awkward, as the algorithm is greedy.
511
512 \begin{dispExample}
513 \nduNewUnitGroup{my sletdaler}
514 [units/sletdaler/symbol={Sletd.}]
515 {sletdaler, ort, skilling}
516 [\mySldl]
517 \mySldl[unit separator={~/~}]{1.2.3}
518 \end{dispExample}
519
520 \end{docCommand}
521
522 \clearpage
523 \subsection{Options For Base Units}
524
525 When creating new units via \refCom{nduNewBaseUnit}, only the last part of the below keys is used (eg. \refKey*{units:factor} is simply \docAuxKey*{factor}).
526
527 \begin{docKey}
528 []
529 [doc label=units:factor]
530 {units/\meta{unit name}/factor}
531 {=\meta{integer} \meta{unit name}}
532 {}
533
534 The conversion factor of a unit is how many of an underlying unit the given unit consists of. This can be specified multiple times.
535
536 This is used by the math macros and keys to calculate the sums and products, but is not necessary for display.
537
538 Can be accessed via \refCom{nduFactor}.
539 \end{docKey}
540
541 \begin{docKey}
542 []
543 [doc label=units:format]
544 {units/\meta{unit name}/format}
545 {=\marg{...}}
546 {}
547
548 Sets how a given base unit should be formatted for display. If none is given, the general top-level \docAuxKey{format} key is used.
549 \end{docKey}
550
551 \begin{docKey}
552 []
553 [doc label=units:symbol]
554 {units/\meta{unit name}/symbol}
555 {=\meta{symbol}}
556 {}
557
558 Configures a symbol displaying the unit. This is used in \docAuxCommand{nduValue}, \docAuxCommand{nduHeader}, and is also available via \docAuxCommand*{SYMBOL} when defining the \refKey{units:format} (see also \refKey{display}).
559 \end{docKey}
560
561 These keys can of course be set temporarily in \refCom{nduValue}, as seen below.
562
563 \begin{dispExample}
564 \nduValue{danish rigsdaler}
565 [units/mark/symbol=Mk.]
566 {.9.}
567
568 \nduValue{danish rigsdaler}
569 [units/rigsdaler/format={\VALUE~Rigsdaler og}]
570 {1.2.3}
571
572 \nduValue{danish rigsdaler}[
573 unit separator={---},
574 units/rigsdaler/format={(\VALUE)},
575 units/mark/format={[\VALUE]},
576 units/skilling/format={\{\VALUE\}},
577 ]
578 {1.2.3}
579 \end{dispExample}
580
581 \clearpage
582 \section{Included Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
583
584 \label{units:included}
585 On the following pages are the units included with the package.
586
587 \unitlisting{british}
588
589 \clearpage
590 \unitlisting{danish}
591
592 \unitlisting{german}
593
594 \printindex %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
595
596 \end{document}