Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
3  
So what you're saying is that you want some function (lets call it a macro) in LaTeX that you supply with input (for example 4 ci5 ci6 ci7 cp cr cp) and it should produce the output as in your code? If so, you need to tell us more about what the input components mean. For example, 4 means 4 columns; ci5 means a centred column with number 5. What is the i, cr and cp for? Put yourself in the communities shoes when you're asking questions. –  Werner Apr 12 '13 at 4:06
    
@Werner Exactly it. Ideally, there is already something can do more structures. Additionally, if there was a way to create a macro that would call my code, and then paste in the result, that would also be helpful –  soandos Apr 12 '13 at 4:49
    
Something like this? –  Marc van Dongen Apr 12 '13 at 12:13
add comment

2 Answers

up vote 7 down vote accepted

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]{%
    % http://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}
share|improve this answer
    
It works, but there is something wierd with the picture snag.gy/etmZr.jpg –  soandos Apr 12 '13 at 4:50
    
@soandos: Without being able to reproduce the problem it is difficult to figure out what went wrong for you. Are you using the latest libraries? You could try increasing the minimum size as it might be a font issue. If that does not work, I'd post a new question and reference this so everyone knows who to blame. :-) Oh, and make sure to include a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem. –  Peter Grill Apr 12 '13 at 4:56
    
I just used your MWE, and I just ran MiKTex update. That fixed the problem –  soandos Apr 12 '13 at 5:07
    
Would you mind explaining the yshift does, or it just moves it down? –  soandos Apr 12 '13 at 5:20
    
The yshift is used to placement of subsequent node relative to the first one. The bigger problem in this kind of situation is to figure out exactly how you want to specify the input (which is what @Werner asked). To me it is not clear how you want to be able to specify what goes below what, so I left it in the simplest manner as possible where you specify it on every line. If you look at the \ThreeDNASequences macro in the linked question it automatically shifts subsequent invocations further down. You should be able to adapt that for this case if it is desired. –  Peter Grill Apr 12 '13 at 5:46
show 1 more comment

My suggestion would be to take a look at the bytefield package. If you have many diagrams to represent and are less familiar with TikZ, this might be the way to go.

To demonstrate it, here are a few short examples from the package documentation:

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

will produce:

simple bytefield example

Of course, you can omit \bitheader{0-31} and then the bis will not be numbered. Also, if you want a box that spans the whole width of your 'column' (or word for that matter), you can use the \wordbox macro, as below:

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

which results in:

bytefield example with words

Or, you can even represent long data blocks in a convenient manner:

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

which after compilation looks like this:

big blocks with bytefield

For more complex structures you just play with these building blocks, like in the following example (just for the fun of it I wrapped the bytefield example into a figure environment and added a caption):

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

and the result of it:

MPEG-4 example with bytefield

So these are some relevant examples with bytefield. I don't know to what extent you need to stick to the syntax you used in your question, but maybe these examples have vetted your appetite and the transition to make is worth the effort.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.