summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/non-decimal-units/docs/non-decimal-units.tex
blob: d5490aa02152dd91894af59230f2a95c8c2e4358 (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
% !TEX TS-program = LuaLaTeX-shell-escape
\documentclass{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}}}

\nduset{
	set aligned for environment=tabular,
	aliases/danish rigsdaler,
	restrict segment depth=2,
}

\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 \nduName{danish rigsdaler}{0} consists of \nduFactor{danish rigsdaler}{1}~\nduName{danish rigsdaler}{1}, each again consisting of \nduFactor{danish rigsdaler}{2}~\nduName{danish rigsdaler}{2} for a total of 96 \nduName{danish rigsdaler}{2} per \nduName{danish rigsdaler}{0} --- 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} but keep in mind I am not very experienced with \LaTeX ;)

\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 and areas
\item[german] Currencies
\end{description}

Alternately, one may configure new units via \refCom{nduNewUnit}.

\end{docCommand}

\begin{docCommand}
	{nduset}
	{\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 name}\oarg{options}\marg{value}}

Formats \meta{value} according to the setup configured for the \meta{unit name}, as well as any provided \meta{options}. The number of decimal points and the values between them determine how many and which segments are displayed.

Empty segments 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 = show,
	]{
		{
			doc parameter = {=values},
		},
		{
			doc parameter = {=values and symbols},
			doc description = initially \texttt{values and symbols},
		},
		{
			doc parameter = {=symbols},
		},
	}

Changes which information is included in the expansion.

Because only those segments with a value will be included, \docAuxKey*{show}=\docValue*{symbols} can be used to list the segment units (though if only one or two is needed, it may be preferable to use \refCom{nduSymbol}).

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

\nduValue{danish hartkorn}
	[show=symbols]
	{0.0...}
\end{dispExample}
\end{docKeys}

See also \cref{units:new} for further discussion on possible options.

\clearpage
\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.

These keys also apply in non-tabular contexts, but are probably most useful here.

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{replace nil with} key
}
\begingroup
\nduset{
	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*}
\end{docKeys}


\clearpage
\subsection{Tabular Data} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

In order to align values in a tabular context, the \docAuxKey*{aligned} key causes \docAuxCommand{nduValue} to wrap each segment in a cell of equal width, using  \docAuxCommand*{makebox}.

Additionally, the \docAuxCommand*{nduHeader} macro provides a convenient header showing the unit symbols.

All segments will be included in the headers and cells, whether they contain a value or not. 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}}
	Formats the unit symbols in boxes suitable for a header. See \cpageref{units:new} for configuration of symbols.
\end{docCommand}

\clearpage
\subsubsection{Options}

\begin{docKeys}
	[]
	{
		{
			doc name = aligned,
			doc description = {initially not set},
		},
		{
			doc name = set aligned for environment,
			doc description = {initially set for \texttt{tabular}},
		},
	}

	Setting \docAuxKey*{aligned} will format the presently displayed header in aligned boxes, desirable in tabular contexts.

	Additionally, the \docAuxKey*{set aligned for environment} key can be set to an environment name, causing \docAuxKey*{aligned} to automatically be set aligned for those enviroments, using \docAuxCommand*{AtBeginEnvironment}. It can be set multiple times, once for each required environment.

\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduHeader} and \docAuxCommand*{nduValue} macros with \docAuxKey*{aligned} key.
}
\begingroup
\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*}

\end{docKeys}

\begin{docKey}
	{cell width}
	{=\meta{length}}
	{initially \texttt{5em}}

Changes the width of each segment.

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{cell width} key
}
\begingroup
\nduset{
	cell width=3em,
}
\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*}
\end{docKey}

\clearpage
\subsection{Arithmetical Operations} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Basic arithmetic functions can be used to build a result for display. Internally, this is done by converting the value to a representation, which is the total number of the smallest usable unit, eg. \nduValue{danish rigsdaler}{1.2.3} is 131 \nduName{danish rigsdaler}{2}.

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 being especially suitable in tabular contexts.

