位框内的规则未对齐

位框内的规则未对齐

我想绘制一个字节字段。在其文档有两个技巧,一个用于基线文本对齐,一个用于使用未使用字段的规则为位域着色。我想把这两个建议放在一起。基线对齐工作正常,但现在如果我在位框中驱动具有位框高度和宽度的规则,它的位置错误:

在此处输入图片描述

我如何正确设置基场和线对齐?

这是代码:

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

答案1

将以下定义放入你的序言中:

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

并使用不应与基线对齐的单元格的内容作为参数\baselineunalign

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

新命令的效果是计算内容向上移动的量,并通过相同大小的负移动来撤消此移动。

答案2

您可以简化\baselinealign并定义\colorbitbox命令,如下所示字节场颜色和基线对齐

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

在此处输入图片描述

相关内容