我必须绘制许多具有不同数量和不同形状的位盒的字节场。
为此我定义了 3 个函数:
我的字节字段它接受可变数量的参数(使用 xparse 和
\SplitList
)\ProcessList
并定义字节场环境位框列表按顺序获取参数并将它们发送到我的比特盒
我的比特盒它按顺序获取 \bitbox 所需的 2 个参数,并
\bitbox{\value{{bitfieldsize}}{#1}
在 2 个参数准备就绪时调用。
它几乎可以工作...我可以\mybytefield
用不同数量的参数来调用它,它会绘制几个字节字段但位盒的位置错误或者被调用“损坏” \ProcessList
。
任何想法 ?
以下是示例代码:
\documentclass[a4paper, 12pt,english]{article}
\usepackage{graphicx}
\usepackage{bytefield}
\usepackage{calc}
\usepackage{xparse}
\usepackage{etoolbox}
\newtoggle{newarg}
\toggletrue{newarg}
\newcounter{bitfieldsize}
\newcommand\mybitbox[1] {
\iftoggle{newarg}{
\setcounter{bitfieldsize}{ #1 }
\togglefalse{newarg}
}{
\bitbox{\value{bitfieldsize}}{#1}
\toggletrue{newarg}}}
\NewDocumentCommand\bitboxlist { >{\SplitList{;}}m} {
\ProcessList{#1}{\mybitbox}}
\newcounter{instsize}
\NewDocumentCommand\mybytefield{m >{\SplitList{;}}m} {
\setcounter{instsize}{#1 - 1 }
\begin{figure}[htbp]
\begin{center}
\begin{bytefield}[endianness=big,bitwidth=1em]{#1}
\bitheader{0-\value{instsize}} \\
\ProcessList{#2}{\bitboxlist}
\end{bytefield}
\end{center}
\end{figure}}
\newcommand\staticbitbox[4] {
\bitbox{#1}{#2} \bitbox{#3}{#4}}
\begin{document}
\mybytefield{32}{{28;part1};{4;part2}}
%should do
\begin{figure}[htbp]
\begin{center}
\begin{bytefield}[endianness=big,bitwidth=1em]{32}
\bitheader{0-31} \\
\staticbitbox{28}{part1}{4}{part2}
\end{bytefield}
\end{center}
\end{figure}
\end{document}
答案1
您的代码中存在虚假空格(参见参考资料 1)。
%
我在包含空格的行尾添加了所需的内容。
您也可以将center
环境更改为,\centering
因为前者引入了额外的垂直间距
参考
代码
\documentclass[a4paper, 12pt]{article}
\usepackage{bytefield}
\usepackage{xparse}
\usepackage{etoolbox}
\newtoggle{newarg}
\toggletrue{newarg}
\newcounter{bitfieldsize}
\newcommand\mybitbox[1] {% <- here
\iftoggle{newarg}{% <- here
\setcounter{bitfieldsize}{ #1 }% <- here
\togglefalse{newarg}% <- here
}{% <- here
\bitbox{\value{bitfieldsize}}{#1}% <- here
\toggletrue{newarg}% <- here
}}
\NewDocumentCommand\bitboxlist { >{\SplitList{;}}m} {% <- here
\ProcessList{#1}{\mybitbox}}
\newcounter{instsize}
\NewDocumentCommand\mybytefield{m >{\SplitList{;}}m} {% <- here
\setcounter{instsize}{#1 - 1 }% <- here
\begin{figure}[htbp]
\begin{center}
\begin{bytefield}[endianness=big,bitwidth=1em]{#1}
\bitheader{0-\value{instsize}} \\
\ProcessList{#2}{\bitboxlist}% <- here
\end{bytefield}
\end{center}
\end{figure}}
\newcommand\staticbitbox[4] {% <- here
\bitbox{#1}{#2} \bitbox{#3}{#4}}
\begin{document}
\mybytefield{32}{{28;part1};{4;part2}}
%should do
\begin{figure}[htbp]
\centering
\begin{bytefield}[endianness=big,bitwidth=1em]{32}
\bitheader{0-31} \\
\staticbitbox{28}{part1}{4}{part2}
\end{bytefield}
\end{figure}
\end{document}