\begin{docCommands}[]{
	{
		doc name = nduMath,
		doc parameter = \marg{unit name}\oarg{options}\marg{variable}\marg{operator}\marg{value}
	},
	{
		doc name = nduResult,
		doc parameter = \marg{unit name}\oarg{options}\marg{variable}
	}
}

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 first time a variable is used, it is assumed that the value is 0. The given value is then converted to its internal representation and stored in the variable. The command does not expand to any output.

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

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

Both may be further configured via the \meta{options} in the same way as the other macros.

\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduMath} and \docAuxCommand*{nduResult} macros
}
\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}
\nduResult{danish rigsdaler}{example 1} % = 1.2.3
\end{dispExample*}

\begin{dispExample*}{
	title=Example usage: \docAuxCommand*{nduResult} macro
}
\nduHeader{danish rigsdaler}\\
\nduResult{danish rigsdaler}[aligned]{example 1}
\end{dispExample*}
\end{docCommands}

\clearpage
\subsubsection{Options}

\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.

\begin{dispExample*}{
	title=Example usage: \docAuxKey*{add to variable} key
}
\begingroup
\nduset{
	cell width=3em,
	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.} \\
	\bottomrule
	total & \nduResult{danish rigsdaler}{example 2} \\ % = 101.3.3
\end{tabular}
\endgroup
\end{dispExample*}

Results are global and remain accessible outside the group:
\begin{dispExample}
\nduResult{danish rigsdaler}{example 2}
\end{dispExample}

Adding an additional 15 \nduName{danish rigsdaler}{2} to the existing result gives:
\begin{dispExample}
\nduMath{danish rigsdaler}{example 2}{+}{0.0.15}
\nduResult{danish rigsdaler}{example 2} % = 101.4.2
\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 \nduName{danish rigsdaler}{2} equal
\nduValue{danish rigsdaler}[normalize]{..100} % 1.0.4
\end{dispExample*}
\end{docKey}


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

\begin{docCommand}
	{nduName}
	{\marg{unit name}\marg{segment}}
	Expands to the name of the the given segment of the unit.
	
	Set by \refKey{segment:name}.
\end{docCommand}

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

\begin{docCommand}
	{nduFactor}
	{\marg{unit name}\marg{segment}}
	Expands to the conversion factor of the the given segment of the unit, ie. how many of the underlying segment the given segment consists of.

\begin{dispExample}
That is, 1 \nduName{danish rigsdaler}{0} consists of
\nduFactor{danish rigsdaler}{1} \nduName{danish rigsdaler}{1}.
\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}
	{nduNewUnit}
	{\marg{unit name}\marg{key/value pairs}}
	
Units can have up to 7 segments, numbered \meta{0-6}. The left-most segment, that is, the \emph{top} or \emph{root} segment, is numbered \texttt{0}.

The numeral part of the below key paths \docAuxKey*{segment 0/} can be any integer up to \texttt{6}, ie. \docAuxKey*{segment 6/}. The internal number of segments is determined by how many name keys are created.

See below for available settings.

\end{docCommand}

\begin{docCommand}
	{nduNewMacro}
	{\marg{unit name}\oarg{key/value pairs}\marg{control sequence}}

	It is possible to create shortcut macros for commonly used \meta{unit name}s 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).

\begin{dispExample}
\nduNewMacro{rigsdaler.mark.skilling}
	[unit groups/rigsdaler.mark.skilling/segment 0/symbol={R\textsuperscript{dl}}]
	{myRdl}
\myRdl{1.2.3}
\end{dispExample}

\end{docCommand}

%\begin{docCommand}
%	{nduCommonSymbols}
%	{\marg{key/value pairs}}
%
%	It is possible to configure commonly used symbols using the form \texttt{\meta{name}=\meta{symbol}}. These will be used as fallbacks if no specific symbol is configured for a segment via \refKey{segment:symbol}.
%
%\end{docCommand}
%
%\begin{docCommand}
%	{nduCommonFactors}
%	{\marg{key/value pairs}}
%
%	Not currently implemented.
%
%\end{docCommand}

