Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Welcome to TeX.sx! –  Qrrbrbirlbel Feb 25 '13 at 13:32
add comment

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
    
Works perfectly thank you! –  user26467 Feb 25 '13 at 14:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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