12

I'm trying to work out if there is a way to get siunitx to natively handle the ~ symbol (\sim) in the same way that it can handle the < and > operators before a number, as I would like to be able to use ~ as short hand for approximately. For example:

\documentclass[12pt,a4paper]{report}
\usepackage{siunitx}
\begin{document}

% Values output the same, with spacing after the < and before the % symbol
\SI{< 10}{\percent} \\
\SI{<10}{\percent} \\

% When writing in math mode a space in placed after the ~ symbol, but not before the % symbol
$\sim10\%$ \\

\end{document}

Ideally I'd like to be able to write something like \SI{~ 10}{\percent} or \SI{\tilde}{10}{\percent}, where {\tilde} is a custom value defined using the DeclareSIUnit\tilde{~} command. But I can't seem to find anything like this for prefix symbols in the documentation. Has anyone else come across a solution to this?

2
  • 1
    What does \SI{\sim 10}{\percent} have that doesn't work?
    – egreg
    Commented Feb 24, 2014 at 17:30
  • I thought I'd tried that permutation already and it didn't work. But you're absolutely right, I've just tried it now and it does indeed work. D'oh! Thanks.
    – cjms85
    Commented Feb 24, 2014 at 17:33

2 Answers 2

16

Using \SI{\sim 10}{\percent} should be OK; maybe you want \approx, instead.

\documentclass[12pt,a4paper]{report}
\usepackage{siunitx}
\begin{document}

\SI{< 10}{\percent}   

\SI{<10}{\percent}   

\SI{\sim 10}{\percent}

\SI{\approx 10}{\percent}

\end{document}

enter image description here

7

I haven't seen an interface for new "prefixes" either, thus the following example patches internals of siunitx. Also I had trouble using ~ as prefix, probably because of rescans, where the catcode of ~ is set to 9, that ignores the characters. Therefore the example uses \~ instead. <> is used a symbol for \approx:

\documentclass[12pt,a4paper]{report}
\usepackage{siunitx}

\ExplSyntaxOn
\exp_args:NNo \cs_set:Nn \__siunitx_number_in_sign_replace:N
 {
  \__siunitx_number_in_sign_replace:N { #1 }
  \tl_replace_all:Nnn #1 { \~ } { \sim }
  \tl_replace_all:Nnn #1 { <> } { \approx }
 }
\tl_put_right:Nn \l__siunitx_input_protect_tl    { \~ }
\tl_put_right:Nn \l__siunitx_input_comparator_tl { \~ }
\ExplSyntaxOff

\begin{document}
  \noindent
  \SI{<10}{\percent}\\
  \SI{\~10}{\percent}\\
  \SI{<>10}{\percent}
\end{document}

Result

5
  • Sorry, but your expl3 coding was terrible. ;-) Isn't the fixed version better? With \exp_args:NNo we jump over two tokens and expand the first token after the {. I don't think Joseph will be happy to see this. ;-)
    – egreg
    Commented Feb 24, 2014 at 19:10
  • @egreg: Thanks for the improved LaTeX3 code :-) It is a pity, that at least some of the modules of LaTeX3 are not available for iniTeX or plain TeX, thus I cannot use it for many of my packages. Here, I first tried etoolbox, but \apptocmd cannot handle different catcodes for the same character (-) inside the definition. Commented Feb 24, 2014 at 19:23
  • @HeikoOberdiek We discussed making the code work without relying on LaTeX (doable, if a little tricky). Please raise on LaTeX-L with a solid use case (Will and I were keen, Frank less so). Plain and IniTeX are easy: ConTeXt slightly more 'fun'.
    – Joseph Wright
    Commented Feb 24, 2014 at 21:29
  • Note that ~ is defined as having a particular meaning for siunitx, inherited from SIstyle, hence it can't really be supported in general input.
    – Joseph Wright
    Commented Feb 24, 2014 at 21:30
  • Another note: patching internal expl3 functions is specially not supported. This can and may well break at some point!
    – Joseph Wright
    Commented Feb 24, 2014 at 21:31

You must log in to answer this question.

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