bytefield doesn't work with subfigures

I'm trying to use bytefield and subfig (and also tested with subfigure with the same results) but I got the error:

! Extra alignment tab has been changed

to \cr. \endtemplate

at the end of the subfloat environment.

The code below reproduces the problem

\documentclass{article}
\usepackage{bytefield}
\usepackage{subfig}

\begin{document}
\pagestyle{empty}
% this works
\begin{figure}
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}
\caption{Some caption}
\end{figure}

% this doesn't
\begin{figure}
\subfloat[]{
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}
}% the error is trown here
\subfloat[]{
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}
}% and here
\caption{Some caption}
\end{figure}

\end{document}


1 Answer

The bytefield environment doesn't like to be in the argument to a command, in this case \subfloat.

You can remedy by using a save bin and the environment lrbox:

\documentclass{article}
\usepackage{bytefield}
\usepackage{subfig}
\newsavebox{\bytefieldbox}

\begin{document}
\pagestyle{empty}

\begin{figure}
\centering

\begin{lrbox}{\bytefieldbox}
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}
\end{lrbox}
\subfloat[]{\usebox{\bytefieldbox}}
%%%
\begin{lrbox}{\bytefieldbox}
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}
\end{lrbox}
\subfloat[]{\usebox{\bytefieldbox}}

\caption{Some caption}
\end{figure}

\end{document}


• Why lrbox worked, while using \sbox did not? Is somehow different in comparison to \sbox? – adn Jan 8 '14 at 11:49
• @adn \sbox suffers from the same problem, because the text is absorbed as an argument, and bytefield doesn't like it. – egreg Jan 8 '14 at 11:50
• So, internally the environments work somehow differently? What is inside is not treated like an argument? – adn Jan 8 '14 at 12:05
• @adn In general environments are not considered as arguments to a command (notice that this is instead the case for the alignments in amsmath). The purpose of lrbox is exactly not absorbing the text as an argument, but packaging it in a box nonetheless. It uses some really slick tricks. – egreg Jan 8 '14 at 12:08