LaTeX에서 데이터 구조를 표시하는 방법 구현

LaTeX에서 데이터 구조를 표시하는 방법 구현

LaTeX에서 데이터 구조를 구현하는 방법이 있나요?

나는 현재 많은 수의 힙(최소, 최대)과 대기열(FIFO, LIFO) 등을 요소 삽입 후 그려야 하는 두 가지 클래스를 수강하고 있습니다.

구조를 추가하기 위해 숫자를 전달한 다음 그리는 등의 작업을 구현할 수 있는 방법이 있었으면 좋겠습니다.

현재 C++로 작성된 작은 프로그램이 입력을 받아 올바른 라텍스 코드(상당히 끔찍한 구문 사용)를 뱉어낸 다음 복사하여 붙여넣지만 이것이 짜증스럽습니다(모든 것을 수동으로 그리는 것보다 훨씬 낫습니다). ).

내가 지금 가지고 있는 것:
입력:4ci5ci6ci7cp crcp
산출:

\[\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}

컴파일 후에는 다음과 같습니다:

바이트필드가 있는 큰 블록

더 복잡한 구조의 경우 다음 예와 같이 이러한 빌딩 블록을 사용하면 됩니다(재미를 위해 예제를 환경 bytefield에 래핑 figure하고 캡션을 추가했습니다).

\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. 귀하의 질문에 사용한 구문을 어느 정도 고수해야 하는지는 모르겠지만 아마도 이러한 예가 귀하의 취향을 조사했으며 전환을 위한 노력은 가치가 있을 것입니다.

관련 정보