1

I've been trying to print a binary number with a small space, automatically, every four digits. Starting from right and moving to the left.

I found this package that allowed me to achieve this, but unfortunately, it requires me to add a

0b

in front of each number. I'd rather not print out this at all, as I use a

$_2$ 

Can someone help me use this package and not print out

0b

or a different way to achieve this? Thanks

1 Answers1

2

There are probably other ways, but this one (inspired by this answer) seems to work.

\documentclass{article}
\usepackage{soul}
\newcounter{binst}
\makeatletter
\newcommand{\FormatBinary}[1]{\begingroup%
\setcounter{binst}{0}
\def\SOUL@soeverytoken{%
\stepcounter{binst}%
\ifnum\value{binst}=5\relax%
\setcounter{binst}{1}\,%
\fi%
\the\SOUL@token}%
\so{#1}\endgroup} 
\makeatother
\begin{document}
\FormatBinary{100100010010}
\end{document}

enter image description here

  • Hey, this works perfectly! Thank you so much! After I'm done making my document, I'm going to go through this and ask some followup questions so I can understand why this works. Thank you! – Greg Hilston Oct 07 '19 at 02:21
  • For a number like 11110, it will format it as "1111 0" instead of "1 1110", is there a way to fix this? I see the soeveryoken, is there a way to do this but backwards? – Kazh Aug 17 '23 at 00:09