1

consider the following image (taken from here):

enter image description here

I want to adapt this image as TikZ picture. I would represent each rectangular node of the tree as a TikZ node. Each node has two character-wise aligned strings as labels. Is there a clever way, for instance using the matrix library of TikZ, to align text of node labels character-wise, like it was done in the picture?

I have looked at some examples, but I have not found a similar situation yet.

Regards

  • 1
    Welcome to TeX.SX! Why not just use a mono spaced font, e.g. \node[align=left,font=\ttfamily]{abcde\\12345};? Do you have the code for the tree, without the "custom" styled nodes? If so, can you add that to your question? – Torbjørn T. Nov 5 '15 at 17:20
  • Thanks for the suggestion. Actually, I do not have the code of the tree. Using monospace font would be a perfect solution. However, i just wondered whether there is a more general way to make matrix-like labels, e.g. if you have sophisticated demands wrt spacing, cell alignment, separating lines etc. – lkzim Nov 5 '15 at 18:04
  • By the way, do you want to use a special tree-package, like tikz-qtree or forest, or is plain TikZ fine? – Torbjørn T. Nov 5 '15 at 20:52
1

For your example, nodes with a monospaced font is sufficient, so you can use something like node[draw,align=center,font=\ttfamily]{imimi\\23451}.

That said, a matrix is just a special type of node, so you can actually use node[matrix] {...} in TikZ tree structure. See example below.

I also added a simple example of a more manual way of setting things up, placing several \matrix constructions relative to each other.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}
\node [draw] {start} 
child {node[draw,align=center,font=\ttfamily]{imimi\\23451}}
child {node[draw,align=center,font=\ttfamily]{abaae\\10011}};
\end{tikzpicture}

\bigskip

\begin{tikzpicture}[every matrix/.append style={ampersand replacement=\&,matrix of nodes}]
\node [matrix,draw] {a \& b \\ c \& d \\} 
child {node[matrix,draw] {a \& b \\ c \& d \\}}
child {node[matrix,draw] {a \& b \\ c \& d \\}};
\end{tikzpicture}

\bigskip


\begin{tikzpicture}[every node/.append style={on grid,node distance=1.5cm and 1cm},
                    every matrix/.append style={matrix of nodes,draw}]
\matrix (m1) {1&2\\b&c\\};
\matrix [below left=of m1] (m2) {1&2\\b&c\\};
\matrix [below right=of m1] (m3) {1&2\\b&c\\};

\draw (m1) edge (m2)
           edge (m3);
\end{tikzpicture}
\end{document}

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.