Use the SIunits package. Note that SI units are to be typeset in roman type, not italics! And please don't mix information and units. “g C” (with a thin space) can mean only one thing, namely the product of the units gram and Coulomb. Mixing in information like C for carbon with the units is a cardinal sin. A major point of standardised units with a standardised notation is disambiguation – if you see an SI unit in a paper far from your own speciality you should still be able to understand what it means.
Please refer to the style guides at the NIST SI units home page for further details.
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
If you want minimal mark-up, you can do it using
siunitx
using eitheror
or you could use the older
unitsdef
package(Notice that the
siunitx
syntax for 'free-standing' units is in-part based onunitsdef
.) As noted in comments and in What package should I use to typeset units?, the 'preferred' syntax insiunitx
isas this provides more logical mark-up but also more programmatic control of outcomes. In any of the
siunitx
cases, the space is adjustable via a key and will not break unless the appropriate setting is altered.