# adding a source to bytefield

below you see my minimum example. Take a look at the second figure with the \includegraphics. I could add a field for citations as shown below with a tabular inside the figure. This works pretty well. What I want to do now is to add the same field for citations to the bytefield, but it doesn't work. I also tried it with a \makebox, but this also lead to no sulition. Has someone an idea?

\documentclass{article}
\usepackage{bytefield}
\usepackage{graphicx}
\newsavebox{\bytefieldbox}

\begin{document}
\pagestyle{empty}

\begin{figure}[]
\centering
\begin{bytefield}[bitwidth=1em]{32}
\bitbox{4}{Version} & \bitbox{4}{IHL} & \bitbox{8}{Type of Service} & \bitbox{16}{Total Length} \\
\bitbox{16}{Identification} & \bitbox{3}{Flags} & \bitbox{13}{Fragment Offset} \\
\bitbox{8}{\emph{Time to Live}} & \bitbox{8}{Protocol} & \bitbox{16}{Header Checksum} \\
\bitbox{24}{Options} & \bitbox{8}{Padding (if any)} \\
\end{bytefield}
\caption{some caption}
\end{figure}

\begin{figure}
\centering
\begin{tabular}{@{}r@{}}
\includegraphics[scale=0.5]{example-image-a}\\

{\footnotesize Source: based on ...}
\end{tabular}
\caption{some caption}
\end{figure}

\end{document}


Thank you

Robert

Apparently, bytefield doesn't like to be in tabular or in \sbox. I suggest a low level trick:

\documentclass{article}
\usepackage{bytefield}
\usepackage{graphicx}

\newsavebox{\bytefieldbox}
\newenvironment{sourcedbytefield}[3][]
{\def\source{#3}\setbox\bytefieldbox=\hbox\bgroup\begin{bytefield}[#1]{#2}}
{\end{bytefield}\egroup
\begin{tabular}{@{}r@{}}
\usebox{\bytefieldbox}\\
\footnotesize\source
\end{tabular}}
\newcommand{\sourcedincludegraphics}[3][]{%
\begin{tabular}{@{}r@{}}
\includegraphics[#1]{#2}\\
\footnotesize#3
\end{tabular}%
}

\begin{document}
\pagestyle{empty}

\begin{figure}[htp]
\centering

\begin{sourcedbytefield}[bitwidth=1em]{32}{Source: based on ...}
\bitbox{4}{Version} & \bitbox{4}{IHL} & \bitbox{8}{Type of Service} & \bitbox{16}{Total Length} \\
\bitbox{16}{Identification} & \bitbox{3}{Flags} & \bitbox{13}{Fragment Offset} \\
\bitbox{8}{\emph{Time to Live}} & \bitbox{8}{Protocol} & \bitbox{16}{Header Checksum} \\
\bitbox{24}{Options} & \bitbox{8}{Padding (if any)}
\end{sourcedbytefield}
\caption{some caption}
\end{figure}

\begin{figure}[htp]
\centering
\sourcedincludegraphics[scale=0.5]{example-image-a}{Source: based on ...}
\caption{some caption}
\end{figure}

\end{document}


Note that there shouldn't be a trailing \\ in the bytefield environment (remove \\ from the last line).

• this is great, thank you! what I like to have now is to move the "source: based on ..." a little more to the left. I tried it with \hspace, but this didn't work. – Robert Aug 30 '18 at 15:04
• @Robert Add something like \hspace*{1em} before the closing brace in the definition. – egreg Aug 30 '18 at 17:00
• sorry for the late reply. \hspace*{1em} works fine. I tried it before without the asterisk and it didn't work. Could you please explain why you need an asterisk in this case? – Robert Sep 3 '18 at 9:10
• @Robert In tabular trailing spaces in a cell are removed (usually it is just a space token, but this removal can also affect other spaces). With \hspace* you ensure the space is never removed. – egreg Sep 3 '18 at 9:17

If you wrap your bytefield in a group {...}, it works fine

\documentclass{article}
\usepackage{bytefield}
\usepackage{graphicx}
\newsavebox{\bytefieldbox}

\begin{document}
\pagestyle{empty}

\begin{figure}[]
\centering
\begin{tabular}{@{}r@{}}
{\lineskip=1pt
\begin{bytefield}[bitwidth=1em]{32}
\bitbox{4}{Version} & \bitbox{4}{IHL} & \bitbox{8}{Type of Service} & \bitbox{16}{Total Length} \\
\bitbox{16}{Identification} & \bitbox{3}{Flags} & \bitbox{13}{Fragment Offset} \\
\bitbox{8}{\emph{Time to Live}} & \bitbox{8}{Protocol} & \bitbox{16}{Header Checksum} \\
\bitbox{24}{Options} & \bitbox{8}{Padding (if any)}
\end{bytefield}
}\\
{\footnotesize Source: based on ...}
\end{tabular}
\caption{some caption}
\end{figure}

\begin{figure}
\centering
\begin{tabular}{@{}r@{}}
\includegraphics[scale=0.5]{example-image-a}\\

{\footnotesize Source: based on ...}
\end{tabular}
\caption{some caption}
\end{figure}

\end{document}


• Hmm, the lines are doubled. – egreg Aug 30 '18 at 9:36
• @egreg Indeed. It seems to be possible to get the correct spacing by removing the \\  before \end{bytefield} – BambOo Aug 30 '18 at 10:12
• I was mentioning the rules in the body of bytefield. You can cure them by adding \lineskip=1pt after the initial brace that starts the group where bytefield is in. – egreg Aug 30 '18 at 10:20
• @egreg My bad, I did not notice it, its is due to the combination with the tabular – BambOo Aug 30 '18 at 10:34
• @BambOo This looks like I had it once before, but I wasn't able to get to this result once again. What i don't like in this case is, that the distande of the source is really far away from the bytefield. – Robert Aug 30 '18 at 15:04