實作在 LaTeX 中顯示資料結構的方法

實作在 LaTeX 中顯示資料結構的方法

有沒有辦法在 LaTeX 中實作資料結構?

我目前正在學習兩個類,它們需要在插入元素後繪製大量堆(最小,最大)和隊列(先進先出,後進先出)等。

我希望有一種方法可以讓我實現一些東西,讓我可以傳入數字來添加結構,然後繪製它或其他東西?

目前,我有一些用C++ 編寫的小程序,它們接受輸入,然後吐出正確的乳膠代碼(使用一些相當可怕的語法),然後我複製並粘貼這些代碼,但這很煩人(儘管比手動繪製所有內容好得多) )。

我現在有什麼:
輸入:4 ci5 ci6 ci7 cp cr cp
輸出:

\[\begin{array}{|c|c|c|c|}
    \hline
    5 &6 &7 & \\
    \hline
\end{array}\]
\[\begin{array}{|c|c|c|c|}
    \hline
    6 &7 & & \\
    \hline
\end{array}\]

有沒有辦法純粹用 LaTeX 來做到這一點?

答案1

這是一份工作tikz

在此輸入影像描述

筆記:

代碼:

\documentclass{article}
\usepackage{tikz}
\usepackage{xstring}
\usetikzlibrary{calc}

\newcommand*{\NodeSize}{0.5cm}%
\tikzset{My Style/.style={minimum size=0.5cm, draw=gray, line width=1pt}}{}

\newcommand*{\DrawNodes}[2][fill=none]{%
    % https://tex.stackexchange.com/questions/12091/tikz-foreach-loop-with-macro-defined-list
    \def\Sequence{#2}
    \foreach [count=\xi] \Label in \Sequence {%
        \pgfmathsetmacro{\XShift}{\NodeSize*\xi}
        \node [My Style, xshift=\XShift, #1] (\Label) {\Label};
    } 
}

\begin{document}
\begin{tikzpicture}
    \DrawNodes[fill=red!20]{5,6,7,}
    \DrawNodes[yshift=-1.0cm, fill=gray!15]{6,7,,,}
\end{tikzpicture}
\end{document}

答案2

我的建議是看一下bytefield包裹。如果您有很多圖表要表示並且不太熟悉TikZ,那麼這可能是您的最佳選擇。

為了演示它,以下是包文檔中的一些簡短示例:

\begin{bytefield}{32}
\bitheader{0-31} \\
\bitbox{4}{Four} & \bitbox{8}{Eight} &
\bitbox{16}{Sixteen} & \bitbox{4}{Four}
\end{bytefield}

將產生:

簡單位元組字段範例

當然,你可以省略\bitheader{0-31},這樣bis就不會被編號了。另外,如果您想要一個跨越“列”(或與此相關的單字)整個寬度的框,您可以使用宏\wordbox,如下所示:

\begin{bytefield}{16}
\wordbox{1}{A 16-bit field} \\
\bitbox{8}{8 bits} & \bitbox{8}{8 more bits} \\
\wordbox{2}{A 32-bit field. Note that text wraps within the box.}
\end{bytefield}

結果是:

帶單字的位元組字段範例

或者,您甚至可以以方便的方式表示長資料塊:

\begin{bytefield}{16}
\wordbox{1}{Some data} \\
\wordbox[lrt]{1}{Lots of data} \\
\skippedwords \\
\wordbox[lrb]{1}{} \\
\wordbox{2}{More data}
\end{bytefield}

編譯後如下圖所示:

帶有位元組字段的大塊

對於更複雜的結構,您只需使用這些構建塊即可,如下例所示(只是為了好玩,我將示例包裝bytefieldfigure環境中並添加了標題):

\documentclass[a4paper]{article}

\usepackage{bytefield}

\begin{document}

RTP packetization of an MPEG-4 Visual bitstream according to the Internet Engineering Task Force's Request for Comments (RFC) number 3016:

\begin{figure}[hb!]
\centering
\begin{bytefield}[bitwidth=1.1em]{32}
\bitheader{0-31} \\
\begin{rightwordgroup}{RTP \\ Header}
\bitbox{2}{V=2} & \bitbox{1}{P} & \bitbox{1}{X}
& \bitbox{4}{CC} & \bitbox{1}{M} & \bitbox{7}{PT}
& \bitbox{16}{sequence number} \\
\bitbox{32}{timestamp}
\end{rightwordgroup} \\
\bitbox{32}{synchronization source (SSRC) identifier} \\
\wordbox[tlr]{1}{contributing source (CSRC) identifiers} \\
\wordbox[blr]{1}{$\cdots$} \\
\begin{rightwordgroup}{RTP \\ Payload}
\wordbox[tlr]{3}{MPEG-4 Visual stream (byte aligned)} \\
\bitbox[blr]{16}{}
& \bitbox{16}{\dots\emph{optional} RTP padding}
\end{rightwordgroup}
\end{bytefield}
\caption{MPEG-4 RTP package}
\end{figure}

\end{document}

及其結果:

具有位元組字段的 MPEG-4 範例

這些是一些與 相關的例子bytefield。我不知道您需要在多大程度上堅持您在問題中使用的語法,但也許這些示例已經滿足了您的胃口,並且所做的轉變是值得付出努力的。

相關內容