22

I'm writing a document that uses a mix of SI and atomic units. The latter are usually written in the slightly awkward notation

p = 1 a.u.

with dots and all. (I know, it's not great, but what can I do?) Since I'm using SI units, I would like to use a cohesive approach to units throughout, via the siunitx package, which has the advantage of proper and easy styling of the atomic units. These can be incorporated into the SI package via a custom \DeclareSIUnit command, which works perfectly well:

\documentclass[aps,onecolumn,preprint,pra]{revtex4-1}

\usepackage{siunitx}
\DeclareSIUnit{\au}{{a.u.}}

\begin{document}

The momentum is $p=\SI{1}{\au}$, so the energy is $E=\SI{0.5}{\au}$. 
The position is therefore unknown.

\end{document}

The problem is that at the end of a sentence you get double periods, one from the a.u. and one from the explicit period. This can be remedied by manually removing the latter, but it makes me liable to forget one if I move things around.

Is there an automatic way to make the siunitx unit \au to "eat" any period if it occurs immediately afterwards?

  • Si units are represented by symbols not abbreviations. As such they should not have decimal points in them: see bipm.org/en/publications/si-brochure. There is no au unit in any case: you probably want for example 'a.u. of energy' which looks like $E_{\mathrm{h}}$. (This doesn't of course answer the technical question, hence being a comment.) – Joseph Wright Feb 25 '15 at 16:22
  • 1
    @JosephWright: Unfortunately, according to en.wikipedia.org/wiki/Atomic_units#Use_and_notation, using just a.u. in that case instead of E_h seems to be the common thing to do, ambiguity and incoherence with "proper" units notwithstanding. – Jake Feb 25 '15 at 16:25
  • 1
    @Jake Like I said, not an answer but a comment :-) At a technical level there is no look-ahead in siunitx and indeed it would be pretty tricky to work out a logic (you first need to know that the last unit ends with a period, then skip over any $, \), etc. then look at the next char: not at all easy). – Joseph Wright Feb 25 '15 at 16:30
  • 2
    @Jake Also, and I'm sure you know this, Wikipedia isn't always right :-) The BIPM documentation, however, is definitive on the rules. – Joseph Wright Feb 25 '15 at 16:33
  • @Joseph I know, which is why I hedged and apologized :). It's not great with consistency, but that's what people use, and anything else in my particular case would just be awkard. (For example, the momentum would be $p=\sqrt{2E_\mathrm{H}}$ in your suggested notation.) Journals do use the dotted a.u.; see e.g. this guidance from Phys. Rev. C. – E.P. Feb 25 '15 at 16:38
13
+50

There's little hope of having a syntax like \SI{10}{\au}, because this would involve deep surgery in siunitx.

A syntax such as \au{10} seems to be the best choice.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand{\au}{m}
 {
  \SI{#1}{{a.u.}}
  \peek_charcode_remove:NT .
   {
    \mode_if_math:F { \spacefactor\sfcode`\.\scan_stop: }
   }
 }
\ExplSyntaxOff

\begin{document}

The star is at \au{10} from Earth.
Stars can be farther than \au{1000}.
The preceding period is considered
end of sentence.


% retry to see that space factor is honored
\sfcode`\.=20000 \xspaceskip=30pt

The star is at \au{10} from Earth.
Stars can be much much farther than \au{1000}.
The preceding period is considered
end of sentence.

\end{document}

We typeset \SI{10}{{a.u.}} and then peek at the next token; if it is a period, it is removed, but the space factor is reset as if the period would be there.

In the example, the paragraph is repeated to make evident what periods affect the space factor, with a very exaggerated setting.

enter image description here

A variant that also checks whether \au{...} comes at the end of a math formula, but that requires you use $. The test could be also improved with a check for \), which is left as an exercise.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn
\NewDocumentCommand{\au}{m}
 {
  \SI{#1}{{a.u.}}
  \peek_charcode_remove:NTF .
   {
    \mode_if_math:F { \spacefactor\sfcode`\.\scan_stop: }
   }
   {
    \mode_if_math:T
     {
      \peek_catcode_ignore_spaces:NT $
       {
        \group_insert_after:N \jost_check_period:
       }
     }
   }
 }
\cs_new_protected:Npn \jost_check_period:
 {
  \peek_charcode_remove:NT .
   {
    \spacefactor\sfcode`\.\scan_stop:
   }
 }
\ExplSyntaxOff

\begin{document}

The star is at \au{10} from Earth.
Stars can be farther than \au{1000}.
The preceding period is considered
end of sentence.

This has the thing is math: $E=\au{10}$.

% retry to see that space factor is honored
\sfcode`\.=20000 \xspaceskip=30pt

The star is at \au{10} from Earth.
Stars can be much much farther than \au{1000}.
The preceding period is considered
end of sentence.

\end{document}

enter image description here

However, my opinion is that you should either avoid the symbol with the periods or leave two periods, which mean different things.

  • I like that xspace is avoided here. However, I see the same caveat that @LaRiFaRi's solution has, namely it fails to properly typeset something like The energy is $E=\au{0.5}$.. I know that peeking outside the equation is one of the major difficulties mentioned by @JosefWright. I would still like to see an attempt to solve this. – Jost Jun 15 '15 at 14:46
  • @Jost Here you are. But, as I added, you should leave the end of sentence period next to the symbol; actually I'd remove the periods from the symbol. – egreg Jun 15 '15 at 15:24
  • 1
    Your variant removes a space where it should remain: 10 a.u.from Earth.. Concerning the general question of whether one should write two dots or only one: I actually prefer the double dot, but I won't mark my thesis... – Jost Jun 15 '15 at 15:41
  • 1
    @Jost Fixed. For what it's worth, you have my sympathy. ;-) Is IAU authoritative? I guess so; look at iau.org/publications/proceedings_rules/units where “au” is recommended. – egreg Jun 15 '15 at 16:08
  • Not quite, the IAU fittingly talks about the astronomical length unit (au). This question is about atomic units (a.u.), which are on a totally different scale. But this mix-up explains, why your text is about stars and Earth being separated by a few picometers (1 a.u. ~ 53 pm). – Jost Jun 15 '15 at 17:03
8

If it is feasible for you, you may want to introduce a new command for this. In my solution, you will not be able to write \SI{123}{\au}, but it will handle all possibilities.

% arara: pdflatex

\documentclass{article}
\usepackage{xspace}
\usepackage{siunitx}
\makeatletter
\DeclareRobustCommand{\au}[1]{%
    \@ifnextchar{.}
    {\SI{#1}{{a.u}}}
    {\SI{#1}{{a.u.}}\ifmmode\else\@\xspace\fi}%
}
\makeatother

\begin{document}
    In the middle \au{123} of a sentence. At the end of a sentence \au{123}. In inline $a\au{123}b$ and in a display equation \[a\au{123}b\]

    The momentum is $p=\au{1}$, so the energy is $E={}$\au{0.5}. % This is the only caveat here...
    The position is therefore unknown.
\end{document}

enter image description here

  • You are missing a %, it doesn't matter here because \@ifnextchar ignores spaces, still, I would add the %. – Manuel Jun 15 '15 at 16:40
  • I will edit now (since you added it in the wrong line :D it's needed after the third argument, between arguments TeX skips spaces); but I don't usually do it since, not long ago, an edit (obviously good in my opinion) wasn't well received. – Manuel Jun 15 '15 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.