6

I am using siunitx to typeset numbers and units. But when typesetting numbers in front of a variable, I feel a thin space is missing.

For example, with \num{0.1234}a I get (1), and with \num{0.1234}\,a I get (2) which looks better. Is there a way to automate this or do I have to manually put a thin space after each number?

enter image description here

1
  • 1
    Assuming that a is some sort of unit you should use \SI{0.1234}{a}. Also, while code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Commented Jan 2, 2016 at 10:25

1 Answer 1

8

I defined \mynum for you which checks if the next token is of catcode “letter”.

\documentclass{article}
\usepackage{siunitx}
\ExplSyntaxOn

\NewDocumentCommand \mynum { o m }
 {
  \num [ #1 ] { #2 }
  \peek_catcode_ignore_spaces:NT \c_catcode_letter_token { \, }
 }

\ExplSyntaxOff
\begin{document}
Normal behaviour (very recommended)
\begin{equation}
  \num{0,1234}\,a
\end{equation}
Desired behaviour
\begin{equation}
  \mynum{0,1234}a
\end{equation}
\end{document}

enter image description here


You can also override the \num command provided by siunitx, which is not recommended. Therefore, I copied the definition of \num from siunitx.sty and appended the relevant bits.

\documentclass{article}
\usepackage{siunitx}

\ExplSyntaxOn

\RenewDocumentCommand \num { o m }
 {
  % begin from siunitx.sty
  \leavevmode
  \group_begin:
   \IfNoValueF {#1}
    { \keys_set:nn { siunitx } {#1} }
   \__siunitx_number_output:n {#2}
  \group_end:
  % end from siunitx.sty
  \peek_catcode_ignore_spaces:NT \c_catcode_letter_token { \, }
 }

\ExplSyntaxOff

\begin{document}
$\num{0,1234}a$
\end{document}

enter image description here

5
  • What about $\num{0.1234} a$?
    – egreg
    Commented Jan 2, 2016 at 15:21
  • @egreg That is why it is not recommended. Or use \peek_catcode_ignore_spaces:NT. Commented Jan 2, 2016 at 15:25
  • You might use \peek_catcode_ignore_spaces:NT
    – egreg
    Commented Jan 2, 2016 at 15:28
  • As I explained in the other question, do not save and redirect \num, instead look at how it's defined and base your new version on the code-level functions (yes, I know they are marked as 'internal').
    – Joseph Wright
    Commented Jan 2, 2016 at 15:44
  • Well, you guys are way above my level. Redefining \num seems indeed a bit too much. I was looking for a simple way to automatically generate a thin space between a variable name and a leading coefficient. I guess I will put thin spaces manually.
    – Bibi
    Commented Jan 5, 2016 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .