尝试限制宽度时使用 bytefield 包发生算术溢出错误

尝试限制宽度时使用 bytefield 包发生算术溢出错误

我正在尝试起草一份大量使用该bytefield软件包的文档,但遇到了这样的情况:对于必须为 64 位宽的字段,似乎没有好的解决方案(甚至文档也没有显示此类示例!)。事不宜迟,下面是结果:

字节字段宽度问题

如您所见,64 位值的宽度超出了页面范围。我尝试使用可选的bitwidth文档中所述的可选参数来限制这一点(例如以下这个答案的问题提供一个论点0.8\textwidth,从那里进行调整,但这样做会产生“算术溢出”错误。有人知道如何限制整个领域本身,或者至少强制bytefield产生一个不能比页面本身更宽的字段?

以下是最小工作示例的代码:

\documentclass[10pt]{extreport}
\usepackage[margin=2.0cm,a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{bytefield}

\begin{document}
    \noindent
    An \texttt{unsigned word} is defined as a 16-bit or two-octet quantity, having the range \texttt{[0, 65535]}: \\

    \begin{center}
        \begin{bytefield}[endianness=big]{16}
            \bitheader{15, 7, 0}                \\
            \bitbox{16}{\texttt{unsigned word}} \\
        \end{bytefield}
    \end{center}

    An \texttt{unsigned word} is addressable as an 2-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the word (the \textit{high byte}) and \texttt{byte[1]} corresponds to the least-significant byte in the word (the \textit{low byte}). \\

    \vspace{5pt}
    \hrule
    \vspace{15pt}

    \noindent
    An \texttt{unsigned doubleword} is defined as a 32-bit or four-octet quantity, having the range \texttt{[0, 4294967295]}: \\

    \begin{center}
        \begin{bytefield}[endianness=big]{32}
            \bitheader{31, 23, 15, 7, 0}                \\
            \bitbox{32}{\texttt{unsigned doubleword}}   \\
        \end{bytefield}
    \end{center}

    An \texttt{unsigned doubleword} is addressable as an 4-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the doubleword (the \textit{high byte}) and \texttt{byte[3]} corresponds to the least-significant byte in the doubleword (the \textit{low byte}). \\

    \vspace{5pt}
    \hrule
    \vspace{15pt}

    \noindent
    An \texttt{unsigned quadword} is defined as a 64-bit or eight-octet quantity, having the range \texttt{[0, 18446744073709551615]}: \\

    \begin{center}
        \begin{bytefield}[endianness=big]{64}
            \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
            \bitbox{64}{\texttt{unsigned quadword}}         \\
        \end{bytefield}
    \end{center}

    An \texttt{unsigned quadword} is addressable as an 8-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the quadword (the \textit{high byte}) and \texttt{byte[7]} corresponds to the least-significant byte in the quadword (the \textit{low byte}). \\
\end{document}

产生错误的非工作代码如下 - 将其包裹在花括号中并不能缓解错误:

\begin{center}
    \begin{bytefield}[endianness=big, bitwidth=0.8\textwidth]{64}
        \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
        \bitbox{64}{\texttt{unsigned quadword}}         \\
    \end{bytefield}
\end{center}

另外,忠告 - 我仍然(慢慢地)掌握 TeX 的诀窍,所以我的自助技能仍然局限于“在线搜索解决方案,希望有人已经实现了它”;你可能需要帮我把潜在的答案分解一下。

如果有帮助的话,所讨论的环境是 MacTex 2015(TeXLive-2015);代码是使用 TeXstudio 2.10.4 构建的。

答案1

您可以尝试\resizebox使用包graphicx

\documentclass[10pt]{extreport}
\usepackage[margin=2.0cm,a4paper]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{bytefield}
\usepackage{graphicx}
\begin{document}
    \noindent
    An \texttt{unsigned word} is defined as a 16-bit or two-octet quantity, having the range \texttt{[0, 65535]}: \\

    \begin{center}
        \begin{bytefield}[endianness=big]{16}
            \bitheader{15, 7, 0}                \\
            \bitbox{16}{\texttt{unsigned word}} \\
        \end{bytefield}
    \end{center}

    An \texttt{unsigned word} is addressable as an 2-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the word (the \textit{high byte}) and \texttt{byte[1]} corresponds to the least-significant byte in the word (the \textit{low byte}). \\

    \vspace{5pt}
    \hrule
    \vspace{15pt}

    \noindent
    An \texttt{unsigned doubleword} is defined as a 32-bit or four-octet quantity, having the range \texttt{[0, 4294967295]}: \\

    \begin{center}
        \begin{bytefield}[endianness=big]{32}
            \bitheader{31, 23, 15, 7, 0}                \\
            \bitbox{32}{\texttt{unsigned doubleword}}   \\
        \end{bytefield}
    \end{center}

    An \texttt{unsigned doubleword} is addressable as an 4-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the doubleword (the \textit{high byte}) and \texttt{byte[3]} corresponds to the least-significant byte in the doubleword (the \textit{low byte}). \\

    \vspace{5pt}
    \hrule
    \vspace{15pt}

    \noindent
    An \texttt{unsigned quadword} is defined as a 64-bit or eight-octet quantity, having the range \texttt{[0, 18446744073709551615]}: \\

    \begin{center}
        \resizebox{1.0\linewidth}{\height}{\begin{bytefield}[endianness=big]{64}
            \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
            \bitbox{64}{\texttt{unsigned quadword}}         \\
        \end{bytefield}}
    \end{center}

    An \texttt{unsigned quadword} is addressable as an 8-size array of bytes, where \texttt{byte[0]} corresponds to the most-significant byte in the quadword (the \textit{high byte}) and \texttt{byte[7]} corresponds to the least-significant byte in the quadword (the \textit{low byte}). \\
\end{document}

在此处输入图片描述

否则,保持相同的大小:

\begin{center}
    \centerline{\begin{bytefield}[endianness=big]{64}
        \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
        \bitbox{64}{\texttt{unsigned quadword}}         \\
    \end{bytefield}}
\end{center}

尽管\centerline不是最好的 LaTeX 命令。(但在这里还不错,因为它是其中唯一的东西center)。

在此处输入图片描述

更新:

由于字节字段框的深度有些令人惊讶,因此需要进行一些反复试验。另外,我不太清楚为什么我必须稍微减小一点宽度以避免垂直偏移。

\begin{center}
    \mbox{\resizebox{.9999\linewidth}{\height}{%
          \begin{bytefield}[endianness=big]{64}
              \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
              \bitbox{64}{}         \\
            \end{bytefield}}%
\kern-\linewidth
     \makebox[\linewidth]{\raisebox{4ex}{\texttt{unsigned quadword}}}%
          }
\end{center}

在此处输入图片描述

答案2

为一位保留的宽度必须乘以位数,因此像这样的数字

bitwidth=0.015\textwidth

或更少的选项更有可能起作用(选项showframe仅用于示例):

\documentclass[10pt]{extreport}
\usepackage[margin=2.0cm,a4paper,showframe]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{bytefield}

\begin{document}
An \texttt{unsigned quadword} is defined as a 64-bit or quantity, in the range
\texttt{[0, 18446744073709551615]}:
\begin{center}
\begin{bytefield}[endianness=big,bitwidth=0.015\textwidth]{64}
  \bitheader{63, 55, 47, 39, 31, 23, 15, 7, 0}    \\
  \bitbox{64}{\texttt{unsigned quadword}}         \\
  \end{bytefield}
\end{center}

\end{document}

在此处输入图片描述

相关内容