2

I have to draw lots of bytefield with different number of bitbox with different shapes.

For that I define 3 functions:

  1. mybytefield that takes a variable number of arguments (using xparse with \SplitList and \ProcessList) and defines the bytefield environment

  2. bitboxlist that takes args in a sequential way and sends them to mybitbox

  3. mybitbox that takes the 2 arguments needed for \bitbox in a sequence and invokes \bitbox{\value{{bitfieldsize}}{#1} when the 2 arguments are ready.

It almost works ... I can invoke \mybytefield with different numbers of args and it draws several bytefield BUT the position of the bitbox are wrong or "corrupted" by the \ProcessListcall.

Any idea ?

Here is a sample code :

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

You have spurious spaces in your code (see reference 1).

I have added the needed % at the end of lines where spaces had been included.

You may also change the center environment to \centering because the former introduces additional vertical spacing

Reference

  1. What is the use of percent signs (%) at the end of lines?
  2. When should we use \begin{center} instead of \centering?

Code

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

Output

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.