4

The manual of the bytefield package explains how to color a normal bitfield with a colored \rule. I'm also using the \skippedwords command in my figures. I would like to also color that fields. However, it seems that there is no option in the bytefield package for a general color changing. Does somebody has an idea how to accomplish that, a workaround or a package suggestion? I know that I could draw everything with tikz but I would like to avoid that.

  • 1
    Providing a working example would be beneficial for those seeking to offer help. Welcome to the site. – Steven B. Segletes Feb 6 '14 at 20:19
4

In the following, I defined a command \cskippedwords allowing you to apply a background color using the mandatory argument. The syntax is

\cskippedwords[<length>]{<color>}

where the optional argument <length> has the same meaning that for the standard \skippedwords, and color will be applied with a 0.2 opacity.

The code:

\documentclass{article}
\usepackage{tikz}
\usepackage{bytefield}

\makeatletter
\DeclareRobustCommand{\cskippedwords}[2][2ex]{%
  \setlength{\units@wide}{\bf@bitwidth * \bits@wide}%
  \setlength{\units@high}{1pt * \ratio{\units@wide}{6.0pt}}%
  \setlength{\units@tall}{#1 + \units@high}%
  \edef\num@wide{\strip@pt\units@wide}%
  \edef\num@tall{\strip@pt\units@tall}%
  \edef\num@high{\strip@pt\units@high}%
  \begin{picture}(\num@wide,\num@tall)%
  \begin{tikzpicture}[overlay]
  \draw[fill=#2,fill opacity=0.2] 
    (0,-\bf@bitheight) -- (0,0) -- (0,\units@high) -- (\units@wide,0) -- +(0,-\bf@bitheight) -- cycle;
  \draw[fill=#2,fill opacity=0.2]
    (0,\units@high+#1+\bf@bitheight) -- ++(\units@wide,0) -- ++(0,-\bf@bitheight-\units@high) -- (0,\units@high+#1) -- cycle;
  \end{tikzpicture}%
  \end{picture}%
  \ifcounting@words
    \inc@bytefield@height{\unitlength * \real{\num@tall}}%
    \global\counting@wordsfalse
  \fi}
\makeatother
\begin{document}

\begin{bytefield}{16}
\wordbox{1}{Some data} \\
\wordbox[lrt]{1}{Lots of data} \\
\cskippedwords{blue!20} \\
\wordbox[lrb]{1}{} \\
\wordbox{2}{More data}
\end{bytefield}

\vspace{1cm}

\begin{bytefield}[bitheight=2.5\baselineskip]{32}
\bitheader{0,7,8,15,16,23,24,31} \\
\begin{rightwordgroup}{\parbox{6em}{\raggedright These words were taken
verbatim from the TCP header definition (RFC~793).}}
\bitbox{4}{Data offset} & \bitbox{6}{Reserved} &
\bitbox{1}{\tiny U\\R\\G} & \bitbox{1}{\tiny A\\C\\K} &
\bitbox{1}{\tiny P\\S\\H} & \bitbox{1}{\tiny R\\S\\T} &
\bitbox{1}{\tiny S\\Y\\N} & \bitbox{1}{\tiny F\\I\\N} &
\bitbox{16}{Window} \\
\bitbox{16}{Checksum} & \bitbox{16}{Urgent pointer}
\end{rightwordgroup} \\
\wordbox[lrt]{1}{Data octets} \\
%\skippedwords \\
\cskippedwords{green} \\
\wordbox[lrb]{1}{} \\
\begin{leftwordgroup}{\parbox{6em}{\raggedright Note that we can display,
for example, a misaligned 64-bit value with clever use of the
optional argument to \texttt{\string\wordbox} and
\texttt{\string\bitbox}.}}
\bitbox{8}{Source} & \bitbox{8}{Destination} &
\bitbox[lrt]{16}{} \\
\wordbox[lr]{1}{Timestamp} \\
\begin{rightwordgroup}{\parbox{6em}{\raggedright Why two Length fields?
No particular reason.}}
\bitbox[lrb]{16}{} & \bitbox{16}{Length}
\end{leftwordgroup} \\
\bitbox{6}{Key} & \bitbox{6}{Value} & \bitbox{4}{Unused} &
\bitbox{16}{Length}
\end{rightwordgroup}
\end{bytefield}

\end{document}

enter image description here

  • @Flasher You're welcome. I updated my answer fixing a little problem with one of the lengths I used; the improved code might be of interest for you. – Gonzalo Medina Feb 7 '14 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.