2

What is the LaTeX command for boxing a number like for example 123 as:


|1|2|3|

I have seen it in a book but can't find it anywhere in TeXnicCenter, which I use.

  • Do you want to place every number in a box, or in other words for every number to have a border? Roughly like the output of \(\fbox{1} \fbox{2} \fbox{3}\)? It would help if you supplied an image of what you want. – N.N. Jul 05 '11 at 13:40

3 Answers3

6

I don't know any packages that provide macros for this, but it can be done easy enough:

\documentclass{article}

\newcommand{\boxednumber}[1]{% \expandafter\readdigit\the\numexpr#1\relax\relax } \newcommand{\readdigit}[1]{% \ifx\relax#1\else \boxeddigit{#1}% \expandafter\readdigit \fi } % Format macro used for every digit, adjust to your liking: \newcommand*{\boxeddigit}[1]{\fbox{#1}\hspace{-\fboxrule}}

\begin{document}

\boxednumber{123}

\boxednumber{\thepage}

\end{document}

Image

See also Good way to make \textcircled numbers? for some tips about the formatting of the digits. However it's actual for circles not boxes.

Martin Scharrer
  • 262,582
  • 1
    I tried to avoid any @ code here to not confuse LaTeX beginners. I like to use \expandafer .. \the\numexpr because it allows to provide the number also as counter value or macro. – Martin Scharrer Jul 05 '11 at 13:52
  • Note: The left line of the frames look funny, but it actually just the usual zooming/anti-aliasing issue of Adobe Reader. – Martin Scharrer Jul 05 '11 at 13:54
3
\documentclass[12pt,a4paper]{scrartcl}
\usepackage[T1]{fontenc}
\makeatletter
\def\BoxNumbers#1{|\BoxNumbers@i#1|\@nil}
\def\BoxNumbers@i#1#2|\@nil{%
  #1|\ifx\relax#2\relax\else\BoxNumbers@i#2|\@nil\fi}
\makeatother
\begin{document}

\BoxNumbers{123} \BoxNumbers{123789}

\end{document}

enter image description here

2

A variation on the theme by Martin:

\makeatletter
\def\boxnumber#1{\mbox{\toks@={\@gobble}\@boxnumber#1\relax\the\toks@}}
\def\@boxnumber#1{\ifx#1\relax % end
  \else
    \toks@=\expandafter{\the\toks@\@numbersep#1}%
    \expandafter\@boxnumber
  \fi}
\def\@numbersep{\kern1pt\vrule\kern1pt}
\makeatother

Here \boxnumber{123} will give (approximately)

1|2|3

egreg
  • 1,121,712