1

I want to draw a bytefield. In its documentation there are two tricks, one for baseline text aligment and one for coloring a bitfield with a rule for unused fields. I wanted to put these two suggestions together. The baseline aligment works correctly, but now If I drive in a bitbox a rule with bitboxbox height and width, it has a wrong position:

enter image description here

How can I set correctly both basefield and line aligment?

This is the code:

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{fourier}
\usepackage{epstopdf}
\usepackage{geometry}
\usepackage{bytefield}

%%% Colors %%%
\definecolor{niceblue}{rgb}{.047,.372,.478}
\definecolor{titlecolor}{rgb}{.749,.937,1.0}
\definecolor{reservedfield}{gray}{0.8}

%%% cigipacket environment %%%
% This environment is the bitfield one with some customization
\newlength{\wletterheight}
\setlength{\wletterheight}{\heightof{W}}
\newcommand{\baselinealign}[1]{%
\centering
\raisebox{0pt}[\wletterheight][0pt]{#1}%
}
\newenvironment{cigipacket}
{\begin{bytefield}[boxformatting=\baselinealign,endianness=big]{32}}
{\end{bytefield}}

\begin{document}
CIGI communication is based on packets. A packet is a byte stream of defined size, that can be fixed or variable according to packet type.

\begin{figure}
\centering
\begin{cigipacket}
\bitheader{0-31} \\
\bitbox{8}{Packet ID = 7}
\bitbox{8}{Packet Size = 16}
\bitbox{16}{Entity ID} \\
\bitbox{8}{Field 1}
\bitbox{8}{yyField 2}
\bitbox{1}{4}
\bitbox{1}{3}
\bitbox{3}{2}
\bitbox{3}{1}
\bitbox{8}{\color{reservedfield}\rule{\width}{\height}} \\
\bitbox{32}{DOF 1} \\
\bitbox{32}{DOF 2}
\end{cigipacket}
\caption{Packet example.}
\label{fig:packetexample}
\end{figure}
\end{document}
Jepessen
  • 173
  • 6

2 Answers2

4

You can simplify \baselinealign and define a \colorbitbox command as in Bytefield colors and baseline align

\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{color}
\usepackage{graphicx}
\usepackage{fourier}
\usepackage{epstopdf}
\usepackage{geometry}
\usepackage{bytefield}

%%% Colors %%%
\definecolor{niceblue}{rgb}{.047,.372,.478}
\definecolor{titlecolor}{rgb}{.749,.937,1.0}
\definecolor{reservedfield}{gray}{0.8}

%%% https://tex.stackexchange.com/a/302403/4427
\newcommand{\baselinealign}[1]{%
  \centering
  \strut#1%
}
\newcommand{\colorbitbox}[3]{%
  \sbox0{\bitbox{#2}{#3}}%
  \makebox[0pt][l]{\textcolor{#1}{\rule[-\dp0]{\wd0}{\ht0}}}%
  \bitbox{#2}{#3}%
}


%%% cigipacket environment %%%
% This environment is the bitfield one with some customization
\newenvironment{cigipacket}
{\begin{bytefield}[boxformatting=\baselinealign,endianness=big]{32}}
{\end{bytefield}}

\begin{document}
CIGI communication is based on packets. A packet is a byte stream of 
defined size, that can be fixed or variable according to packet type.

\begin{figure}
\centering
\begin{cigipacket}
\bitheader{0-31} \\
\bitbox{8}{Packet ID = 7}
\bitbox{8}{Packet Size = 16}
\bitbox{16}{Entity ID} \\
\bitbox{8}{Field 1}
\bitbox{8}{yyField 2}
\bitbox{1}{4}
\bitbox{1}{3}
\bitbox{3}{2}
\bitbox{3}{1}
\colorbitbox{reservedfield}{8}{} \\
\bitbox{32}{DOF 1} \\
\bitbox{32}{DOF 2}
\end{cigipacket}
\caption{Packet example.}
\label{fig:packetexample}
\end{figure}

\end{document}

enter image description here

egreg
  • 1,121,712
  • @egreg Your solution is superior ... – gernot Aug 19 '16 at 21:38
  • Is there a way to make this work with \bitboxes* as well? I tried: \newcommand{\colorbitboxes}[3]{% \sbox0{\bitboxes{#2}{#3}}% \makebox[0pt][l]{\textcolor{#1}{\rule[-\dp0]{\wd0}{\ht0}}}% \bitboxes*{#2}{#3}% } (sorry, apparently can’t format in a comment), and that fails with “You can't use `macro parameter character #' in internal vertical mode.” This is all black magic to me, so I am stuck. – James Elliott May 22 '17 at 22:08
  • @JamesElliott Please make a followup question. – egreg May 22 '17 at 22:12
  • I also can’t figure out how to pass arguments like [ltb] to \colorbitbox which would be an alternative to implementing \colorbitboxes*, albeit more versbose since I have a lot of bits I want in there. – James Elliott May 22 '17 at 22:13
  • OK, thanks @egreg, I will look for the followup question interface. I am brand new to this forum. – James Elliott May 22 '17 at 22:13
  • @JamesElliott Just ask a new question and add a link to this one in the body. – egreg May 22 '17 at 22:15
  • All right, that’s done, thanks (that was the impression I got by finding a few example followup questions as well.) Should I clean up after myself by deleting my unreadable comments here, now? – James Elliott May 22 '17 at 22:37
1

Put the following definitions into your preamble:

\newlength\unalignheight
\newcommand\baselineunalign[1]%
 {\unalignheight\height
  \advance\unalignheight-\wletterheight
  \divide\unalignheight by 2%
  \raisebox{-\unalignheight}{#1}%
 }

and use the contents of the cell that should not get baseline-aligned as argument for \baselineunalign:

\bitbox{8}{\baselineunalign{\color{reservedfield}\rule{\width}{\height}}}

The effect of the new command is to compute the amount that the contents is shifted upwards and to undo this shift by a negative shift of the same size.

gernot
  • 49,614