Implementing your hack is quite easy:
\newif\ifMetric\Metrictrue% metric by default
\newcommand\MyUnit[2]{\ifMetric #1\else #2\fi}% \MyUnits{metric}{imperial}
Then you can simply use \MyUnit{191cm}{6'3''}
in your document and change between metric and imperial at any point using \Metrictrue
and \Metricfalse
.
Here's a full example (with a crude use of SIunits as requested in the comments):
\documentclass{article}
\usepackage{SIunits}
\newif\ifMetric\Metrictrue% metric by default
\newcommand\MyUnit[2]{\ifMetric #1\else #2\fi}% \MyUnits{metric}{imperial}
\begin{document}
Metric: \MyUnit{191 \centi\meter}{$6'3''$}
\Metricfalse Imperial: \MyUnit{191 \centi\meter}{$6'3''$}
\Metrictrue Metric: \MyUnit{191 \centi\meter}{$6'3''$}
\end{document}
and the output:
Edit: the smartunits package
When I first wrote this post I said that it ought to be possible to do this properly using pgfkeys. Partly as a proof-of-concept, and partly as an exercise to learn how to use pgfkeys
, there is now a smartunits package for converting between metric and Imperial units.
Here is a MWE:
\documentclass{article}
\usepackage{smartunits}
\usepackage{xcolor}
\usepackage{listings}\lstset{language=[LaTeX]TeX}
\lstset{language=[LaTeX]TeX,
texcsstyle=*\bfseries\color{blue},
keywordstyle=\color{blue},
commentstyle=\color{brown},
morekeywords={SmartUnit,SmartUnitSettings,sisetup},
}
\begin{document}
\begin{lstlisting}[texcl]
\SmartUnitSettings{metric imperial, places=1}
\SmartUnit{km=100.0,figures=1} % \SmartUnit{km=100.0,figures=1}
\SmartUnit{miles=62.15,places=1} % \SmartUnit{miles=62.15,places=1}
\SmartUnit{cm=10} % \SmartUnit{cm=10}
\SmartUnit{celsius=20} % \SmartUnit{celsius=20}
\SmartUnit{miles=5.0, figures=1} % \SmartUnit{miles=5.0, figures=1}
\SmartUnit{miles=5.0,places=2} % \SmartUnit{miles=5.0, places=2}
\SmartUnit{hours=0, minutes=59} % \SmartUnit{hours=0, minutes=59}
\SmartUnit{hours=12, minutes=12} % \SmartUnit{hours=12, minutes=12}
\SmartUnit{kg=10.0, places=1} % \SmartUnit{kg=10.0, places=1}
\SmartUnit{pound=10.0,figures=1} % \SmartUnit{pound=10.0,figures=1}
\SmartUnit{l=10.0, places=1} % \SmartUnit{l=10.0, places=1}
\SmartUnit{L=10.0, places=1,uk} % \SmartUnit{L=10.0, places=1,uk}
\end{lstlisting}
\end{document}
and here is the output this produces:
(There is some trickery using the listings package to have LateX typeset the commands after the %
's on each line.)
Best Answer
From the plain TeX reference:
pt
: Pointpc
: pica (12pt
)in
: inch (72.27pt
)bp
: Big point (72bp
= 1in
)cm
: Centimetermm
: Millimeterdd
: Didot point (1157dd
= 1238pt
)cc
: cicero (12dd
)sp
: Scaled point (65536sp
= 1pt
), the smallest TeX unitex
: Nominal x-heightem
: Nominal m-widthAvailable in math mode:
mu
: math unit, 1em
= 18mu
, whereem
is taken from the math symbols family, various lengths are derived from it (thinspace
,thickspace
, etc.)Additionally available in pdfTeX and LuaTeX:
px
: "pixel", the dimension given to the\pdfpxdimen
primitive; default value is 1bp
, corresponding to a pixel density of 72 dpiSee also here on TeX.SX:
The meanings of the various points are described here: