summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/non-decimal-units/docs/non-decimal-units.tex
blob: 866efed85a2ee92a5f34dabf891cdf011b0fabe4 (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
% !TEX TS-program = LuaLaTeX-shell-escape
\documentclass[
	a4paper,
	margin=4cm
]{article}

\usepackage{non-decimal-units.preamble}

\title{Non-Decimal Units for \LaTeX}
\author{Mikkel Eide Eriksen\\\href{mailto:mikkel.eriksen@gmail.com}{\texttt{mikkel.eriksen@gmail.com}}}

\nduKeys{
	set aligned for environment=tabular,
	tabularray column type=U,
	unit depth=skilling,
	unit separator={~},
}

\begin{document}

\maketitle

\section{Preface} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Many historical unit systems were non-decimal. For example, the Danish rigsdaler\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.

% https://en.wikipedia.org/wiki/Non-decimal_currency

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.

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.

This package enables creation and configuration of such units to facilitate their presentation in textual and tabular contexts, as well as simple arithmetic.

In order to do this, values are divided into \emph{segments}, which are separated by decimal points: for example, the historical Danish monetary value \nduValue{danish rigsdaler}{1.2.3} is entered as \texttt{1.2.3}, which the code then formats appropriately.

Issues can be reported at \url{https://github.com/mikkelee/latex-units/issues}.

\clearpage
\section{Configuration} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The package is configured in the following manner:

\begin{docCommand}
	{usepackage}
	{\oarg{options}\brackets{non-decimal-units}}

Where \meta{options} may contain one or more of the following unit systems. See \cpageref{units:included} for details.

\begin{description}
\item[british] Currencies
\item[danish] Currencies, areas, and weights
\item[german] Currencies
\end{description}

Alternately, one may configure new units via \refCom{nduNewBaseUnit} and \refCom{nduNewUnitGroup}.

\end{docCommand}

\begin{docCommand}
	{nduKeys}
	{\marg{options}}

	Can be used to set options globally (in the preamble) or locally (in a group). See further documentation for possible keys/values.

\end{docCommand}

\clearpage
\section{Usage} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\subsection{Formatting Values} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

The central macro is \docAuxCommand*{nduValue}. It formats values for display and is configurable in a number of ways.

\begin{docCommand}
	{nduValue}
	{\marg{unit group}\oarg{options}\marg{value}}

Formats \meta{value} according to the setup configured for the \meta{unit group}, as well as any provided \meta{options}.

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.
	
\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduValue} macro
}
\nduValue{danish rigsdaler}{1.2.3}\\
\nduValue{danish rigsdaler}{1}\\
\nduValue{danish rigsdaler}{.2}\\
\nduValue{danish rigsdaler}{..3}\\
\end{dispExample*}
\end{docCommand}

\clearpage
\subsubsection{Options}
	
\begin{docKeys}[
		doc name = display,
	]{
		{
			doc parameter = {=values only},
		},
		{
			doc parameter = {=formatted},
			doc description = initially \texttt{formatted},
		},
		{
			doc parameter = {=symbols only},
		},
		{
			doc parameter = {=numprint},
		},
	}

	Changes which information is included in the expansion.

	Because only present values will be included, \docAuxKey*{display}=\docValue*{symbols only} can be used to list the segment units (though it may be preferable to use \refCom{nduHeader} or \refCom{nduSymbol}).

\begin{dispExample}
\nduValue{danish hartkorn}
	[display=symbols only]
	{0.0.0.0.0}

\nduValue{danish hartkorn}
	[display=values only]
	{0.0...}
\end{dispExample}
	
	Finally, it is possible to use the \href{https://ctan.org/pkg/numprint}{numprint} package to format numbers, especially useful for larger values. Note that this may be counter to the formatting of some units (eg. British pounds), in which case one may prefer the \docAuxKey{use numprint} key instead.

\begin{dispExample}
\nduValue{danish rigsdaler}
	[display=numprint]
	{1000}
\end{dispExample}
\end{docKeys}

\begin{docKey}
	{format}
	{=\marg{...}}
	{initially \docAuxCommand*{VALUE}\docAuxCommand*{nobreakspace}\docAuxCommand*{SYMBOL}}

	Sets how a given base unit should be formatted for display.
	
	The placeholders \docAuxCommand*{VALUE} and \docAuxCommand*{SYMBOL} will be substituted when the value is typeset.
\end{docKey}

\begin{docKeys}
	[]
	{
		{
			doc name = replace nil with,
			doc parameter = {=\meta{...}},
			doc description = {no default, initially empty},
		},
		{
			doc name = treat zero as nil,
			doc description = {initially not set},
		},
	}

The key \docAuxKey*{replace nil with} replaces nil (empty) segments with a custom string.

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.

\end{docKeys}

\begin{docKey}
	{unit depth}
	{=\meta{unit name}}
	{initially no restriction}
	
	When calculating or displaying a value, only the segments up to and including \meta{unit name} will be considered.
	
	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 indeed not restricting it globally).
	
	If the \meta{unit name} is not present in the current unit group, it has no effect.
	
