5

How can I draw the following in Latex? enter image description here

I have created the two tables, but I can't figure out how to draw the lines among the cells.

2 Answers2

4

It's possible to reproduce the whole composition with two TiKZ matrices:

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning}

\begin{document}
\begin{tikzpicture}[
mycell/.style={draw, minimum size=7mm, fill=gray!30},
matrixA/.style={matrix of nodes,
    nodes={mycell, anchor=center},
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth, 
    row 1/.style={nodes={draw=none, fill=none}},
    row 2/.style={font=\slshape}},
matrixB/.style={matrix of nodes,
    nodes={mycell, anchor=center},
    column sep=-\pgflinewidth, 
    row sep=-\pgflinewidth, 
    row 3/.style={nodes={draw=none, anchor=base, fill=none}, font=\slshape},
}]

\matrix[matrixA] (A) {
|[label=180:edge]| 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 21 & 21 & 22 & 23\\
|[label={[font=\rmfamily]180:destination}]| b & d & e & a & c & e & b & e & g & a & e & f & a & b & c & d & f & g & d & e & g & c & e & f \\
|[label=180:weight]| 4 & 12 & 9 &  4 & 8 & 7 & 8 & 5 & 2 & 12 & 3 & 6 & 9 & 7 & 5 & 3 & 1 & 13 & 6 & 1 & 11 & 2 & 13 & 11\\};

\matrix[matrixB, below=of A] (B) {
|[label=180:first edge]| 0 & 3 & 6 & 9 & 12 & 18 & 21\\
|[label=180:outdegree]| 3 & 3 & 3 & 3 & 6 & 3 & 3\\
|[label={[font=\rmfamily]180:vertex}]| a & b & c & d & e & f & g\\};

\foreach \i [count=\j] in {1, 4, 7, 10, 12, 18, 21} 
\draw (A-3-\i.south west)--(B-1-\j.north west);
\draw (A-3-23.south east)--(B-1-7.north east);

\end{tikzpicture}
\end{document}
Ignasi
  • 136,588
  • one more question. how can I resize the tikzpicture? I tried to do that with resizebox but it does not work. – Arshak Mehrabyan Jul 26 '16 at 12:04
  • @ArshakMehrabyan: If you want to include a tikzpicture (which includes matrix) inside a resizebox command you need to apply ampersand replacement option. See: this answer. A second solution would be to use standalone class to obtain a cropped figure and include it in your text with \includegraphics command. – Ignasi Jul 26 '16 at 12:23
  • thank you again @ignasi. I fixed it with ampersand replacement. – Arshak Mehrabyan Jul 26 '16 at 13:27
0

Here is a solution with nicematrix and Tikz.

\documentclass[landscape]{article}
\usepackage{geometry}
\geometry{left=1cm,right=1cm}
\usepackage{xcolor}
\usepackage{nicematrix,tikz}

\begin{document}

\begin{center} \begin{NiceTabular}{*{24}{c}}[first-row,first-col,hvlines,columns-width=auto,name=A] \CodeBefore \arraycolor{lightgray} \Body edge & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 & 21 & 21 & 22 & 23\ destination & \RowStyle{\itshape} b & d & e & a & c & e & b & e & g & a & e & f & a & b & c & d & f & g & d & e & g & c & e & f \ weight & 4 & 12 & 9 & 4 & 8 & 7 & 8 & 5 & 2 & 12 & 3 & 6 & 9 & 7 & 5 & 3 & 1 & 13 & 6 & 1 & 11 & 2 & 13 & 11 \end{NiceTabular} \end{center}

\vspace{3cm}

\begin{center} \begin{NiceTabular}{*{7}{c}}[first-col,last-row,hvlines,columns-width=auto,name=B] \CodeBefore \arraycolor{lightgray} \Body first edge & 0 & 3 & 6 & 9 & 12 & 18 & 21\ outdegree & 3 & 3 & 3 & 3 & 6 & 3 & 3\ vertex & \RowStyle{\itshape} a & b & c & d & e & f & g\ \end{NiceTabular} \end{center}

\tikzset{A/.style = {name prefix = A-} , B/.style = {name prefix = B-}}

\begin{tikzpicture}[remember picture, overlay] \draw [A] (3-|1) { [B] -- (1-|1) } (3-|4) { [B] -- (1-|2) } (3-|7) { [B] -- (1-|3) } (3-|10) { [B] -- (1-|4) } (3-|12) { [B] -- (1-|5) } (3-|18) { [B] -- (1-|6) } (3-|21) { [B] -- (1-|7) } (3-|24) { [B] -- (1-|8) } ; \end{tikzpicture}

\end{document}

Output of the above code

F. Pantigny
  • 40,250