8

I have a tikz figure in which a table is embedded within the horizontal lines drawn in the figure. I need to adjust the rows of the table according to the figure placement. I tried adjust the table spacing manually but it does not produce a consistent result. Only the last row is somehow aligned. The first entry (y, y_n) is off by a row. Any help regarding how this can be achieved will be appreciated.

enter image description here

MWE

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{booktabs}

\pagestyle{empty}

\begin{document} \begin{tikzpicture}[line cap=round,line join=round] \draw [ultra thick] (-6.,2.)-- (-6.,1.); \draw [ultra thick] (-6.,2.)-- (-4.,2.); \draw [ultra thick] (-4.,1.)-- (-4.,4.); \draw [ultra thick] (0.,7.)-- (0.,1.); \draw [ultra thick] (-2.,3.)-- (-2.,1.); \draw [ultra thick] (3.,5.)-- (3.,1.); \draw [ultra thick] (5.,3.)-- (5.,1.); \draw [ultra thick] (1.,2.)-- (0.,2.); \draw [ultra thick] (3.,5.)-- (0.,5.); \draw [ultra thick] (-4.,4.)-- (0.,4.); \draw [ultra thick] (-4.,1.)-- (-5.,1.); \draw [ultra thick] (-2.,1.)-- (-3.,1.); \draw [ultra thick] (-2.,3.)-- (0.,3.); \draw [ultra thick] (0.,1.)-- (-1.,1.); \draw [ultra thick] (1.,2.)-- (1.,1.); \draw [ultra thick] (3.,1.)-- (2.,1.); \draw [ultra thick] (3.,2.)-- (4.,2.); \draw [ultra thick] (4.,2.)-- (4.,1.); \draw [ultra thick] (5.,1.)-- (6.,1.); \draw [ultra thick] (5.,3.)-- (3.,3.); \draw [gray] (-6.5,0.5)-- (9,0.5); \draw [gray] (-6.5,1.5)-- (9,1.5); \draw [gray] (-6.5,2.5)-- (9,2.5); \draw [gray] (-6.5,3.5)-- (9,3.5); \draw [gray] (-6.5,4.5)-- (9,4.5); \draw [gray] (-6.5,5.5)-- (9,5.5); \draw [gray] (-6.5,6.5)-- (9,6.5); \draw [gray] (-6.5,7.5)-- (9,7.5); \draw [gray] (-6.5,8.5)-- (9,8.5);

\draw node (-3,-.25) {Symbol \tikz \fill [] circle (0.25cm); denotes one pair of rabbits.}; \fill [] (0.,7.) circle (0.25cm); \fill [] (0.,6.) circle (0.25cm); \fill [] (0.,5.) circle (0.25cm); \fill [] (0.,4.) circle (0.25cm); \fill [] (0.,3.) circle (0.25cm); \fill [] (0.,2.) circle (0.25cm); \fill [] (0.,1.) circle (0.25cm); \fill [] (-1.,1.) circle (0.25cm); \fill [] (-2.,1.) circle (0.25cm); \fill [] (-3.,1.) circle (0.25cm); \fill [] (-4.,1.) circle (0.25cm); \fill [] (-5.,1.) circle (0.25cm); \fill [] (-6.,1.) circle (0.25cm); \fill [] (-6.,2.) circle (0.25cm); \fill [] (-4.,2.) circle (0.25cm); \fill [] (-4.,3.) circle (0.25cm); \fill [] (-4.,4.) circle (0.25cm); \fill [] (-2.,3.) circle (0.25cm); \fill [] (-2.,2.) circle (0.25cm); \fill [] (1.,2.) circle (0.25cm); \fill [] (1.,1.) circle (0.25cm); \fill [] (2.,1.) circle (0.25cm); \fill [] (3.,1.) circle (0.25cm); \fill [] (3.,2.) circle (0.25cm); \fill [] (3.,3.) circle (0.25cm); \fill [] (3.,4.) circle (0.25cm); \fill [] (3.,5.) circle (0.25cm); \fill [] (4.,2.) circle (0.25cm); \fill [] (4.,1.) circle (0.25cm); \fill [] (5.,1.) circle (0.25cm); \fill [] (5.,2.) circle (0.25cm); \fill [] (5.,3.) circle (0.25cm); \fill [] (6.,1.) circle (0.25cm);

\node (tbl) at (9,4.5){ \begin{tabularx}{.2\textwidth}{cc} $n $ & $y_{n}$ \[20pt] 1 & 1 \[20pt] 2 & 1 \[20pt] 3 & 2 \[20pt] 4 & 3 \[20pt] 5 & 5 \[20pt] 6 & 8 \[20pt] 7 & 13 \[20pt] \end{tabularx}};

