# A Binary Code Index for Enumerate Items Using Stackengine

I am currently working on a project that contains hundreds of enumerate items, each containing a paragraph(s) that addresses some combination of anywhere between 1 and 12 different subjects. The list is getting rather cumbersome without any aid to differentiating the contents of each entry. So, if would like to append a little binary index to each entry containing a sequence of 12 colored balls, representing 12 different subjects, either red (indicating a certain subject is addressed) or blue (if that subject is not addressed). Something like this---

which would indicate that, the first item in the enumerate list addresses only the tenth, eleventh, and twelfth subjects (with each particular subject being assigned a unique integer from 1 to 12).

What I have so far is this:

\documentclass{article}
\textheight 8.25in \textwidth 4.75in

\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{white}\rule{5ex}{1.15ex}}{%
\color{blue!35}$\mkern1mu\makeballs{#1}% \textcolor{red}{\makeballs{\the\numexpr12-#1\relax}}$}{O}{c}{F}{F}{L} }
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
\expandafter\makeballs\the\numexpr#1-1\relax\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[4pt]{\color{blue!65!black}%
\bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
\enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[9] \lipsum[11]
\item[4] \lipsum[39]
\item[11] \lipsum[13]
\item[1] \lipsum[13]
\end{benumerate}
\end{document}


which gives the output

The code is a modification of Steven B. Segletes' answer to a question which required only, that the red dots be consecutive and all located at the end of a 4-ball sequence.

Create a custom numbered list with little balls and gray rectangle (also with the shadows)

I have figured out how to display a 12-ball string as shown in the first two items. However, when I request either more than than ten blue balls (such as \item[11] \lipsum[13]) or more than ten red balls (\item[1] \lipsum[13]), get the strange output shown, respectively, in the third and fourth displayed items of the MWE.

\Moreover, I have not been able to figure out how to display a binary index that does not have all the red balls occurring consecutively at the end of the string; say, for example, red at the 1st, 3rd, 4th, and 10th entries and blue everywhere else.

So, I would like to ask the following.

QUESTION: How may I modify the given code so that (i) more than 10 consecutive colors can be accommodated, (ii) the balls of one color need not be consecutive; e.g., RBRRBBBBBRBB, and (iii) the 12-ball string is displayed not horizontally all at once, but as a 3x4 matrix'' where the top row contains the colored balls for the first four subjects, the next row the colored balls for subjects 5-8, and the last for subjects 9-12 (i.e., as the ancient Greeks would have displayed the oblong number 12)?

Thank you.

PART 1

You needed a brace around the recursive argument to \makeballs in the definition of \makeballs. Without it, any two-digit argument results in unexpected results. I also added an \expandafter chain in the same location so that the recursion passed a clean number rather than a \numexpr relation.

\documentclass{article}
\textheight 8.25in \textwidth 4.75in

\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{white}\rule{5ex}{1.15ex}}{%
\color{blue!35}$\mkern1mu\makeballs{#1}% \textcolor{red}{\makeballs{\the\numexpr12-#1\relax}}$}{O}{c}{F}{F}{L} }
\def\makeballs#1{\ifnum#1>0\relax{\bullet}%
\expandafter\makeballs\expandafter{\the\numexpr#1-1\relax}\fi}% <--FIX HERE
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[4pt]{\color{blue!65!black}%
\bfseries\sffamily\theenumi}{\difbox{\difficulty}}}}%
\enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[9] \lipsum[11]
\item[4] \lipsum[39]
\item[11] \lipsum[13]
\item[1] \lipsum[13]
\end{benumerate}
\end{document}


PART 2

As to the greater issue, that of choosing one's own sequence of red and blue balls, I change tack and make the user specify the red/blue sequence with a string composed of R and B tokens.

Here, \makeballs now accepts the input string as its argument and strips off the first token, using it to decide red or blue. It then recursively passes the rest of the input tokens back to itself, until the list is exhausted.

\documentclass{article}
\textheight 8.25in \textwidth 4.75in

\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{white}\rule{5ex}{1.15ex}}{%
$\iftrue\makeballs#1\fi$}{O}{c}{F}{F}{L} }
\def\makeballs#1#2\fi{\fi\ifx R#1\textcolor{red}{\bullet}\else
\ifx B#1\textcolor{blue!35}{\bullet}\fi\fi%
\ifx\relax#2\relax\else\makeballs#2\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[4pt]{\color{blue!65!black}%
\bfseries\sffamily\theenumi}{\expandafter\difbox\expandafter{\difficulty}}}}%
\enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[RB] \lipsum[11]
\item[RRRBBBB] \lipsum[39]
\item[RBBRRRRBRB] \lipsum[13]
\end{benumerate}
\end{document}


Note: the uncentered number over the balls results from the extra space that the OP inserted at the end of the \difbox definition ({O}{c}{F}{F}{L} }). I left it there, as perhaps the OP intended it. However removing that space will result in the number being centered over the balls.

PART 3

Part 3 is a bit trickier. Here, I need to build the argument to the ball stack in advance (in \tmp) and then pass it to a \Shortstack. Fortunately, \makeballs is expandable in this regard, as long as I apply \noexpand to instances of \textcolor (and, optionally, \bullet).

In addition to R and B tokens in the ball specification, I now use . to signify line breaks.

\documentclass{article}
\textheight 8.25in \textwidth 4.75in

\usepackage{stackengine,xcolor,lipsum}
\setstackEOL{\\}
\let\svitem\item
\newcommand\difbox[1]{\edef\tmp{$\iftrue\makeballs#1\fi$}%
\stackengine{0pt}{\color{white}\rule{5ex}{1.15ex}}{%
\expandafter\Shortstack\expandafter{\tmp}}{O}{c}{F}{F}{L} }
\def\makeballs#1#2\fi{\fi\ifx R#1\noexpand\textcolor{red}{\noexpand\bullet}\else
\ifx B#1\noexpand\textcolor{blue!35}{\noexpand\bullet}\else
\ifx .#1$\\$\fi\fi\fi%
\ifx\relax#2\relax\else\makeballs#2\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
\def\labelenumi{\smash{\stackunder[4pt]{\color{blue!65!black}%
\bfseries\sffamily\theenumi}{\expandafter\difbox\expandafter{\difficulty}}}}%
\enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[RB.RR] \lipsum[11]
\item[RRRB.BBBR] \lipsum[39]
\item[RBBR.RRRB.RBBR] \lipsum[13]
\end{benumerate}
\end{document}


• Many thanks. This is very helpful to me.. Sep 15, 2021 at 17:47
• You are most welcome, @mlchristians Sep 15, 2021 at 17:51
• Would you be able to check the code for Part 3? When I run it, I get error messages for the line containing  \item[RB.RR] such as the use of \\stackunder doesn't match its definition. As far as I can tell, it all relates to  \ifx .#1$\\$\fi\fi\fi% in which there seems to be a problem with .---for when I use other symbols or replace . by $.$, the code runs without error, but also without breaking the colored balls into a matrix shape. The problem may very well be on my end, but if you would check the code, I would appreciate it. Many thanks. Sep 16, 2021 at 14:17
• @mlchristians Do you have the latest version of stackengine? You can find it at ctan.org/pkg/stackengine Sep 16, 2021 at 15:06
• Unfortunately, I don't believe I do. I was afraid that might be the problem. Sep 16, 2021 at 15:21