2

I'm trying to add a bytefield inside an msc diagram. Here's a 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}

Actually, this MWE gives an error: Extra alignment tab has been changed to \cr. I suppose this is due to msc, but I'm unsure how to solve it, and it may very well be the root of the problem. I've tried wrapping bytefield with braces or using parbox, but that didn't solve either.

The result is the following: the bytefield inside the diagram is on two lines instead of one. enter image description here

I'd really appreciated some help on this. Thanks!

1 Answer 1

2

The bytefield environment doesn't like to be processed as the argument to another command. Here's a workaround:

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

enter image description here

Alternatively, define sbytefield (safe bytefield) to be used as argument to a command.

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

The trick is that bytefield wants & to have category code 10 in its code and that's what the regex replacement does.

2
  • I was typing the exact same thing up... Too slow, you beat me by 35 seconds :(
    – Skillmon
    Commented Sep 8, 2023 at 10:09
  • @Skillmon On the other hand, since msc is based on TikZ, the bytefield might be emulated with a pic, perhaps.
    – egreg
    Commented Sep 8, 2023 at 10:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .