bytefield 包中的 color \skippedwords 分别表示一般的背景颜色

bytefield 包中的 color \skippedwords 分别表示一般的背景颜色

手册字节字段包解释了如何使用有色的 为普通位域着色\rule。我也在我的图中使用了该\skippedwords命令。我也想为这些字段着色。但是,字节域包中似乎没有用于常规颜色更改的选项。有人知道如何实现这一点、解决方法或包建议吗?我知道我可以用 tikz 绘制所有内容,但我想避免这种情况。

答案1

下面,我定义了一个命令,\cskippedwords允许你使用强制参数应用背景颜色。语法是

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

其中可选参数<length>具有与标准相同的含义\skippedwords,并且颜色将应用不0.2透明度。

代码:

\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}

在此处输入图片描述

相关内容