我正在尝试在 msc 图中添加一个字节字段。这是 MWE:
\documentclass{article}
\usepackage{bytefield}
\usepackage{msc}
\begin{document}
\begin{bytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{bytefield}
\begin{figure}
\begin{msc}[instance distance=5cm]{}
\declinst{alice}{Alice}{}
\declinst{bob}{Bob}{}
\nextlevel[5]
\mess{
\begin{bytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{bytefield}
}{alice}{bob}
\nextlevel[5]
\end{msc}
\end{figure}
\end{document}
实际上,这个 MWE 给出了一个错误:Extra alignment tab has been changed to \cr
。我认为这是由于 msc 造成的,但我不确定如何解决它,它很可能是问题的根源。我尝试过用括号或使用 parbox 包装字节字段,但都没有解决问题。
我真的很感激有人能帮我解决这件事。谢谢!
答案1
环境bytefield
不喜欢被处理为另一个命令的参数。这里有一个解决方法:
\documentclass{article}
\usepackage{bytefield}
\usepackage{msc}
\newsavebox{\bfbox}
\begin{document}
\begin{bytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{bytefield}
\begin{figure}
\begin{lrbox}{\bfbox}
\begin{bytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{bytefield}
\end{lrbox}
\begin{msc}[instance distance=5cm]{}
\declinst{alice}{Alice}{}
\declinst{bob}{Bob}{}
\nextlevel[5]
\mess{\usebox{\bfbox}}{alice}{bob}
\nextlevel[5]
\end{msc}
\end{figure}
\end{document}
或者,定义sbytefield
(safe bytefield
)作为命令的参数。
\documentclass{article}
\usepackage{bytefield}
\usepackage{msc}
\ExplSyntaxOn
\NewDocumentEnvironment{sbytefield}{O{}+b}
{
\tl_set:Nn \l_tmpa_tl { #2 }
\regex_replace_all:nnN { & } { \cS\& } \l_tmpa_tl
\zalessandro_bytefield:nV { #1 } \l_tmpa_tl
}{}
\cs_new_protected:Nn \zalessandro_bytefield:nn
{
\begin{bytefield}[#1]#2\end{bytefield}
}
\cs_generate_variant:Nn \zalessandro_bytefield:nn { nV }
\ExplSyntaxOff
\begin{document}
\begin{bytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{bytefield}
\begin{figure}
\begin{msc}[instance distance=5cm]{}
\declinst{alice}{Alice}{}
\declinst{bob}{Bob}{}
\nextlevel[5]
\mess{%
\begin{sbytefield}[bitwidth=1.5em, bitheight=1.5em]{4}
\bitbox{1}{0} & \bitbox{1}{1} & \bitbox{1}[]{\dots} & \bitbox{1}{0}
\end{sbytefield}%
}{alice}{bob}
\nextlevel[5]
\end{msc}
\end{figure}
\end{document}
诀窍在于它的代码中bytefield
包含&
类别代码 10,这就是正则表达式替换所做的。