# Combining color, bitboxes* and base alignment

The code in Misaligment in a rule inside a bitbox has got me almost all of where I need to go, but I also want to be able to color \bitboxes*.

I tried this variant of @egreg’s \colorbitbox:

\newcommand{\colorbitboxes*}[3]{%
\sbox0{\bitboxes*{#2}{#3}}%
\makebox[0pt][l]{\textcolor{#1}{\rule[-\dp0]{\wd0}{\ht0}}}%
\bitboxes*{#2}{#3}%
}


But unfortunately when I try to use that it fails with: You can't use ‘macro parameter character #’ in internal vertical mode.

I also can’t figure out how to pass arguments like [ltb] to \colorbitbox, which would be another way to get the output I want, albeit much more verbose, since I have twelve bits I want in there. When I try that, it says ! Missing number, treated as zero.

I know some LaTeX, but this is deep into voodoo for me, so I am stuck, any help would be greatly appreciated!

To expand on my comment, @Mike’s answer looks like this with my bytefield (code below):

\newcommand{\baselinealign}[1]{%
\centering
\strut#1%
}

\begin{bytefield}[bitwidth=1.9em, leftcurly=., leftcurlyspace=0pt, boxformatting={\baselinealign}]{16}

\begin{leftwordgroup}{\tiny\bfseries 00}

\colorbitbox{lightgreen}{1}{\tt 11} & \colorbitbox{lightgreen}{4}{\tt 872349ae} &
\colorbitbox{yellow}{1}{\tt 11} & \colorbitbox{yellow}{4}{$TxID$} &
\colorbitbox{lightred}{1}{\tt 10} & \colorbitbox{lightred}{2}{$type$} &
\colorbitbox{lightcyan}{1}{\tt 0f} & \colorbitbox{lightcyan}{1}{$n$} &
\colorbitbox{lightpurple}{1}{\tt 14}
\end{leftwordgroup} \\

\begin{leftwordgroup}{\tiny\bfseries 10}
\colorbitbox{lightpurple}{4}{{\tt 0000000c}\small{ (12)}} &
\colorbitbox[lbt]{lightpurple}{1}{$t_1$} & \colorbitbox[bt]{lightpurple}{1}{$t_2$} &
\colorbitbox[bt]{lightpurple}{1}{$t_3$} & \colorbitbox[bt]{lightpurple}{1}{$t_4$} &
\colorbitbox[bt]{lightpurple}{1}{$t_5$} & \colorbitbox[bt]{lightpurple}{1}{$t_6$} &
\colorbitbox[bt]{lightpurple}{1}{$t_7$} & \colorbitbox[bt]{lightpurple}{1}{$t_8$} &
\colorbitbox[bt]{lightpurple}{1}{$t_9$} & \colorbitbox[bt]{lightpurple}{1}{$t_{10}$} &
\colorbitbox[bt]{lightpurple}{1}{$t_{11}$} & \colorbitbox[rbt]{lightpurple}{1}{$t_{12}$}
\end{leftwordgroup} \\

\begin{leftwordgroup}{}
\wordbox[lrt]{1}{Arguments} \\
\skippedwords \\
\wordbox[lrb]{1}{}
\end{leftwordgroup}

\end{bytefield}


Once upon a time, I came up with this, which worked fine so far:

\documentclass{article}
\usepackage{bytefield}
\usepackage{xcolor}

\newcommand{\colorwordbox}[4][rlbt]{%
\rlap{\wordbox[#1]{#3}{\color{#2}\rule{\dimexpr\width-0.4pt}{\dimexpr\height-0.4pt}}}%
\wordbox[#1]{#3}{#4}}
\newcommand{\colorbitbox}[4][lrbt]{%
\rlap{\bitbox[#1]{#3}{\color{#2}\rule{\dimexpr\width-0.4pt}{\dimexpr\height-0.4pt}}}%
\bitbox[#1]{#3}{#4}}

\begin{document}
\begin{bytefield}[endianness=big,
bitwidth=3.5em]{8}
\colorwordbox{green}{1}{a green byte}\\
\wordbox[lrb]{1}{a byte without color}\\
\colorwordbox[]{red}{1}{a red byte with no frame}\\
\colorbitbox{green}{3}{3 bits} &
\bitbox{1}{bit 4}              &
\colorbitbox{green}{1}{bit 3}  &
\colorbitbox{red}{1}{bit 2}    &
\colorbitbox{green}{2}{2 bits}\\
\bitbox{1}{bit 7} &
\bitbox{1}{bit 6} &
\colorbitbox[]{cyan}{4}{4 bits, no frame} &
\bitbox{1}{bit 1} &
\colorbitbox{cyan}{1}{bit 0}
\end{bytefield}
\end{document}


A downside: due to \dimexpr\width-0.4pt and \dimexpr\height-0.4pt colored fields without frame are a bit to short. But without it, they would overdraw neighbouring frames partially.

• That’s almost working, but the color is not quite vertically aligned, perhaps because of the fact that I am using boxformatting={\baselinealign} in my bytefield, and the edges of the color are not quite reaching each other when I omit the left and right frames. So I guess I am still looking for a colorized bitboxes* that is compatible with base alignment. – James Elliott May 23 '17 at 3:13
• (And I realize that your answer pointed out the edges wouldn’t meet. I might actually be able to live with that if the vertical alignment could be sorted out. I updated my question to show the results I’m getting with this approach.) – James Elliott May 23 '17 at 3:25
• The gap between frameless fields can be fixed by writing \width and \height instead of \dimexpr\width-0.4pt and \dimexpr\height-0.4pt (with the downside of partially overdrawing neighbouring frames, which can be fixed by drawing the necessary lines ([lt] for 4 bits, no frame)). But this doesn't help with the vertical alignment. So ... (see comment on your answer) – Mike May 23 '17 at 20:12

Combining @egreg’s answer to the linked question with @Mike’s example of how to pass [lrbt] arguments along to the invocation of \bitbox yielded a version that works well enough for me:

\newcommand{\colorbitbox}[4][rlbt]{%
\sbox0{\bitbox[#1]{#3}{#4}}%
\makebox[0pt][l]{\textcolor{#2}{\rule[-\dp0]{\wd0}{\ht0}}}%
\bitbox[#1]{#3}{#4}%
}


Thanks, everyone!