Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This is from an old package I developed back in 2003. The idea was to cover international system units (SI) and other type of units, roughly following guidelines from that document.

Units should use some straight font (roman vs. italic or oblique), they are not abbreviations and should never use a period, and should be separated from the quantity by a space (if typographically possible by a small space) and should be separated also from following punctuation.

So I defined macro \unit that would typeset the following argument as \mathrm handling a tinyspace \, before to separate it from the quantity and would add an additional tinyspace after if followed by punctuation. It also took an optional argument for typesetting an exponent.

It also had an additional macro \newunit that would create a command for most common uints.

I also attempted for these commands to work both in normal text as in math-mode.

The code bellow shows the macros defined and an example:

\documentclass[twocolumn]{article}
\makeatletter
  %%% Modified from xspace
  \DeclareRobustCommand\uspace{\futurelet\@let@token\@uspace}
  \def\@uspace{%
    \ifx\@let@token\bgroup\else
    \ifx\@let@token\egroup\else
    \ifx\@let@token\/\else
    \ifx\@let@token\ \else
    \ifx\@let@token~\else
    \ifx\@let@token.\,\else
    \ifx\@let@token!\,\else
    \ifx\@let@token,\,\else
    \ifx\@let@token:\,\else
    \ifx\@let@token;\,\else
    \ifx\@let@token?\,\else
    \ifx\@let@token/\else
    \ifx\@let@token'\else
    \ifx\@let@token)\,\else
    \ifx\@let@token-\else
    \ifx\@let@token\@xobeysp\else
    \ifx\@let@token\space\else
    \ifx\@let@token\@sptoken\else
     \space
     \fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi\fi}
  \newcommand\unit[1]{\def\@tempa{#1}\unit@}
  \newcommand\unit@[1][\@empty]{%
    \ensuremath{\,%
      \textrm{\@tempa}%
      \ifx#1\@empty\else
       ^{#1}\fi}%
    \uspace}
  \newcommand\newunit[2]{%
    \@namedef{#1}{\unit{#2}}}
  \newcommand\degree{%
    \ensuremath{^\circ}}
\makeatother
% SI official and common derivated units
\newunit{m}{m}
\newunit{kg}{kg}
\newunit{sec}{s}
\newunit{amp}{A}
\newunit{ohm}{$\Omega$}
\newunit{kelvin}{\degree K}
\newunit{celcius}{\degree C}

\begin{document}
\newunit{cm}{cm}
One inch is 2.54\cm and one square inch is 6.4516\cm[2].
\[(2.54\cm)^2=6.4516\cm[2]\]

\newunit{farenheit}{\degree F}
Water freezes at 32\farenheit and boils at 212\farenheit.
\[100\celcius=212\farenheit-32\farenheit\]

International unit system defines the Ampere (\amp) and the
second (\sec), however it does ot define the Culomb (\unit{C})
prefering the compund Ampere-second (\amp\sec).
(Although it should look as \unit{As}.)
\[1\unit{C} = 1\amp\sec = 1\unit{As}.\]

If a tension of 12\unit{V} on a resistor of 15\ohm will produce
a current of 0.8\amp.
\[\frac{12\unit{V}}{15\ohm}=0.8\amp\]

The gravitational field on Eath's surface is 1\unit{g}, and this
is roughly equal to 9.8\unit{N}/\kg or 32\unit{ft}/\sec[2].
\[1\unit{g} \simeq 9.8\frac{\unit{N}}{\kg}
 = 9.8\m\sec[-2] \simeq 32\frac{\unit{ft}}{\sec[2]}\]

\end{document}

Example output

In most running text and and math mode it works fine, however the output sometimes leaves spaces too wide where no space should be.

What kind of trick may I use to eliminate the spaces where I don't need them while preserving the space I want?

I have noticed that command \mathrel handle the spaces similarly as I expect for \unit

(The space previous to the degree symbol might be handled apart, as it affects all the instances of those units.)

((I got the attention that package siunitx might already do what I attempted; while using that package might solve my typographic problems, it doesn't solve my “I want to know how to do it” problem.))

share|improve this question
    
It looks like the key question here is trying to reproduce functionality similar to the xspace package, i.e 'auto-adding' spaces after commands. That would mean that that the units part is really not that significant other than for context. Is that correct? –  Joseph Wright Dec 13 '13 at 6:59
    
Can you give a link to »that document«? Are you aware of the siunitx package? –  Johannes_B Dec 13 '13 at 8:53
    
@Johannes_B the document was printed and posted in a wall, however I think it might be this one here: gemini.udistrital.edu.co/comunidad/grupos/gispud/RAIZDC/… (pages enumerated 1 to 7) I don't find, however, the part of separating units from punctuation. –  Carlos Eugenio Thompson Pinzón Dec 13 '13 at 14:13
    
@JosephWright I indeed recognize that my \uspace command was a rip of \xspace and it behaves nicely when using single units inside text (or math mode) just after a number. It does not work as well when mentioning units in isolation or composing units. –  Carlos Eugenio Thompson Pinzón Dec 13 '13 at 14:16
    
@CarlosEugenioThompsonPinzón My point is that your code isn't just a rip of \xspace: try the latter in your demo and the problems go away. Side issue: unit names are not capitalised: it's 'ampere' and 'coulomb'. –  Joseph Wright Dec 13 '13 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.