\clearpage
\subsubsection{Options}

\begin{docKey}
	{segment separator}
	{=\meta{...}}
	{initially \texttt{\~{}}}
	
	When displaying a value, this string will be inserted between each segment.

\begin{dispExample}
\nduValue{danish hartkorn}[
		show=values,
		segment separator=.
	]
	{1.2.3.4}

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

\end{docKey}

\begin{docKey}
	{restrict segment depth}
	{=\meta{integer}}
	{initially no restriction}
	
	When calculating or displaying a value, only the segments up to and including \meta{integer} will be considered.
	
	In this document, the depth has been globally set to \texttt{2} for \docAuxKey*{danish rigsdaler}, but the older historical sub-unit \nduName{danish rigsdaler}{3} can be included by locally setting the depth to \texttt{3} (or indeed not restricting it globally).
	
\begin{dispExample}
\nduValue{danish rigsdaler}
	[restrict segment depth=3]
	{1.2.3.4}
\end{dispExample}
\end{docKey}

\begin{docKey}
	[]
	[doc label=segment:name]
	{segment \meta{n}/name}
	{=\meta{name}}
	{no default, initially undefined}

	Gives the proper name of the segment's unit. Used internally to determine how many segments the unit contains.
	
	Can be accessed with by \refCom{nduName}.
\end{docKey}

\begin{docKey}
	[]
	[doc label=segment:symbol]
	{segment \meta{n}/symbol}
	{=\meta{symbol}}
	{no default, initially undefined}

	Configures a symbol displaying the unit. This is used in \docAuxCommand{nduHeader} and is also available via \docAuxCommand{nduSym} when defining the \refKey{segment:display} (see below).
	
	If none is configured, an attempt to look up a common symbol by its name is made. These can be configured with \refCom{nduCommonSymbols}.
\end{docKey}

\begin{docKeys}
	[
		doc parameter = {=\meta{...}},
	]
	{
		{
			doc name = {segment \meta{n}/prefix},
			doc label = segment:prefix,
			doc description = {initially set to \texttt{\{\}}},
		},
		{
			doc name = {segment \meta{n}/suffix},
			doc label = segment:suffix,
			doc description = {initially set to \texttt{\{~\docAuxCommand*{nduSym}\}}},
		},
	}

	When displaying a value, segments will be wrapped between the \meta{prefix} and \meta{suffix}.
	
	The macro \docAuxCommand{nduSym} is available here to show the symbol configured for the segment.
\end{docKeys}

\begin{docKey}
	[]
	[doc label=segment:display]
	{segment \meta{n}/display}
	{=\marg{prefix}\marg{suffix}}
	{}

	Sets both \refKey{segment:prefix} and \refKey{segment:suffix} at the same time.
\end{docKey}

\begin{docKey}
	[]
	[doc label=segment:factor]
	{segment \meta{n}/factor}
	{=\meta{integer}}
	{no default, initially undefined}
	
	The conversion factor of a segment is how many of the underlying segment the given segment consists of.
	
	This is used in the math macros, in order to calculate the correct segment values.
	
	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}
	[segment 1/symbol=Mk.]
	{.9.}

\nduValue{danish rigsdaler}
	[segment 0/display={}{~Rigsdaler og}]
	{1.2.3}

\nduValue{danish rigsdaler}[
		segment separator={---},
		segment 0/display={(}{)},
		segment 1/display={[}{]},
		segment 2/display={\{}{\}},
	]
	{1.2.3}
\end{dispExample}

\begin{docKey}
	{create macro named}
	{=\meta{control sequence}}
	{no default, initially empty}
	
	Units may provide a default shortcut macro, for example the \docValue*{danish rigsdaler} unit configures \docAuxCommand*{rdl}.
	
	This is done via \refCom{nduNewMacro} which describes the arguments of the resulting macros.

\begin{dispExample*}{
	sidebyside,
	righthand width=1.75cm,
}
\rdl{2.3.}
\end{dispExample*}
\end{docKey}

\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}