\end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688
Damitr
  • 1,889
  • Although most likely still not ideal, you could replace your tabularx node with something like \node(tbl0) at (8.5,8){$n $ \quad $y_{n}$}; \node(tbl1) at (8.5,7){1 \quad 1}; \node(tbl2) at (8.5,6){2 \quad 1}; \node(tbl3) at (8.5,5){3 \quad 2}; \node(tbl4) at (8.5,4){4 \quad 3}; \node(tbl5) at (8.5,3){5 \quad 5}; \node(tbl6) at (8.5,2){6 \quad 8}; \node(tbl7) at (8.5,1){7 \quad 13};. – leandriis Aug 08 '20 at 12:03
  • Yes, I had thought about doing it manually. But it seems to be un-LaTeX thing to do. – Damitr Aug 08 '20 at 12:14
  • @Damitr This is a very pretty and efficient diagram to explain the calculation of the Fibonacci numbers. Il like it a lot ! – projetmbc Aug 08 '20 at 13:18
  • Thanks. It is the part of a calculus book I am typesetting – Damitr Aug 10 '20 at 10:00

3 Answers3

9

This is more than you asked for, but just to demonstrate a completely different approach to drawing a diagram like that. Like AboAmmar I add the "table" as nodes, using a loop.

enter image description here

\documentclass[10pt]{article}
\usepackage{tikz}
\pagestyle{empty}

\begin{document} \begin{tikzpicture}[ scale=0.75, yscale=-1 ]

% draw horizontal lines with labels
\foreach \nn/\yn [count=\i from 0] in {n/y_n,1/1,2/1,3/2,4/3,5/5,6/8,7/13} { \draw [gray] (0, \i+0.5) -- +(15,0); \node at (14.7,\i) {$\yn$}; \node at (14,\i) {$\nn$};

\ifnum \i=0
    \draw [gray] (0, \i-0.5) -- +(15,0);
\fi

}

% draw connections \draw [ultra thick] (1,7) |- (3,6) |- (2,7) (3,6) |- (7,4) |- (5,5) |- (4,7) (7,5) |- (6,7) (7,6) -| (8,7) (7,4) -- (7,1) (7,3) -| (10,7) -- (9,7) (10,6) -| (11,7) (10,5) -| (12,7) -- (13,7);

% draw markers \draw [draw=none,mark=*,mark size=2mm] plot coordinates {% (7,1) (7,2) (7,3)(10,3) (3,4)(7,4)(10,4) (3,5)(5,5)(7,5)(10,5)(12,5) (1,6)(3,6)(5,6)(7,6)(8,6)(10,6)(11,6)(12,6) (1,7)(2,7)(3,7)(4,7)(5,7)(6,7)(7,7)(8,7)(9,7)(10,7)(11,7)(12,7)(13,7) };

\end{tikzpicture} \end{document}

Torbjørn T.
  • 206,688
6

Remove the tabular, it won't mix well with the tikz picture. Use instead the following two lines:

\foreach \y[count=\i] in {7,6,...,1} \node at (8,\y){\i};
\foreach \y[count=\i] in {13,8,5,3,2,1,1} \node at (8.9,\i){\y};

to add the numbers to the right of your dots.

\documentclass[10pt]{article}
\usepackage{tikz}
\usepackage{tabularx}
\usepackage{booktabs}

\pagestyle{empty}

\begin{document} \begin{tikzpicture}[line cap=round,line join=round] \draw [ultra thick] (-6.,2.)-- (-6.,1.); \draw [ultra thick] (-6.,2.)-- (-4.,2.); \draw [ultra thick] (-4.,1.)-- (-4.,4.); \draw [ultra thick] (0.,7.)-- (0.,1.); \draw [ultra thick] (-2.,3.)-- (-2.,1.); \draw [ultra thick] (3.,5.)-- (3.,1.); \draw [ultra thick] (5.,3.)-- (5.,1.); \draw [ultra thick] (1.,2.)-- (0.,2.); \draw [ultra thick] (3.,5.)-- (0.,5.); \draw [ultra thick] (-4.,4.)-- (0.,4.); \draw [ultra thick] (-4.,1.)-- (-5.,1.); \draw [ultra thick] (-2.,1.)-- (-3.,1.); \draw [ultra thick] (-2.,3.)-- (0.,3.); \draw [ultra thick] (0.,1.)-- (-1.,1.); \draw [ultra thick] (1.,2.)-- (1.,1.); \draw [ultra thick] (3.,1.)-- (2.,1.); \draw [ultra thick] (3.,2.)-- (4.,2.); \draw [ultra thick] (4.,2.)-- (4.,1.); \draw [ultra thick] (5.,1.)-- (6.,1.); \draw [ultra thick] (5.,3.)-- (3.,3.); \draw [gray] (-6.5,0.5)-- (9,0.5); \draw [gray] (-6.5,1.5)-- (9,1.5); \draw [gray] (-6.5,2.5)-- (9,2.5); \draw [gray] (-6.5,3.5)-- (9,3.5); \draw [gray] (-6.5,4.5)-- (9,4.5); \draw [gray] (-6.5,5.5)-- (9,5.5); \draw [gray] (-6.5,6.5)-- (9,6.5); \draw [gray] (-6.5,7.5)-- (9,7.5); \draw [gray] (-6.5,8.5)-- (9,8.5);

