0

I'm making documentation for data communication protocol and trying to make a table column which shows an offset of the message fields, calculated for each field (table row) and takes field width instead. For offset calculation I made a counter, which keeps current offset, and define new command:

\newcounter{bytepos}
\newcommand{\fieldwidth}[1]{%
\ifnumcomp{#1}{=}{1}%
{\thebytepos\stepcounter{bytepos}}
{\thebytepos--\the\numexpr\value{bytepos} + #1 - 1\relax\addtocounter{bytepos}{#1}}%
}

I also define new column type, which should call my command for every item in the column:

\newsavebox{\byteposbox}
\newcolumntype{B}{>{\begin{lrbox}{\byteposbox}}%
l%
<{\end{lrbox}%
\fieldwidth{\usebox{\byteposbox}}%
}}

For populating table I use next sample code:

\begin{tabular}{Bl}
  2 & Start mark   \\
  8 & Message data \\
  4 & Service info \\
  1 & Checksum     \\
  1 & End mark
 \end{tabular}

It should be compiled as like as

\begin{tabular}{ll}
   0-- 1 & Start mark   \\
   2-- 9 & Message data \\
  10--13 & Service info \\
  14     & Checksum     \\
  15     & End mark
\end{tabular}

But I gave “missing number, treated as zero” instead. After searching in network I found that numbers in LaTeX is something special, and it's looks like \usebox returns complete and glued box, not a text, which cannot be converted back to raw text.

How can I get raw text from every row? Or maybe my understanding is wrong and I missing something simple and important?

1

It is easier to manipulate count registers than boxes

enter image description here

\documentclass{article}

\usepackage{array}
\newcount\zzthis
\newcount\zztotal
\newcommand\zzrange{%
\the\zztotal
\global\zztotal=\numexpr\zztotal+\zzthis-1\relax
\ifnum\zzthis>1--\the\zztotal\fi
\global\advance\zztotal1
}
\newcolumntype{B}{>{\let\ignorespaces\empty\afterassignment\zzrange\zzthis=}l}
\begin{document}

\begin{tabular}{Bl}
  2 & Start mark   \\
  8 & Message data \\
  4 & Service info \\
  1 & Checksum     \\
  1 & End mark
 \end{tabular}

\end{document}
| improve this answer | |

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.