2

Consider the MWE

\documentclass{article}
\textheight 8.25in \textwidth 4.75in
\usepackage{scalefnt}
\usepackage{amsmath,amssymb}

%\blacksquare\hspace*{.7em}
\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}{\blacksquare}\else
  \ifx B#1\textcolor{blue!35}{\blacksquare}\fi\fi%
  \ifx\relax#2\relax\else\makeballs#2\fi}
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
  \def\labelenumi{\smash{\stackunder[5pt]{\color{blue!65!black}%
  \bfseries\sffamily\theenumi}{\expandafter\difbox\expandafter{\difficulty}}}}%
  \enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[RBRRBR] \lipsum[11]
\item[RRR] \lipsum[39]
\item[BRRRB] \lipsum[13]
\end{benumerate}
\end{document}

with output

enter image description here

I would like to widen the gap between the colored squares; but when I replace both instances of \blacksquare with \blacksquare\hspace*{.7em}---I get the extra spacing I want, but relative to the above enumeration, the new horizontal stack appears to be non-aligned; i.e., not lying centered beneath the above number, as follows:

enter image description here

Now, the alignment in the first output is not perfect, but reasonably accurate. However, the alignment in the second output is clearly not centered beneath the corresponding number.

QUESTION: How may I increase the gap between the colored boxes of the original output so that the horizontal stack of colored squares lies reasonably centered below the upper number?

Thank you.

0

1 Answer 1

2

You can use \unskip at the end to remove the trailing space that is causing the offset:

\documentclass{article}
\textheight 8.25in \textwidth 4.75in
\usepackage{scalefnt}
\usepackage{amsmath,amssymb}

\usepackage{stackengine,xcolor,lipsum}
\let\svitem\item
\newcommand\difbox[1]{\stackengine{0pt}{\color{white}\rule{5ex}{1.15ex}}{\makeballs{#1}}{O}{c}{F}{F}{L}}

\newcommand\makeballs[1]{$\iftrue\makeballsA#1\fi \unskip$}
\def\makeballsA#1#2\fi{\fi
  \ifx R#1\textcolor{red}{\blacksquare}\else
  \ifx B#1\textcolor{blue!35}{\blacksquare}\fi\fi \hspace{.7em}%
  \ifx\relax#2\relax \else\makeballsA#2\fi}
  
\newenvironment{benumerate}
{\renewcommand\item[1][1]{\def\difficulty{##1}\svitem}%
  \def\labelenumi{\smash{\stackunder[5pt]{\color{blue!65!black}%
  \bfseries\sffamily\theenumi}{\expandafter\difbox\expandafter{\difficulty}}}}%
  \enumerate}{\endenumerate}

\begin{document}
\large
\begin{benumerate}
\setcounter{enumi}{0}
\item[RBRRBR] \lipsum[11]
\item[RRR] \lipsum[39]
\item[BRRRB] \lipsum[13]
\end{benumerate}
\end{document}

Here I abstracted away \makeballs to a separate macro so it may be reused elsewhere without having to pay attention to the actual working details.

Also, \hspace (unstarred) or \hskip must be used here since \hspace* adds intervening boxes.


Addendum: another way is to add the space only if there is a next iteration.

\newcommand\makeballs[1]{$\iftrue\makeballsA#1\fi$}
\def\makeballsA#1#2\fi{\fi
  \ifx R#1\textcolor{red}{\blacksquare}\else
  \ifx B#1\textcolor{blue!35}{\blacksquare}\fi\fi
  \ifx\relax#2\relax \else\hspace{.7em}\makeballsA#2\fi}

You must log in to answer this question.

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