[Tex/LaTex] Implementing a way to show data structures in LaTeX

automationpackage-writing

Is there a way to implement data structures in LaTeX?

I am currently taking two classes that require large numbers of heaps (min,max) and queues (FIFO,LIFO) and the like to be drawn after inserting elements.

I would like there to be a way for me to implement something that would all me to pass in the number to add the the structure, and then draw it or something?

Currently, I have little programs written in C++ that take in the inputs, and then spit out the correct latex code (using some fairly horrible syntax), which I then copy and paste, but this is annoying (though far better than drawing everything manually).

what I have now:
input: 4 ci5 ci6 ci7 cp cr cp
output:

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

Is there a way to do this in purely in LaTeX?

Best Answer

This is a job for tikz:

enter image description here

Notes:

Code:

\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}
Related Question