\begin{dispExample}
\nduValue{danish rigsdaler}
	[unit depth=skilling]
	{1.2.3.4.5}

\nduValue{danish rigsdaler}
	[unit depth=penning]
	{1.2.3.4.5}
\end{dispExample}
\end{docKey}

\begin{docKey}
	{unit separator}
	{=\meta{...}}
	{initially \docAuxCommand*{nobreakspace}}
	
	When displaying a value, this string will be inserted between each segment.

\begin{dispExample}
\nduValue{danish hartkorn}[
		display=values only,
		unit separator=.
	]
	{1.2.3.4}

\nduValue{danish rigsdaler}
	[unit separator={---}]
	{1.2.3}
\end{dispExample}

\end{docKey}

\begin{docKey}
	{use numprint}
	{}
	{not set initially}
	
	When displaying a value, use the \href{https://ctan.org/pkg/numprint}{numprint} package, including when using the \docAuxKey{format} key. You can of course also configure the numprint settings, either in a group or locally.

\begin{dispExample}
\begingroup
\selectlanguage{ngerman}
\nduValue{danish rigsdaler}
	[use numprint]
	{1000.0}
\endgroup
\end{dispExample}

\end{docKey}

\clearpage
\section{Arithmetical Operations} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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.

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.

\begin{docCommand}
	{nduMath}
	{\marg{unit name}\oarg{options}\marg{variable}\marg{operator}\marg{value}}

	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.

	Note that mixing unit groups in the same variable is not currently supported, and will likely give incorrect results.

\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduMath} macro
}
\nduMath{danish rigsdaler}{example 1}{+}{0.0.10}
\nduMath{danish rigsdaler}{example 1}{+}{..8}
\nduMath{danish rigsdaler}{example 1}{+}{0.2}
\nduMath{danish rigsdaler}{example 1}{+}{0.5.1}
% there is no output, the result 1.2.3
% will be seen in the following example.
\end{dispExample*}

\end{docCommand}

\begin{docCommand}
	{nduResult}
	{\marg{unit name}\oarg{options}\marg{variable}}

	The \docAuxCommand*{nduResult} macro takes a stored \meta{variable} and formats it via \meta{options} for display in the same way as \refCom{nduValue}.

\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduResult} macro
}
\nduResult{danish rigsdaler}{example 1} % = 1.2.3

And let's add an additional 15 skilling:
\nduMath{danish rigsdaler}{example 1}{+}{0.0.15}
\nduResult{danish rigsdaler}{example 1} % = 1.3.2
\end{dispExample*}

\end{docCommand}

\clearpage
\subsection{Options for Arithmetical Operations}

\begin{docKeys}
	[
		doc parameter = {=\meta{...}},
	]
	{
		{
			doc name = add to variable,
		},
		{
			doc name = subtract from variable,
		},
	}

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.

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{add to variable} key
}
\begingroup
\nduKeys{
	replace nil with=---,
	add to variable=example 2
}
\begin{tabular}{r r}
	\toprule
	& \nduHeader{danish rigsdaler} \\
	\midrule
	a & \nduValue{danish rigsdaler}{1.2.3} \\
	b & \nduValue{danish rigsdaler}{100.1.} \\
	\midrule
	total & \nduResult{danish rigsdaler}{example 2} \\ % = 101.3.3
	\bottomrule
\end{tabular}
\endgroup
\end{dispExample*}
\end{docKeys}

\begin{docKey}
	{normalize}
	{}
	{initially not set}

Reformats an amount, which is useful for quick conversions.

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{normalize} key
}
100 skilling equal
\nduValue{danish rigsdaler}[normalize]{..100} % 1.0.4
\end{dispExample*}
\end{docKey}

\clearpage
\section{Tabular Data} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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 segment in a cell of configurable width.

Additionally, all segments 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 segment, and no nil replacement is specified with the \refKey{replace nil with} key, the cell will be empty.

\begin{docCommand}
	{nduHeader}
	{\marg{unit name}\oarg{options}}
	Convenient header showing the unit symbols. See \cpageref{units:new} for configuration of symbols.
\end{docCommand}

\subsection{Options for Tabular Data}

\begin{docKey}
	{aligned}
	{}
	{initially not set}

Causes each value to be wrapped in right-aligned cells of configurable width.
\end{docKey}

\begin{docKey}
	{cell widths}
	{=\meta{length}}
	{initially \texttt{3em}}

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 \pageref{example:table:widths} for example.
\end{docKey}

