Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Units in math mode in LaTeX

5,084 views
Skip to first unread message

gilbert

unread,
Dec 2, 2002, 5:27:11 PM12/2/02
to
I often have to write documents in LaTeX including calculations which
include units. In normal math mode, this forces my units to be
displayed in italics and spaces are collapsed. Of course, this is not
the desired effect. While I have been working around this problem so
far by escaping spaces using \; and placing units in a \mathrm{}
command. However, I suspect that this may be unnecessary. Is there an
environment similar to math mode or a package that I should download
that will do away with these workarounds? Thanks.

Josselin Noirel

unread,
Dec 2, 2002, 5:31:04 PM12/2/02
to
gilbert, wrote

units.sty

-- Josselin

David Kastrup

unread,
Dec 2, 2002, 5:31:50 PM12/2/02
to
theresabo...@yahoo.com (gilbert) writes:

units.sty, SIunits.sty

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
UKTUG FAQ: <URL:http://www.tex.ac.uk/cgi-bin/texfaq2html>

gilbert

unread,
Dec 3, 2002, 5:38:52 PM12/3/02
to
David Kastrup <David....@t-online.de> wrote in message news:<x5r8d08...@lola.goethe.zz>...

> theresabo...@yahoo.com (gilbert) writes:
>
> > I often have to write documents in LaTeX including calculations which
> > include units. In normal math mode, this forces my units to be
> > displayed in italics and spaces are collapsed. Of course, this is not
> > the desired effect. While I have been working around this problem so
> > far by escaping spaces using \; and placing units in a \mathrm{}
> > command. However, I suspect that this may be unnecessary. Is there an
> > environment similar to math mode or a package that I should download
> > that will do away with these workarounds? Thanks.
>
> units.sty, SIunits.sty

units.sty solved the problem of italicizing units, but it did not
solve the problem of spacing. To clarify, sometimes my units consist
of an SI unit and the name of a chemical compound and I need a space
between the name of the compound and the chemical compound (for
example, "mol NaOH"). Any suggestions?

Chrisada Sookdhis

unread,
Dec 4, 2002, 6:20:55 AM12/4/02
to
> units.sty solved the problem of italicizing units, but it did not
> solve the problem of spacing. To clarify, sometimes my units consist
> of an SI unit and the name of a chemical compound and I need a space
> between the name of the compound and the chemical compound (for
> example, "mol NaOH"). Any suggestions?

Well, that has not clarified things. What did you type, what did you
get, and what did you want to get?

I think to \unit[1.0]{mol NaOH} give space between mol and NaOH, but
bigger space than between 1.0 and mol.

Using SIunits, you can control the space between value and unit, and
in between compound units. If you want equal spacing, this should work

\usepackage[thinqspace,thinspace]{SIunits}

\unit{1.0}{\mole \usk NaOH}

G

unread,
Dec 4, 2002, 10:03:38 PM12/4/02
to
theresabo...@yahoo.com (gilbert) wrote in message news:<7eba5997.02120...@posting.google.com>...

The problem of units in latex has been raised many times. Even though
existing packages are good, I still think the most simple and elegant
solution is to define a command like this one

%% APS recommends full intra-word space between number and unit
\newcommand{\un}[1]{\ensuremath{\ \mathrm{#1}}}

Usage:

for units with roman alphabet letters, in paragraph mode, no need for
extra command:
...velocity is 30~m/s...

units with greek letters or other symbols, in paragraph mode
...resistance is 1\un{M\Omega}...

in math mode
...\( 1\un{V} = (1\un{\Omega}) \cdot (1\un{A}) \)
subscripts and superscripts have the right size
...our funky law is \( V = I^{1\un{M\Omega}} \)...

if your unit is awkward to write every time, just define a shorthand
command

\newcommand{\molNaOH}{\ensuremath{\mathrm{mol\;NaOH}}}

or similar.

Hope this is helpful.

-Giulio

Michael Cree

unread,
Dec 5, 2002, 4:08:40 PM12/5/02
to
stan...@quipo.it (G) writes:

> theresabo...@yahoo.com (gilbert) wrote in message news:<7eba5997.02120...@posting.google.com>...
> > I often have to write documents in LaTeX including calculations which
> > include units.
>

> The problem of units in latex has been raised many times. Even though
> existing packages are good, I still think the most simple and elegant
> solution is to define a command like this one
>
> %% APS recommends full intra-word space between number and unit
> \newcommand{\un}[1]{\ensuremath{\ \mathrm{#1}}}

Yes, I also like a command like that. Below is my favourite code for
producing units. It is based upon a posting of one of the TeX gurus a
number of years back (sorry, I've forgotten who first posted it), but I've
modified it and added my own little niceties. Usage is:

The speed of light is 2.9979\E{8}\un{m s^{-1}}.

or in its starred form:

The units of flux are~\un*{J m^{-2} s^{-1}}.

Notice the little extras of the \E{} for scientific notation and the
renaming of \deg (better duck - the LaTeX police are on the way) to be a
degree symbol (the LaTeX defn of \deg is put into \olddeg). The \un{}
command even knows the \deg command and does not put in a small space after
the number if the units are degrees.

Code follows (put into .sty file or surround with \makeatletter,
\makeatother.):

\DeclareMathSymbol{\circx}{\mathord}{symbols}{"0E}
\let\olddeg=\deg
\renewcommand\deg{\ensuremath{^\circx}}
\DeclareMathSymbol{\timesx}{\mathord}{symbols}{"02}
\newcommand{\etimes}{\mskip1.1mu\timesx\mskip0.9mu\relax}
\newcommand{\E}[1]{\ensuremath{\etimes10^{#1}}}
\newmuskip\unitmuskip
\newmuskip\unitpreskip
\unitmuskip=1mu plus 0.3mu % Change this to suit your preference
\unitpreskip=4mu plus 1mu minus 1mu % Change this to suit your preference
\def\unitmskip{\penalty10000 \mskip\unitmuskip}%
\DeclareRobustCommand\un{\@ifstar{\@unstar}{\@un}}
\newcommand\@unstar[1]{\leavevmode
\ensuremath{%
\begingroup \fam\z@
\let\,\unitmskip \unit@PreserveSpaces\@empty #1 \unit@PreserveSpaces
\endgroup}}%
\newcommand\@un[1]{\leavevmode
\unskip\penalty1000
\ensuremath{%
\@checkfordeg #1\@empty
\begingroup \fam\z@
\let\,\unitmskip \unit@PreserveSpaces\@empty #1 \unit@PreserveSpaces
\endgroup}}%
\def\unit@PreserveSpaces#1 {#1\@ifnextchar\unit@PreserveSpaces{\@gobble}%
{\unitmskip \unit@PreserveSpaces\@empty}}%
\def\@checkfordeg{\@ifnextchar\deg{\@junkrest}{\mskip\unitpreskip\@junkrest}}%
\def\@junkrest#1\@empty{}%


Michael.

0 new messages