5

I have a table like the one below and want to write this in Latex. what would be the best aproach? I have tried it with Tikz where i can nest a Table but failed with the lines wich connect the different fields. Another solution would be to use a tikz tree, but there i don't know how to label the edges.

Is it possible to create something like this with an easy markup, either in TikZ or with any other latex package?

Huffman Coding

  • 2
    Look more like a diagram or complex tree structure than as a table with some arrows. Search in tree tags (tikz-trees, tikz-qtree ...) to see some examples. – Fran Aug 21 '12 at 21:49
9

Here is a beginning of a solution with tikz matrixenter image description here

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}


\begin{document}

\begin{tikzpicture}
[every node/.style={minimum height=2em}]
\matrix [matrix of math nodes](Mat)
{
x_i  &|[minimum width=4em]|& P(x_i) &|[minimum width=4em]| Code &       &|[minimum width=4em]|      &       &|[minimum width=4em]|  &   &|[minimum width=4em]|  &   &\\
x_1 &&0.30     &        & 0.30  &   &0.30   &   &0.45&  &0.55&\\
x_2 && 0.25    &        & 0.25  &   &0.25   &   &0.30&  &0.45&\\
x_3 && 0.20   &     & 0.20  &   &0.25   &   &0.25&  &   &\\
x_4 && 0.12    &        &0.13   &   &0.20   &   &   &   &   &\\
x_5 && 0.08    &        &0.12   &   &       &   &   &   &   &\\
x_6 && 0.05    &        &   &   &       &   &   &   &   &\\
};

\draw (Mat-2-3.east) -- (Mat-2-3-|Mat-2-5.west)node[above,midway]{00};
\draw(Mat-5-3.east) --++(1em,0) -- (Mat-6-5.west)node[above,pos=0.2]{101};
\draw (Mat-6-3.east)-|node[above,pos=0.4]{1000}++(2em,-1em) coordinate(aa) --++(1em,0) |- (Mat-5-5.west);
\draw (Mat-7-3.east)-|node[below,pos=0.4]{1001} (aa);
\end{tikzpicture}

\end{document}
  • very nice, didn't know about the possibility of "adressing" the matrix cells like any other named node. But in the first line with draw you don't need to specify the anchor east and west. The following compiles exactly the same: \draw (Mat-2-3) -- (Mat-2-5)node[above,midway]{00}; – jrast Aug 22 '12 at 9:33

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.