\begin{docKeys}
	[]
	{
		{
			doc name = set aligned for environment,
			doc parameter = {=\meta{name}},
			doc description = {initially not set},
		},
		{
			doc name = tabularray column type,
			doc parameter = {=\meta{letter}},
			doc description = {initially not set},
		},
	}

	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 \pageref{example:table:environment} for example.

	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 \pageref{example:table:tabularray} for example\footnote{Thanks to Yukai Chou for help with \href{https://github.com/lvjr/tabularray}{tabularray} integration.}.

\end{docKeys}

\begin{dispExample*}{
	title=Example of tabular data using \docAuxKey{set aligned for environment},
	label=example:table:environment
}
\begingroup
\nduKeys{
%	has been set in this document's preamble:
%	set aligned for environment=tabular,
	treat zero as nil,
	replace nil with=---,
}
\begin{tabular}{r r}
	\toprule
	& \nduHeader{danish rigsdaler} \\
	\midrule
	a & \nduValue{danish rigsdaler}{1.2.3} \\
	b & \nduValue{danish rigsdaler}{100.0.0} \\
	c & \nduValue{danish rigsdaler}{.1.} \\
	\bottomrule
\end{tabular}
\endgroup
\end{dispExample*}

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{cell widths} key,
	label=example:table:widths
}
\begingroup
\nduKeys{
	cell widths={5em, 1.5em},
}
\begin{tabular}{r r}
	\toprule
	& \nduHeader{danish rigsdaler} \\
	\midrule
	a & \nduValue{danish rigsdaler}{1.2.3} \\
	b & \nduValue{danish rigsdaler}{100..} \\
	c & \nduValue{danish rigsdaler}{.1.} \\
	\bottomrule
\end{tabular}
\endgroup
\end{dispExample*}

\begin{dispExample*}{
	title=Example of tabular data using \docAuxKey{tabularray column type},
	label=example:table:tabularray
}
\begingroup
%	has been set in this document's preamble:
%	tabularray column type=U
\begin{tblr}{
	r
	U{danish rigsdaler}{add to variable=table result 1}|
	U{danish rigsdaler}{add to variable=table result 2}
}
	\toprule
	& HEADER & HEADER\\
	\midrule
	a & 1.2.3 & ..15 \\
	b & 100.0.0 & ..10 \\
	c & .1. & ..2 \\
	\midrule
	total & RESULT & RESULT \\
	\bottomrule
\end{tblr}
\endgroup
\end{dispExample*}

\clearpage
\section{Accessing Information About Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{docCommand}
	{nduSymbol}
	{\marg{unit name}}
	Expands to the symbol of the given base unit.
	
	Set by \refKey{units:symbol}.
\end{docCommand}

\begin{docCommand}
	{nduFactor}
	{\marg{unit name}\marg{unit name}}
	Expands to the conversion between two base units.

	Set by \refKey{units:factor}.
\begin{dispExample}
That is, 1 \nduSymbol{rigsdaler} consists of
\nduFactor{rigsdaler}{skilling} \nduSymbol{skilling}.
\end{dispExample}
\end{docCommand}

\clearpage
\section{Creating New Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\label{units:new}
If the included units are not suitable, more can be created. Pull requests are also welcome at \url{https://github.com/mikkelee/latex-units}.

\begin{docCommand}
	{nduNewBaseUnit}
	{\marg{unit name}\marg{key/value pairs}}
	
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).

\end{docCommand}

\begin{docCommand}
	{nduNewUnitGroup}
	{\marg{unit name}\oarg{key/value pairs}\marg{ordered base units}\oarg{control sequence}}

	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{B\rightarrow C} and either \texttt{A\rightarrow C} or \texttt{A\rightarrow B}; if only the latter is configured, an attempt to calculate and cache it will be made internally.

	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. the name of the unit).
	
	Including several sub units may make the math results awkward, as the algorithm is greedy.

\begin{dispExample}
\nduNewUnitGroup{my sletdaler}
	[units/sletdaler/symbol={Sletd.}]
	{sletdaler, ort, skilling}
	[\mySldl]
\mySldl{1.2.3}
\end{dispExample}

\end{docCommand}

\clearpage
\subsection{Options For Base Units}

\begin{docKey}
	[]
	[doc label=units:symbol]
	{units/\meta{unit name}/symbol}
	{=\meta{symbol}}
	{}

	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{}).
\end{docKey}

\begin{docKey}
	[]
	[doc label=units:format]
	{units/\meta{unit name}/format}
	{=\marg{...}}
	{}

	Sets how a given base unit should be formatted for display. If none is given, the general top-level \docAuxKey{format} key is used.
\end{docKey}

\begin{docKey}
	[]
	[doc label=units:factor]
	{units/\meta{unit name}/factor}
	{=\meta{integer} \meta{unit name}}
	{}
	
	The conversion factor of a unit is how many of an underlying unit the given unit consists of. This can be specified multiple times.
	
	This is used by the math macros and keys to calculate the sums and products.
	
	Can be accessed via \refCom{nduFactor}.
\end{docKey}

These keys can of course also be set temporarily in \refCom{nduValue}

\begin{dispExample}
\nduValue{danish rigsdaler}
	[units/mark/symbol=Mk.]
	{.9.}

\nduValue{danish rigsdaler}
	[units/rigsdaler/format={\VALUE~Rigsdaler og}]
	{1.2.3}

\nduValue{danish rigsdaler}[
		unit separator={---},
		units/rigsdaler/format={(\VALUE)},
		units/mark/format={[\VALUE]},
		units/skilling/format={\{\VALUE\}},
	]
	{1.2.3}
\end{dispExample}

\clearpage
\section{Included Units} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\label{units:included}
On the following pages are the units included with the package.

\unitlisting{british}

\clearpage
\unitlisting{danish}

\unitlisting{german}

\printindex  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}