2

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}
  \bitheader{0,3,4,7,8,15,16,31} \\
  \bitbox{4}{Version} & \bitbox{4}{Type} & \colorbitbox{lightgray}{8}{Unused} & \bitbox{16}{Checksum} \\
  \bitbox{32}{Group Address}
\end{bytefield}

How can I solve this issue?

Stefan Pinnow
  • 29,535
Tommiie
  • 526
  • 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. – Stefan Pinnow Apr 04 '16 at 10:06
  • Correct, Stefan. I pressed when I wanted to apply a tag, but that submitted the question. Apologies for that. – Tommiie Apr 04 '16 at 10:09

3 Answers3

4

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}
  \bitheader{0,3,4,7,8,15,16,31} \\
  \bitbox{4}{Version} & \bitbox{4}{Type} & \colorbitbox{lightgray}{8}{Unused} & \bitbox{16}{Checksum} \\
  \bitbox{32}{Group Address}
\end{bytefield}

\end{document}

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

enter image description here

egreg
  • 1,121,712
  • 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... – stefanct Nov 03 '17 at 15:13
  • @stefanct This seems to require a new question with all details. – egreg Nov 03 '17 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. – stefanct Nov 03 '17 at 18:45
0

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}
}
Tommiie
  • 526
0

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.

jrtc27
  • 161
  • There are spurious spaces all over the place. This is not a good answer. – Henri Menke Dec 02 '18 at 05: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? – jrtc27 Dec 02 '18 at 18:01