字节场颜色和基线对齐

字节场颜色和基线对齐

我刚刚发现了这个bytefield包,并阅读了文档以稍微定制默认布局。

  1. 我按照文档确保所有字段的基线匹配:

    \newcommand{\baselinealign}[1]{%
      \centering
      \small
      \raisebox{0pt}[\bitboxmaxheight][0pt]{#1}%
    }
    
  2. \colorbitbox我想要某些字段的背景颜色,因此我从文档中复制了代码。

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

然而,将两者结合起来,会将彩色背景打印得太高。

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

我该如何解决这个问题?

答案1

不要猜测加注金额的价值,而是测量它:

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

我没有使用技巧\heightof,而是在发出之前插入一个正常大小的支柱\small

在此处输入图片描述

答案2

经过一番尝试,下面的代码似乎可以完成这项工作。但是,我希望看到更好的方法来使用,\raisebox而不是通过反复试验来确定垂直“跳过”。

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

答案3

感谢@egreg 的回答,我得出以下结论:

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

这避免了需要进行\colorbitbox补偿;相反,格式化本身应该自动处理任何太大而无法适应.3\baselineskip深度的内容,这可以扩展到任何内容,而不仅仅是\rule

相关内容