\foreach \y[count=\i] in {7,6,...,1} \node at (8,\y){\i}; \foreach \y[count=\i] in {13,8,5,3,2,1,1} \node at (8.9,\i){\y};

\draw node (-3,-.25) {Symbol \tikz \fill [] circle (0.25cm); denotes one pair of rabbits.}; \fill [] (0.,7.) circle (0.25cm); \fill [] (0.,6.) circle (0.25cm); \fill [] (0.,5.) circle (0.25cm); \fill [] (0.,4.) circle (0.25cm); \fill [] (0.,3.) circle (0.25cm); \fill [] (0.,2.) circle (0.25cm); \fill [] (0.,1.) circle (0.25cm); \fill [] (-1.,1.) circle (0.25cm); \fill [] (-2.,1.) circle (0.25cm); \fill [] (-3.,1.) circle (0.25cm); \fill [] (-4.,1.) circle (0.25cm); \fill [] (-5.,1.) circle (0.25cm); \fill [] (-6.,1.) circle (0.25cm); \fill [] (-6.,2.) circle (0.25cm); \fill [] (-4.,2.) circle (0.25cm); \fill [] (-4.,3.) circle (0.25cm); \fill [] (-4.,4.) circle (0.25cm); \fill [] (-2.,3.) circle (0.25cm); \fill [] (-2.,2.) circle (0.25cm); \fill [] (1.,2.) circle (0.25cm); \fill [] (1.,1.) circle (0.25cm); \fill [] (2.,1.) circle (0.25cm); \fill [] (3.,1.) circle (0.25cm); \fill [] (3.,2.) circle (0.25cm); \fill [] (3.,3.) circle (0.25cm); \fill [] (3.,4.) circle (0.25cm); \fill [] (3.,5.) circle (0.25cm); \fill [] (4.,2.) circle (0.25cm); \fill [] (4.,1.) circle (0.25cm); \fill [] (5.,1.) circle (0.25cm); \fill [] (5.,2.) circle (0.25cm); \fill [] (5.,3.) circle (0.25cm); \fill [] (6.,1.) circle (0.25cm);

%\node (tbl) at (9,4.5){ %\begin{tabularx}{.2\textwidth}{cc} %$n $ & $y_{n}$ \[20pt] %1 & 1 \[20pt] %2 & 1 \[20pt] %3 & 2 \[20pt] %4 & 3 \[20pt] %5 & 5 \[20pt] %6 & 8 \[20pt] %7 & 13 \[20pt] %\end{tabularx}};

\end{tikzpicture} \end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
6

If you want to do that with a tabular, here is a solution with a {NiceArray} of nicematrix.

\documentclass[10pt]{article}
\usepackage{nicematrix}
\usepackage{tikz}
\pagestyle{empty}

\def \N { \raise-1mm\hbox{\scalebox{2}{$\bullet$}} } \def \C { \Cdots } \def \V { \Vdots }

\NiceMatrixOptions { rules/width=0.2pt , xdots = { line-style={solid,thick}, shorten=-0.5mm } }

\begin{document}

$\begin{NiceArray}{*{27}{c}}[nullify-dots] & & & & & & & & & & & & & & & & & & & & & & & & &n&y_n \ \hline \ & & & & & & & & & & & &\N& & & & & & & & & & & & &1&1 \ & & & & & & & & & & & &\V& & & & & & & & & & & & & & \ \hline \ & & & & & & & & & & & &\N& & & & & & & & & & & & &2&1 \ & & & & & & & & & & & &\V& & & & & & & & & & & & & & \ \hline \ & & & & & & & & & & & &\N& & &\C& & &\N& & & & & & &3&2 \ & & & & & & & & & & & &\V& & & & & &\V& & & & & & & & \ \hline \ & & & &\N& & & &\C& & & &\N& & & & & &\N& & & & & & &4&3\ & & & &\V& & & & & & & &\V& & & & & &\V& & & & & & & & \ \hline \ & & & &\N& & & &\N& &\C& &\N& & & & & &\N& &\C& &\N& & &5&5\ & & & &\V& & & &\V& & & &\V& & & & & &\V& & & &\V& & & & \ \hline \ \N& &\C& &\N& & & &\N& & & &\N&\C&\N& & & &\N&\C&\N& &\N& & &6&8 \ \V& & & &\V& & & &\V& & & &\V& &\V& & & &\V& &\V& &\V& & & & \ \hline \ \N& &\N&\C&\N& &\N&\C&\N& &\N&\C&\N& &\N& &\N&\C&\N& &\N& &\N&\C&\N&7&13\ \end{NiceArray}$

\end{document}

Ouput of the above code

F. Pantigny
  • 40,250