2

I've made a bytefield packet (see picture), and I want to color the field with the field 256-bit RSA Signature :

enter image description here

Here is the code I used (from another question) :

\documentclass[10pt,a4paper]{article}
\usepackage[table, usenames,dvips, dvipsnames]{xcolor}
\usepackage{bytefield}

\begin{document}
    \begin{bytefield}{32}
        \bitheader{0,2,4,16, 31} \\
        \bitbox{2}{12} & \bitbox{2}{\textcolor{WildStrawberry}{65}} & \bitbox[lrt]{28}{}\\
        \wordbox[lr]{1}{\textcolor{WildStrawberry}{65}-bit EC Public Key}\\
        \bitbox[lrb]{5}{} & \bitbox[lrt]{27}{}\\
        \wordbox[lr]{3}{256-bit RSA Signature} \\
        \bitbox[lrb]{5}{} & \bitbox[lt]{27}{}\\
    \end{bytefield}
\end{document}

I have seen the (not so common) trick to color a field in the bytefield package. They create a command to add a colored boxe, like this :

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

It works well for classical fields, but the \colorbitbox created doesn't allow \bytefiled with options like [lrb].

How can I do this for a field splitted on multiple lines ?

2

It's easy to modify \colorbitbox to accept the optional parameter and declare an equivalent \colorwordbox.

The problem is that the result is not nice, you can see on top figure below. Width word fields original definition is not possible to just draw some part of its borders as it's needed in this example.

So my best solution (second bytefield code) is that you manually divide each wordbox in as many as needed bitbox. The result is the second figure.

\documentclass[10pt,a4paper]{article}
\usepackage[table, usenames, dvipsnames]{xcolor}
\usepackage{bytefield}
\usepackage{graphicx}

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

\newcommand{\colorwordbox}[4][]{%
\rlap{\wordbox[#1]{#3}{\color{#2}\rule{\width}{\height}}}%
\wordbox[#1]{#3}{#4}}

\definecolor{lightcyan}{rgb}{0.84,1,1}
\definecolor{lightgreen}{rgb}{0.64,1,0.71}
\definecolor{lightred}{rgb}{1,0.7,0.71}

\begin{document}
    \begin{bytefield}{32}
        \bitheader{0,2,4,16, 31} \\
        \bitbox{2}{12} & \bitbox{2}{\textcolor{WildStrawberry}{65}} 
              & \bitbox[lrt]{28}{}\\
        \wordbox[lr]{1}{\textcolor{WildStrawberry}{65}-bit EC Public Key}\\
        \bitbox[lrb]{5}{} & \colorbitbox[lrt]{lightcyan}{27}{}\\
        \colorwordbox[lr]{lightcyan}{3}{256-bit RSA Signature} \\
        \colorbitbox[lrb]{lightcyan}{5}{} & \bitbox[lt]{27}{}\\
    \end{bytefield}

    \begin{bytefield}{32}
        \bitheader{0,2,4,16, 31} \\
        \bitbox{2}{12} & \bitbox{2}{\textcolor{WildStrawberry}{65}} 
              & \bitbox[lrt]{28}{}\\
        \wordbox[lr]{1}{\textcolor{WildStrawberry}{65}-bit EC Public Key}\\
        \bitbox[lrb]{5}{} & \colorbitbox[lrt]{lightcyan}{27}{}\\
        \colorbitbox[lt]{lightcyan}{5}{}\colorbitbox[r]{lightcyan}{27}{} \\
        \colorbitbox[lr]{lightcyan}{32}{256-bit RSA Signature} \\
        \colorbitbox[l]{lightcyan}{5}{}\colorbitbox[br]{lightcyan}{27}{} \\
        \colorbitbox[lrb]{lightcyan}{5}{} & \bitbox[lt]{27}{}\\
    \end{bytefield}
\end{document}

enter image description here

  • Thanks for the option ! But it seems tht when including the options, it doesn't display the frame around anymore... Have you a workaround ? – EisenHeim Apr 9 '16 at 10:33
  • @EisenHeim I'm not sure to understand your question. My second example is a workaround for this problem which is shown in first example. Do you have a different one? – Ignasi Apr 11 '16 at 9:52
  • For another application, I tried to do it with a simple example, juste a colorbitbox on a 2 boxes-packet. But the borders around the colored box have disappeared, unless I explicitely say I want them with [lrbt]. I found it odd, it's just that. I tried with this : \begin{bytefield}{32} \bitheader{0-31} \\ \bitbox{4}{Four} & \bitbox{8}{Eight} & \colorbitbox{lightcyan}{16}{Sixteen} & \bitbox{4}{Four} \end{bytefield} – EisenHeim Apr 11 '16 at 12:27
  • 1
    @EisenHeim. This one is easy. Change \newcommand{\colorbitbox}[4][]{% with \newcommand{\colorbitbox}[4][lrtb]{%. This way, every colorbitbox has its four sides drawn by default. And for colorwordbox the default could be lr. – Ignasi Apr 11 '16 at 14:28

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.