# Using bytefield as parameter to self-defined macro raises error

I defined a simple LaTeX macro \bansen which takes a single argument:

\newcommand{\bansen}[1]{#1}


Unfortunately, I noticed that I can not use a bytefield environment as a parameter for this macro as in

\documentclass{article}
\usepackage{bytefield}
\newcommand{\bansen}[1]{#1}    \usepackage{bytefield}

\begin{document}
\bansen{
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field}\\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits}
\end{bytefield}
}
\end{document}


because that would create an error at the macro's closing bracket }:

! Extra alignment tab has been changed to \cr.
<template> \endtemplate

l.11 }


which was apparently introduced by the & character -- replacing that line by

        \bitbox{8}{8 bits} % & \bitbox{8}{8 more bits}


lets the error vanish together with the second column of that line.

Can anybody tell me what is going wrong and how to fix this?

Motivation: My real macro \bansen contains several other typesetting commands and several more parameters. The LaTeX code which uses that macro is computer-generated which allows me to keep content (generator) and form (LaTeX style) separated, so I am not too fond of the idea to not use a macro.

• Did you try putting the argument in a group? ie \newcommand{\bansen}[1]{{#1}} or possibly using a makebox around the argument? – Thruston May 27 '15 at 16:20
• Ignore that previous comment, but just get rid of the & between the two bitboxes – Thruston May 27 '15 at 16:23

For its internal purposes, bytefield changes the category code of & upon starting the environment. This change cannot affect the argument already absorbed by \bansen, so you have to do the category change beforehand:

\documentclass{article}
\usepackage{bytefield}

\newcommand{\bansen}{%
\begingroup\catcode&=10
\banseninternal}
\newcommand{\banseninternal}[1]{%
#1%
\endgroup
}

\begin{document}

\begin{bytefield}{16}
\wordbox{1}{A 16-bit field}\\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits}
\end{bytefield}

\bigskip

\bansen{%
\begin{bytefield}{16}
\wordbox{1}{A 16-bit field}\\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits}
\end{bytefield}%
}

\end{document}


The supplementary formatting instruction will go in \banseninternal instead of \bansen.

• As a side effect, this would mean that a tabular environment can never be used as a parameter for \bansen since the & character was temporarily reclassified as whitespace. I can circumvent that and not use tabulars (by modifying the generator), but is there a solution to that problem? I thought about redeclaring environments but I do not see if this could help. – Twonky May 27 '15 at 17:47
• @Twonky No, you can't use & in the argument to \bansen, but you can do \let\AMP=& and use \AMP for &`. A character can't have two category codes at the same time. – egreg May 27 '15 at 17:58