4

This code

\documentclass{article}
\usepackage{bytefield}

\begin{document}
\begin{bytefield}[leftcurly=.,leftcurlyspace=0pt]{32}
  \begin{leftwordgroup}{0}
    \wordbox{1}{\the\height}
  \end{leftwordgroup} \\
  \begin{leftwordgroup}{\raisebox{\totalheight}4}
    \wordbox{2}{\the\height}
  \end{leftwordgroup} \\
  \begin{leftwordgroup}{\raisebox{6ex}{12}}
    \wordbox{2}{\the\height}
  \end{leftwordgroup}
\end{bytefield}
\end{document}

results in

enter image description here

I'd like to get the 0 and the 4 left word groups to be vertically aligned with the top of their corresponding wordboxes. However, the only way I've found to do that is to trial and error it with \raisebox{6ex}. \height and \totalheight seem to refer to the height of the text in the group. leftwordgroup doesn't make \height available the same way wordbox and bitbox does.

Is there a more precise way to specify the vertical alignment of the left wordgroup?

Update

Thanks for taking the time to help me out with this.

When this was applied to a more sophisticated use of bytefield, it's a bit off.

\documentclass{standalone}
\usepackage{bytefield}
\usepackage{stackengine}
\newcommand\topwg[2]{%
  \makebox[3ex][l]{\makebox[2ex][r]{\tiny{#1}}}\belowbaseline[-.3\ht\strutbox]{#2}
}

\begin{document}
\begin{bytefield}[leftcurly=.,leftcurlyspace=0pt]{32}
  \bitheader{0,8,16,24} \\
  \topwg{0}{\begin{leftwordgroup}{}
      \begin{rightwordgroup}{Standard Header}
        \wordbox{1}{\the\height}
      \end{rightwordgroup}
  \end{leftwordgroup}} \\
  \topwg{4}{\begin{leftwordgroup}{}
    \wordbox{2}{\the\height}
  \end{leftwordgroup}} \\
  \topwg{12}{\begin{leftwordgroup}{}
    \wordbox{2}{\the\height}
  \end{leftwordgroup}}
\end{bytefield}
\end{document}

enter image description here

Again, thanks a lot for your help.

  • Joe, you needed to leave a comment on my answer in order for me to know that you had seen my answer. Nonetheless, I've seen your update now. But I don't understand what's a bit "off". Could you be more specific? You can comment on your own question, but if you want me to be notified, you have to include the phrase "@Steven B. Segletes" somewhere in your comment. – Steven B. Segletes Jan 15 '14 at 19:29
  • @Steven B. Segletes: thanks for the advice on commenting. Referring to the second image in the question, the "Standard Header" right word group is a bit high, and there are gaps between the word boxes, when the -.3 modifier is specified. I worked out how to keep the \bitheader from being shifted to the left. – Joe Doyle Jan 15 '14 at 20:28
  • Please see my revised answer. – Steven B. Segletes Jan 16 '14 at 1:12
3

Here, I introduce \topwg macro that takes two arguments. The first is the label that would otherwise go as the first argument to leftwordgroup, and the second is the leftwordgroup environment with a blank first argument.

What this does is vertically shift the word group so that, instead of having its baseline aligned with the central text, it instead places the top of the wordgroup box at the height of a \strutbox.

EDITED to address some deficiencies noted by the OP. I believe the inter-box gap was caused by some glue introduced by boxes that were located at fractional baseline height. Thus, I restored the \ht\strutbox shift of \belowbaseline and tuned the left-label relative to that with a \raisebox. This also corrected the problem with the vertical position of the right-hand label. Additionally, I added a small negative \vspace at the end of \topwg to avoid a double-thick rule between boxes.

\documentclass{article}
\usepackage{bytefield}
\usepackage{stackengine}
\newcommand\topwg[2]{%
  \raisebox{.7\ht\strutbox}{\makebox[3ex][l]{\makebox[2ex][r]{%
  \tiny{#1}}}}\belowbaseline[-\ht\strutbox]{#2}\vspace{-.65pt}%
}
%\renewcommand\topwg[2]{#2}
\begin{document}
\begin{bytefield}[leftcurly=.,leftcurlyspace=0pt]{32}
  \bitheader{0,8,16,24} \\
  \topwg{0}{\begin{leftwordgroup}{}
      \begin{rightwordgroup}{Standard Header}
        \wordbox{1}{\the\height}
      \end{rightwordgroup}
  \end{leftwordgroup}} \\
  \topwg{4}{\begin{leftwordgroup}{}
    \wordbox{2}{\the\height}
  \end{leftwordgroup}} \\
  \topwg{12}{\begin{leftwordgroup}{}
    \wordbox{2}{\the\height}
  \end{leftwordgroup}}
\end{bytefield}
\end{document}

enter image description here

If you plan on having more than two digits in the label to the left of the box, you can expand the width of the two \makeboxes defined in \topwg. The first number (3ex) determines the width of the labels on the left while the second number (2ex) specifies the left padding for those labels.

To change the height of the left-label, change the amount of the \raisebox at the beginning of \topwg.

I also changed the \documentclass to article rather than standalone to prevent the bottom rule from being clipped (I'm not sure why).

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.