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.

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

share|improve this question
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
add comment

1 Answer

up vote 9 down vote accepted

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}
share|improve this answer
    
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
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.