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

    5 &6 &7 & \\
    6 &7 & & \\

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

  • 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, 2013 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, 2013 at 4:49
  • Something like this?
    – user10274
    Apr 12, 2013 at 12:13

2 Answers 2


This is a job for tikz:

enter image description here




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

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

    \DrawNodes[yshift=-1.0cm, fill=gray!15]{6,7,,,}
  • It works, but there is something wierd with the picture snag.gy/etmZr.jpg
    – soandos
    Apr 12, 2013 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. Apr 12, 2013 at 4:56
  • I just used your MWE, and I just ran MiKTex update. That fixed the problem
    – soandos
    Apr 12, 2013 at 5:07
  • Would you mind explaining the yshift does, or it just moves it down?
    – soandos
    Apr 12, 2013 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. Apr 12, 2013 at 5:46

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:

\bitheader{0-31} \\
\bitbox{4}{Four} & \bitbox{8}{Eight} &
\bitbox{16}{Sixteen} & \bitbox{4}{Four}

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:

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

which results in:

bytefield example with words

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

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

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):




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

\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} \\
\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{16}{\dots\emph{optional} RTP padding}
\caption{MPEG-4 RTP package}


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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