Debian Codegraph @

texmf-dist/doc/latex/smartunits/smartunits.tex

raw
1 \documentclass[svgnames]{report}
2 \usepackage{smartunits}
3 \usepackage{manfnt}
4 \usepackage{enumitem}
5 \setlist[description]{font=\sffamily\bfseries\color{DodgerBlue},labelwidth=\textwidth}
6 \usepackage{booktabs}
7 \usepackage[a4paper,margin=18mm]{geometry}
8 \synctex=1
9
10 \usepackage{fixltx2e} % LaTeX patches, \textsubscript
11 \usepackage{cmap} % fix search and cut-and-paste in Acrobat
12 \usepackage[T1]{fontenc}
13 \usepackage[utf8]{inputenc}
14 \setcounter{secnumdepth}{0}
15
16 \usepackage{listings}\lstset{language=[LaTeX]TeX,framerule=2pt}
17 \lstset{language=[LaTeX]TeX,
18 texcsstyle=*\bfseries\color{Blue},
19 backgroundcolor=\color{Ivory},
20 numbers=none,
21 breaklines=true,
22 keywordstyle=\color{Blue},
23 commentstyle=\color{Brown},
24 tabsize=2,
25 morekeywords={SmartUnit,SmartUnitSettings,sisetup},
26 resetmargins=true,
27 }
28 % hyperref links to ctan
29 \newcommand\ctan[1]{\href{https://www.ctan.org/pkg/#1}{\texttt{#1}}}
30
31 \usepackage{xparse}
32 \NewDocumentCommand{\Verb}{v}{{\color{blue}#1}}
33
34 \newcommand\Section[1]{\subsection{\textcolor{blue}{#1}}}
35
36 \makeatletter
37 \author{Andrew Mathas}
38 \usepackage{tikz}
39 \usetikzlibrary{shadows.blur}
40 \tikzset{shadowed/.style={blur shadow={shadow blur steps=5},
41 bottom color=LightSkyBlue!30,
42 draw=MidnightBlue!70,shade,
43 font=\normalfont\Huge\bfseries\scshape,
44 rounded corners=8pt,
45 top color=SkyBlue,
46 },
47 boxes/.style={draw=MidnightBlue,
48 fill=Cornsilk,
49 font=\sffamily\small,
50 inner sep=5pt,
51 rectangle,
52 rounded corners=8pt,
53 text=RoyalBlue,
54 }
55 }
56 \def\SmartTitle{
57 \begin{tikzpicture}[remember picture,overlay]
58 \node[yshift=-3cm] at (current page.north west)
59 {\begin{tikzpicture}[remember picture, overlay]
60 \draw[shadowed](30mm,0) rectangle node[white]{Smart Units} (\paperwidth-30mm,16mm);
61 \node[anchor=west,boxes] at (4cm,0cm) {\@author};
62 \node[anchor=east,boxes] at (\paperwidth-4cm,0) {\smart@version};
63 \end{tikzpicture}
64 };
65 \end{tikzpicture}
66 \vspace*{20mm}
67 }
68
69
70 \def\@oddfoot{\textsc{Smart Units} -- \smart@version\hfill\thepage}
71
72 \usepackage[colorlinks=true,linkcolor=blue,urlcolor=blue]{hyperref}
73 \hypersetup{pdfcreator={ Generated by pdfLaTeX },
74 pdfinfo={Author ={ Andrew Mathas },
75 Keywords={ units, metric, Imperial },
76 License ={ LaTeX Project Public License v1.3c or later },
77 Subject ={ Convert between metric and Imperial units },
78 Title ={ smartunits - \smart@version }
79 },
80 }
81 \makeatother
82
83 \begin{document}
84
85 \SmartTitle
86
87 %\Section{Smart units}
88 This package implements a \Verb|\SmartUnit| macro for
89 converting between the following metric and Imperial units:
90
91 \begin{center}
92 \begin{tabular}{@{\color{Grey!50}}ll@{\quad$\longleftrightarrow$\quad}l}\toprule
93 & Metric & Imperial \\\midrule
94 \color{LightSkyBlue}distance & kilometers & miles \\
95 \color{LightSkyBlue}length & centimeters & feet and inches \\
96 \color{LightSkyBlue}temperature & Celsius & Fahrenheit \\
97 \color{LightSkyBlue}time & 24-hour time& 12-hour time \\
98 \color{LightSkyBlue}volume & litres & USA/UK gallons \\
99 \color{LightSkyBlue}weight & kilograms & pounds \\
100 \bottomrule
101 \end{tabular}
102 \end{center}
103 Of course, the underlying unit for length and distance is the same
104 but it makes sense to convert between units of similar ``scale''.
105 The \Verb|\SmartUnit| macro is designed to print only one unit at a
106 time, where the quantity is given in terms of either metric or
107 Imperial units (there is no error checking, however). Depending on
108 the (global) settings the unit will be printed either as a metric
109 unit, or as an Imperial unit, or both. If the required unit is not
110 given as an argument to \Verb|\SmartUnit| then it is computed.
111
112 The units are printed using \ctan{siunitx}, so the formatting of the
113 units can be controlled using \Verb|\sisetup| together with
114 formatting parameters from \ctan{siunitx}. Some aspects of the
115 formatting can be controlled ``locally'' using \Verb|\SmartUnit| and
116 ``globally'' using \Verb|\SmartUnitSettings|.
117
118 The \Verb|\SmartUnit| macro can print units in the following
119 four different formats:
120
121 \begin{lstlisting}
122 \SmartUnit{metric} % --> metric
123 \SmartUnit{imperial} % --> Imperial
124 \SmartUnit{metric imperial} % --> metric (Imperial)
125 \SmartUnit{imperial metric} % --> Imperial (metric)
126 \end{lstlisting}
127
128 For the impatient, here are some examples:
129
130 \bgroup
131 \sisetup{round-mode=places,round-precision=2}
132 \small
133 \begin{lstlisting}[escapeinside=<>]
134 \documentclass{article}
135 \usepackage{smartunits}
136 \begin{document}
137 \SmartUnitSettings{places=2} % Some global settings < \SmartUnitSettings{places=2} >
138 \SmartUnit{metric imperial} % Output format: metric (Imperial) < \SmartUnit{metric imperial} >
139 \SmartUnit{cm=10} % < \SmartUnit{cm=10} >
140 \SmartUnit{cm=66} % < \SmartUnit{cm=66} >
141 \SmartUnit{feet=4,inches=3} % < \SmartUnit{feet=4,inches=3} >
142 \SmartUnit{celsius=20} % < \SmartUnit{celsius=20} >
143 \SmartUnit{metric} % Output format: metric < \SmartUnit{metric} >
144 \SmartUnit{miles=5, places=1} % < \SmartUnit{miles=5,places=1} >
145 \SmartUnit{imperial} % Output format: Imperial < \SmartUnit{imperial} >
146 \SmartUnit{hours=12, minutes=1} % < \SmartUnit{hours=12, minutes=1} >
147 \SmartUnit{km=5,places=3} % < \SmartUnit{km=5,places=3} >
148 \SmartUnit{imperial metric} % Output format: Imperial (metric) < \SmartUnit{imperial metric} >
149 \SmartUnit{miles=5, km=7} % < \SmartUnit{miles=5,km=7} >
150 \SmartUnit{l=1.0, places=1} % < \SmartUnit{l=1.0, places=1} >
151 \SmartUnit{L=1.0, places=1} % < \SmartUnit{L=1.0, places=1} >
152 \SmartUnit{gal=25.0,uk,places=2} % < \SmartUnit{gal=25.0, places=2,uk} >
153 \SmartUnit{gal=25.0,usa,places=2} % < \SmartUnit{gal=25.0, places=2,usa} >
154 \end{document}
155 \end{lstlisting}
156 \egroup
157
158 The calculations done by \Verb|\SmartUnit| are reasonably accurate
159 but LaTeX is not a calculator so rounding and other errors can and
160 do happen (indeed, see the displayed example below). In addition, even assuming
161 that the calculations done by \Verb|\SmartUnit| are accurate this
162 may not be what you want. For example, if the metric unit is given
163 as \SI{100}{km} then many people will want the Imperial unit to be
164 written as \SI{60}{\text{mi}}, rather than the more accurate
165 \SI{62.137119}{\text{mi}}. In fact, \Verb|\SmartUnit| can cater for
166 these considerations because all units are printed using the
167 \ctan{siunitx} package, which is really clever. The precision of the
168 printed units can be controlled using \Verb|\sisetup|, from the
169 \ctan{siunitx} package or by using the short-hands, \textsf{figures}
170 and \textsf{places}, provided by \Verb|\SmartUnit|:
171 \begin{lstlisting}[texcl]
172 \SmartUnit{km=100.0, figures=1} % \SmartUnit{km=100.0,figures=1}
173 \SmartUnit{km=100.0, places=5} % \SmartUnit{km=100.0,places=5}
174 \end{lstlisting}
175 \noindent (Comparing with the paragraph above, the last calculation
176 is only accurate to three decimal places!) The values of the
177 computed units can be overridden, during proof-reading for example,
178 by specifying both the metric and Imperial units:
179 \begin{lstlisting}[texcl]
180 \SmartUnit{km=100,miles=70} % \SmartUnit{km=100,miles=70}
181 \end{lstlisting}
182 \noindent \raisebox{3.9mm}{\dbend}\quad Of course, if you specify
183 units incorrectly like this then incorrect values will be printed.
184
185 Behind the scenes all units are printed using \ctan{siunitx}. Therefore,
186 the precision and formatting of the units can be changed using the
187 options to \Verb|\sisetup| described in the \ctan{siunitx}\ manual. In
188 particular, when the rounding capabilities of~\ctan{siunitx}\ are enabled
189 any units calculated by \Verb|\SmartUnit| will be rounded.
190 In this case there is a shorthand for overriding the global
191 \ctan{siunitx}\ settings for the rounding precision on an individual
192 ``smart unit'':
193
194 \begin{lstlisting}[escapeinside=<>]
195 % Locally=\sisetup{round-precision=3, round-mode=places}
196 \SmartUnit{cm=2, places=3} % < \SmartUnit{cm=2, places=3} >
197 \end{lstlisting}
198
199 Section~5.5 of the \ctan{siunitx}\ manual should be consulted for all of the
200 options for controlling ``post-processing'' of numbers. In particular,
201 units that are given as integers will always be printed as integers.
202 For example, the metric version of \Verb|\SmartUnit{cm=2, places=3}|
203 is printed as \SmartUnit{cm=2, places=3, metric} whereas the Imperial version is
204 \num{0.787}$'$. If, instead, we use
205 \Verb|\SmartUnit{cm=2.0, places=3}| then the metric unit is
206 printed as \SmartUnit{cm=2.0, places=3, metric}.
207
208
209 \Section{Unit conversions}
210 For all of these examples we use the following global settings:
211 \begin{lstlisting}[texcl]
212 \SmartUnitSettings{places=2, metric imperial} % Global settings
213 \end{lstlisting}
214 \SmartUnitSettings{places=2, metric imperial}
215 \noindent Of course, as in some of the example below, this can always be overridden locally.
216
217 \begin{description}
218 \item[Distance -- km, miles, mi]
219 Conversions between kilometers and miles. Miles can be specified
220 using either \textbf{miles} or \textbf{mi}.
221 \begin{lstlisting}[texcl]
222 \SmartUnit{km=100,miles=60} % \SmartUnit{km=100,miles=60}
223 \SmartUnit{km=100.0,figures=1} % \SmartUnit{km=100,figures=1}
224 \SmartUnit{miles=62.15,places=1} % \SmartUnit{miles=62.15,places=1}
225 \end{lstlisting}
226
227 \item[Length -- cm, inches, feet]
228 Feet are always printed as integers. Significant figures/places
229 are applied only to the printing of inches.
230 \begin{lstlisting}[texcl]
231 \SmartUnit{feet=7, inches=1} % \SmartUnit{feet=7, inches=1}
232 \SmartUnit{cm=189, feet=7, inches=1} % \SmartUnit{cm=189, feet=7, inches=1}
233 \SmartUnit{cm=191, places=1} % \SmartUnit{cm=191, places=1}
234 \end{lstlisting}
235
236 \item[Temperature -- celsius, fahrenheit]
237 Convesions between Celsius and Fehrenheit can be speficied using
238 \textsf{celsius}, \textsf{C}, \textsf{fahrenheit} or \textsf{F}.
239
240 \begin{lstlisting}[texcl]
241 \SmartUnit{celsius=32,places=3} % \SmartUnit{celsius=32,places=3}
242 \SmartUnit{celsius=0} % \SmartUnit{celsius=0}
243 \SmartUnit{C=37} % \SmartUnit{C=37}
244 \SmartUnit{fahrenheit=0} % \SmartUnit{fahrenheit=0,places=2}
245 \SmartUnit{F=22,figures=4} % \SmartUnit{F=212,figures=4}
246 \end{lstlisting}
247
248 \item[Time -- hours, minutes, seconds, am, pm]
249 \begin{lstlisting}[texcl]
250 \SmartUnit{hours=0, minutes=59} % \SmartUnit{hours=0, minutes=59}
251 \SmartUnit{hours=12, minutes=12} % \SmartUnit{hours=12, minutes=12}
252 \SmartUnit{hours=13, minutes=9} % \SmartUnit{hours=13, minutes=9}
253 \SmartUnit{hours=8, minutes=31, pm} % \SmartUnit{hours=8, minutes=31, pm}
254 \SmartUnit{hours=9, minutes=3, am} % \SmartUnit{hours=9, minutes=3, am}
255 \SmartUnit{hours=0, minutes=0,seconds=1} % \SmartUnit{hours=0, minutes=0,seconds=1}
256 \SmartUnit{hours=12, minutes=0,seconds=1} % \SmartUnit{hours=12, minutes=0,seconds=1}
257 \end{lstlisting}
258
259 \item[Volume -- L, l, gal, gallons]
260 Converting between litres and gallons. Liters can be specified
261 using \textbf{L} or \textbf{l} and gallons can be given with
262 \textbf{gallons} or \textbf{gal}. Both UK gallons and USA
263 gallons are supported, with USA gallons being the default.
264 \begin{lstlisting}[texcl]
265 \SmartUnit{l=10.0, places=1} % \SmartUnit{l=10.0, places=1}
266 \SmartUnit{L=10.0, places=1,uk} % \SmartUnit{L=10.0, places=1,uk}
267 \SmartUnit{gal=10.0, places=2} % \SmartUnit{gal=10.0, places=2}
268 \end{lstlisting}
269
270 \item[Weight -- kg, pounds, lbs]
271 \begin{lstlisting}[texcl]
272 \SmartUnit{kg=10.0, places=1} % \SmartUnit{kg=10.0, places=1}
273 \SmartUnit{pound=10.0, places=1} % \SmartUnit{pound=10.0, places=1}
274 \SmartUnit{pound=10.0,figures=1} % \SmartUnit{pound=10.0,figures=1}
275 \end{lstlisting}
276 \end{description}
277
278 \Section{Formatting options}
279
280 The following options can either be used inside \Verb|\SmartUnit| to
281 control the formatting \textit{locally} for just the unit being
282 printed. They can also be used as arguments to
283 \Verb|\SmartUnitSettings| to change the formatting of all subsequent
284 smart units (unless these settings are overridden locally). More
285 control over the formatting of the units is given by the
286 \Verb|\sisetup| macro from the \ctan{siunitx} package.
287
288 \begin{description}
289 \item[figures] This is equivalent to \Verb|\sisetup{round-mode=figures, precision=#1}|.
290 Units are printed with \#1 significant figures.
291 \begin{lstlisting}[texcl]
292 \SmartUnit{miles=60, metric, figures=0} % \SmartUnit{miles=60, metric, figures=0}
293 \SmartUnit{miles=60, metric, figures=1} % \SmartUnit{miles=60, metric, figures=1}
294 \SmartUnit{miles=60, metric, figures=2} % \SmartUnit{miles=60, metric, figures=2}
295 \SmartUnit{miles=60, metric, figures=3} % \SmartUnit{miles=60, metric, figures=3}
296 \SmartUnit{miles=60, metric, figures=4} % \SmartUnit{miles=60, metric, figures=4}
297 \end{lstlisting}
298
299 \item[places]
300 This is equivalent to \Verb|\sisetup{round-mode=places, precision=#1}|.
301 Units are printed with \#1 decimal places.
302 \begin{lstlisting}[texcl]
303 \SmartUnit{miles=60, metric, places=0} % \SmartUnit{miles=60, metric, places=0}
304 \SmartUnit{miles=60, metric, places=1} % \SmartUnit{miles=60, metric, places=1}
305 \SmartUnit{miles=60, metric, places=2} % \SmartUnit{miles=60, metric, places=2}
306 \SmartUnit{miles=60, metric, places=3} % \SmartUnit{miles=60, metric, places=3}
307 \SmartUnit{miles=60, metric, places=4} % \SmartUnit{miles=60, metric, places=4}
308 \end{lstlisting}
309
310
311 \item[imperial]
312 Print only Imperial units.
313 \begin{lstlisting}[texcl]
314 \SmartUnit{km=1.0, imperial} % \SmartUnit{km=1.0, imperial, figures=2}
315 \SmartUnit{miles=1.0, imperial} % \SmartUnit{miles=1.0, imperial, figures=2}
316 \end{lstlisting}
317 \item[imperial metric]
318 Print Imperial units with metric units enclosed in brackets.
319 \begin{lstlisting}[texcl]
320 \SmartUnit{km=1.0, imperial metric} % \SmartUnit{km=1.0, imperial metric, figures=2}
321 \SmartUnit{miles=1.0, imperial metric} % \SmartUnit{miles=1.0, imperial metric, figures=2}
322 \end{lstlisting}
323 \item[metric (default)]
324 Print only metric units.
325 \begin{lstlisting}[texcl]
326 \SmartUnit{km=1.0, metric} % \SmartUnit{km=1.0, metric, figures=2}
327 \SmartUnit{miles=1.0, metric} % \SmartUnit{miles=1.0, metric, figures=2}
328 \end{lstlisting}
329 \item[metric imperial]
330 Print metric units with Imperial units enclosed in brackets.
331 \begin{lstlisting}[texcl]
332 \SmartUnit{km=1.0, metric imperial} % \SmartUnit{km=1.0, metric imperial, figures=2}
333 \SmartUnit{miles=1.0, metric imperial} % \SmartUnit{miles=1.0, metric imperial, figures=2}
334 \end{lstlisting}
335 \item[uk]
336 Selects the UK variants of the Imperial units (currently this
337 only affects gallons):
338 \begin{lstlisting}[texcl]
339 \SmartUnit{l=1.0, imperial metric, figures=2,uk} % \SmartUnit{l=1.0, imperial metric, figures=2,uk}
340 \SmartUnit{gal=1.0, imperial metric, figures=2,uk} % \SmartUnit{gal=1.0, imperial metric, figures=2,uk}
341 \end{lstlisting}
342 \item[usa (default)]
343 Selects the USA variants of the Imperial units (currently this
344 only affects gallons):
345 \begin{lstlisting}[texcl]
346 \SmartUnit{l=1.0, imperial metric, figures=2,usa} % \SmartUnit{l=1.0, imperial metric, figures=2,usa}
347 \SmartUnit{gal=1.0, imperial metric, figures=2,usa} % \SmartUnit{gal=1.0, imperial metric, figures=2,usa}
348 \end{lstlisting}
349 By default \Verb|\SmartUnit| uses the American Imperial units.
350 This is a purely democratic choice dictated by the populations
351 of the two countries.
352 \end{description}
353
354 \Section{The code}
355 Behind the scenes, the \Verb|\SmartUnit| macro uses \ctan{pgfkeys} and
356 \ctan{pgfmath} to convert between the different units. The interface is
357 surprisingly simple and easy to extend. For example, the following
358 code takes care of the conversion between kilometers and miles:
359 \bgroup\footnotesize
360 \begin{lstlisting}
361 %%%% convert=distance: km <-> miles %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
362 km/.style={convert=distance, unit km=#1},%
363 mi/.style={convert=distance, unit miles=#1},%
364 miles/.style={convert=distance, unit miles=#1},%
365 distance/metric/.style={/SmartUnit/conversion={km}{miles}{0}{1.609344}{0}{km}},%
366 distance/imperial/.style={/SmartUnit/conversion={miles}{km}{0}{0.62137119}{0}{\text{mi}}},%
367 \end{lstlisting}
368 \egroup
369 \noindent
370 Most of the conversions are done in this way --- the exceptions are
371 ``length'' and ``time'', which are more complicated. The key
372 \Verb|/SmartUnit/conversion| is a macro that takes six arguments and
373 then implements the generic conversion formula:
374 \[
375 \text{New unit}(\Verb|#1|) = \Big(\text{Old unit}(\Verb|#2)|+\text{Pre-offset}(\Verb|#3|)\Big)*\text{Multiplier}(\Verb|#4|) + \text{Post-offset}(\Verb|#5|),
376 \]
377 where \Verb|#6| is the metric or Imperial symbol for the unit being
378 printed.
379
380 \textit{Adding additional conversions is quite straightforward and I am
381 happy to incorporate suggestions.}
382
383 There is, of course, some trickery driving how the units are printed.
384 Here is the definition of \Verb|\SmartUnit|:
385 \begin{lstlisting}
386 \newcommand\SmartUnit[1]{%
387 \bgroup%=> changes are local to \SmartUnit => no need to reset
388 \pgfkeys{/SmartUnit,#1}% Pass the keys to /SmartUnit
389 \pgfkeys{/SmartUnit,printunit}% Calculate units and print
390 \egroup%
391 }
392 \end{lstlisting}
393 So \Verb|\SmartUnit| calls \ctan{pgfkeys} twice: the first time to
394 set the keys and the second time to print them using
395 \Verb|/SmartUnit/printunit|. In turn, this uses
396 \Verb|/SmartUnit/convert| to identify the unit being printed,
397 after which the pgf key
398 \Verb|/SmartUnit/output| is called. This has subkeys for the four
399 different \textit{output formats}:
400 \begin{center}
401 \texttt{metric}, \texttt{imperial},
402 \texttt{metric imperial} and \texttt{imperial metric}.
403 \end{center}
404 Finally, \Verb|/SmartUnit/output/<format>| uses
405 \Verb|/SmartUnit/convert| to perform the required conversion.
406 This multi-step printing process is necessary for converting between
407 inputs like time or centimeters, feet and inches, where the
408 output units depend on more than one input value. It is also used to
409 print the four different output formats discussed above.
410
411
412 \Section{Acknowledgement}
413 The package was written in response to a
414 \href{http://tex.stackexchange.com/questions/241169/}{TeX.SX question},
415 partly as a proof-of-concept and partly as an exercise to learn how
416 to use \ctan{pgfkeys}. Thanks to Mark Adams for asking the question.
417
418 \Section{Author}
419 \href{mailto:[email protected]?smartunits}{Andrew Mathas},
420 \copyright\space 2015, 2016.
421
422 \Section{Licence}
423 Released under the
424 \href{http://www.latex-project.org/lppl.txt}%
425 {LaTeX Project Public License}, v1.3c or later.
426
427 \end{document}