# Bytefield colors and baseline align

I just found out about the bytefield package and read the documentation to customize the default layout a bit.

1. I followed the documentation to make sure the baselines of all fields match:

\newcommand{\baselinealign}[1]{%
\centering
\small
\raisebox{0pt}[\bitboxmaxheight][0pt]{#1}%
}

2. I want background colors for some fields so I copied the code for \colorbitbox from the documentation.

\newcommand{\colorbitbox}[3]{%
\rlap{\bitbox{#2}{\color{#1}\rule{\width}{\height}}}%
\bitbox{#2}{#3}
}


However, combining them both, prints the colored background too high.

\newlength{\bitboxmaxheight}
\setlength{\bitboxmaxheight}{\heightof{W}}
\newcommand{\baselinealign}[1]{%
\centering
\small
\raisebox{0pt}[\bitboxmaxheight][0pt]{#1}%
}
\newcommand{\colorbitbox}[3]{%
\rlap{\bitbox{#2}{\color{#1}\rule{\width}{\height}}}%
\bitbox{#2}{#3}
}
\begin{bytefield}[boxformatting={\baselinealign}]{32}
\bitbox{4}{Version} & \bitbox{4}{Type} & \colorbitbox{lightgray}{8}{Unused} & \bitbox{16}{Checksum} \\
\end{bytefield}


How can I solve this issue?

• Welcome to TeX.SX! I think you have hit the "send" button too early, because the list seems to be uncompleted and I cannot identify a question so far. Please press the "edit" button at the end of your question and complete the question. Apr 4, 2016 at 10:06
• Correct, Stefan. I pressed <enter> when I wanted to apply a tag, but that submitted the question. Apologies for that. Apr 4, 2016 at 10:09

Instead of guessing the value of the amount of raising, measure it:

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

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

\begin{document}

\begin{bytefield}[boxformatting={\baselinealign}]{32}
\bitbox{4}{Version} & \bitbox{4}{Type} & \colorbitbox{lightgray}{8}{Unused} & \bitbox{16}{Checksum} \\
\end{bytefield}

\end{document}


Instead of the trick with \heightof, I just insert a strut in normal size, before issuing \small.

• With the IEEEtran document class this does not work and bitboxes (w/o color) are higher and get rendered on top of colored boxes of the previous line. No idea why but IEEEtran mangles lots of settings related to baseline spacings... Nov 3, 2017 at 15:13
• @stefanct This seems to require a new question with all details. Nov 3, 2017 at 15:16
• Does it? :) Just replace article with IEEEtran in your MWE above and you should see the problem. I have worked around it by simply using colorbitboxes with white background. In a new question I would not know what to ask but "how to do that but in an IEEEtran document?", which seems like a very bad way to spread information on your almost perfect solution for future users IMHO but I can of course do so if you want. NB: This question does not specify the document class either. Nov 3, 2017 at 18:45

After a bit of playing around, the following code seems to do the job. However, I would love to see a better way to use the \raisebox instead of determining the vertical "skip" by trial-and-error.

\newcommand{\colorbitbox}[3]{%
\rlap{\bitbox{#2}{\raisebox{-1.3ex}{\color{#1}\rule{\width}{\height}}}}%
\bitbox{#2}{#3}
}


Thanks to @egreg's answer, I was able to come up with the following:

\newcommand{\baselineboxformatting}[1]{
% Measure size of contents
\sbox0{\small#1}
% Use the difference between the contents' height and the bitbox's height,
% clamped to [-.3\baselineskip, 0], as our minimum depth.
\setlength{\skip0}{\ht0 - \height}
\ifdim\skip0>0pt
\setlength{\skip0}{0}
\else
\ifdim\skip0<-.3\baselineskip
\setlength{\skip0}{-.3\baselineskip}
\fi
\fi
\centering\rule[\skip0]{0pt}{\baselineskip}\small#1
}


This avoids needing to mess with \colorbitbox to compensate; instead, the formatting itself should automatically handle anything too large to fit in with a full .3\baselineskip of depth, which extends to anything, not just \rule.

• There are spurious spaces all over the place. This is not a good answer. Dec 2, 2018 at 5:06
• @HenriMenke Please explain? I missed of \small in front of both my #1s to match the question, but otherwise I cannot visually distinguish between the output of this and the accepted answer. Do you simply mean that I need % at the end of some of my lines? Dec 2, 2018 at 18:01