3

I have the following latex file which I am compiling with LuaLatex on Overleaf:

\documentclass{article}

\usepackage{graphicx} % Pictures
\usepackage{amsmath} % Maths formulas
\usepackage{float} % Figure placement
\usepackage[margin=3cm]{geometry}
\usepackage{tikz} % Pretty graphs
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees, layered}

\title{Assignment 1}

\begin{document}
\pagenumbering{arabic}
\maketitle
\section{}
\subsection*{a.}
In the following table, the first row corresponds to the setup step of Dijkstra's algorithm and the other rows correspond to the state during each iteration of the for loop in the algorithm.
\begin{table}[h!]
    \begin{center}
        \label{tab:table1}
        \begin{tabular}{c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c}
            \textbf{$\#$} & \textbf{T} & \textbf{$u_1$} & \textbf{$u_2$} & \textbf{$u_3$} & \textbf{$u_4$} & \textbf{$u_5$} & \textbf{$u_6$} & \textbf{$u_7$} & \textbf{$P_1$} & \textbf{$P_2$} & \textbf{$P_3$} & \textbf{$P_4$} & \textbf{$P_5$} & \textbf{$P_6$} & \textbf{$P_7$}\\
            \hline
            \textbf{0} & \textbf{$\{2,3,4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1}\\
            \hline
            \textbf{1} & \textbf{$\{3,4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{6} & \textbf{$\infty$} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{2} & \textbf{1}\\
            \hline
            \textbf{2} & \textbf{$\{4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{12} & \textbf{$\infty$} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{3} & \textbf{1} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{3} & \textbf{$\{4,5,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{12} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{3} & \textbf{6} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{4} & \textbf{$\{4,5\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{9} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{7} & \textbf{6} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{5} & \textbf{$\{5\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{9} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{7} & \textbf{6} & \textbf{2} & \textbf{3}\\
        \end{tabular}
    \end{center}
\end{table}

This is the resulting spanning tree:
\newline
\begin{figure}[H]
    \begin{tikzpicture}
          \tikz \graph[tree layout, nodes={circle,draw}]
          {
            1 -- {2,3};
            2 -- 6 -- 5;
            3 -- 7 -- 4;
         };
    \end{tikzpicture}
\end{figure}
\end{document}

It yields this result and I don't understand why:

enter image description here

I tried giving [h], [h!] and [H] to the figure float but I still can't get it to render below the text.

2 Answers 2

6

First off, you don't want a table environment, that might send the tabular somewhere else.

Second, \tikz inside \begin{tikzpicture} is wrong.

I also suggest array instead of tabular, so you can simplify a lot the input.

\documentclass{article}

\usepackage{graphicx} % Pictures
\usepackage{amsmath} % Maths formulas
\usepackage{float} % Figure placement
\usepackage[margin=3cm]{geometry}
\usepackage{tikz} % Pretty graphs
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees, layered}

\title{Assignment 1}

\begin{document}

\section{Some title}
\subsection*{a.}

In the following table, the first row corresponds to the setup step of 
Dijkstra's algorithm and the other rows correspond to the state during 
each iteration of the for loop in the algorithm.

\begin{center}
\boldmath
$\begin{array}{c*{15}{|c}}
\# & T & u_1 & u_2 & u_3 & u_4 & u_5 & u_6 & u_7 & P_1 & P_2 & P_3 & P_4 & P_5 & P_6 & P_7\\
\hline
0 & \{2,3,4,5,6,7\} & 0 & 2 & 5 & \infty & \infty & \infty & \infty & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
\hline
1 & \{3,4,5,6,7\} & 0 & 2 & 5 & \infty & \infty & 6 & \infty & 1 & 1 & 1 & 1 & 1 & 2 & 1 \\
\hline
2 & \{4,5,6,7\} & 0 & 2 & 5 & 12 & \infty & 6 & 7 & 1 & 1 & 1 & 3 & 1 & 2 & 3 \\
\hline
3 & \{4,5,7\} & 0 & 2 & 5 & 12 & 12 & 6 & 7 & 1 & 1 & 1 & 3 & 6 & 2 & 3 \\
\hline
4 & \{4,5\} & 0 & 2 & 5 & 9 & 12 & 6 & 7 & 1 & 1 & 1 & 7 & 6 & 2 & 3 \\
\hline
5 & \{5\} & 0 & 2 & 5 & 9 & 12 & 6 & 7 & 1 & 1 & 1 & 7 & 6 & 2 & 3 \\
\end{array}$
\end{center}

This is the resulting spanning tree:
\begin{center}
\begin{tikzpicture}
  \graph[
    tree layout,
    nodes={circle,draw}
  ]{
    1 -- {2,3};
    2 -- 6 -- 5;
    3 -- 7 -- 4;
  };
\end{tikzpicture}
\end{center}

\end{document}

enter image description here

If you also load \usepackage{booktabs} and change the code for the table to

\begin{center}
\boldmath
$\begin{array}{c*{15}{c}}
\toprule
\# & T & u_1 & u_2 & u_3 & u_4 & u_5 & u_6 & u_7 & P_1 & P_2 & P_3 & P_4 & P_5 & P_6 & P_7\\
\midrule
0 & \{2,3,4,5,6,7\} & 0 & 2 & 5 & \infty & \infty & \infty & \infty & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\
1 & \{3,4,5,6,7\} & 0 & 2 & 5 & \infty & \infty & 6 & \infty & 1 & 1 & 1 & 1 & 1 & 2 & 1 \\
2 & \{4,5,6,7\} & 0 & 2 & 5 & 12 & \infty & 6 & 7 & 1 & 1 & 1 & 3 & 1 & 2 & 3 \\
3 & \{4,5,7\} & 0 & 2 & 5 & 12 & 12 & 6 & 7 & 1 & 1 & 1 & 3 & 6 & 2 & 3 \\
4 & \{4,5\} & 0 & 2 & 5 & 9 & 12 & 6 & 7 & 1 & 1 & 1 & 7 & 6 & 2 & 3 \\
5 & \{5\} & 0 & 2 & 5 & 9 & 12 & 6 & 7 & 1 & 1 & 1 & 7 & 6 & 2 & 3 \\
\bottomrule
\end{array}$
\end{center}

you get much better (in my opinion) output

enter image description here

And even better without \boldmath:

enter image description here

4
  • Thanks for the answer. \tikz together with \graph was indeed the crux of the problem. I wonder if you could expound on your first statement? What does it mean that it might send the tabular somewhere else? What is then the purpose of the table envorinment?
    – Alakanu
    Commented Feb 12, 2023 at 15:00
  • 1
    @Alakanu: table is a class of floats. Floats are not typeset in sequence with the normal text but instead are floated to a convenient place, such as the top of a following page.
    – Unknown
    Commented Feb 12, 2023 at 15:22
  • So, following the same reasoning, should I also avoid the figure environment for my tikzpicture?
    – Alakanu
    Commented Feb 12, 2023 at 16:05
  • @Alakanu Yes, in this case you should avoid it. And indeed I didn't use it. Use table or figure only if there's a caption.
    – egreg
    Commented Feb 12, 2023 at 16:20
2

If you use the following options to \graph and remove the \tikz command like this:

\graph[tree layout, grow down, nodes={circle,draw}]

Then it moves it down:

enter image description here

Code:

\documentclass{article}

\usepackage{graphicx} % Pictures
\usepackage{amsmath} % Maths formulas
\usepackage{float} % Figure placement
\usepackage[margin=3cm]{geometry}
\usepackage{tikz} % Pretty graphs
\usetikzlibrary{graphs}
\usetikzlibrary{graphdrawing}
\usegdlibrary{trees, layered}

\title{Assignment 1}

\begin{document}
\pagenumbering{arabic}
\maketitle
\section{}
\subsection*{a.}
In the following table, the first row corresponds to the setup step of Dijkstra's algorithm and the other rows correspond to the state during each iteration of the for loop in the algorithm.
\begin{table}[h!]
    \begin{center}
        \label{tab:table1}
        \begin{tabular}{c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c|c}
            \textbf{$\#$} & \textbf{T} & \textbf{$u_1$} & \textbf{$u_2$} & \textbf{$u_3$} & \textbf{$u_4$} & \textbf{$u_5$} & \textbf{$u_6$} & \textbf{$u_7$} & \textbf{$P_1$} & \textbf{$P_2$} & \textbf{$P_3$} & \textbf{$P_4$} & \textbf{$P_5$} & \textbf{$P_6$} & \textbf{$P_7$}\\
            \hline
            \textbf{0} & \textbf{$\{2,3,4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1}\\
            \hline
            \textbf{1} & \textbf{$\{3,4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{$\infty$} & \textbf{$\infty$} & \textbf{6} & \textbf{$\infty$} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{2} & \textbf{1}\\
            \hline
            \textbf{2} & \textbf{$\{4,5,6,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{12} & \textbf{$\infty$} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{3} & \textbf{1} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{3} & \textbf{$\{4,5,7\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{12} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{3} & \textbf{6} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{4} & \textbf{$\{4,5\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{9} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{7} & \textbf{6} & \textbf{2} & \textbf{3}\\
            \hline
            \textbf{5} & \textbf{$\{5\}$} & \textbf{0} & \textbf{2} & \textbf{5} & \textbf{9} & \textbf{12} & \textbf{6} & \textbf{7} & \textbf{1} & \textbf{1} & \textbf{1} & \textbf{7} & \textbf{6} & \textbf{2} & \textbf{3}\\
        \end{tabular}
    \end{center}
\end{table}


This is the resulting spanning tree:
\newline
\begin{figure}[h]
    \begin{tikzpicture}
          \graph[tree layout, grow down, nodes={circle,draw}]
          {
            1 -- {2,3};
            2 -- 6 -- 5;
            3 -- 7 -- 4;
         };
    \end{tikzpicture}
\end{figure}
\end{document}

If you want the tree diagram in the centre like this:

enter image description here

use this:

\begin{figure}[h]
\centering
    \begin{tikzpicture}
          \graph[tree layout, grow down, nodes={circle,draw}]
          {
            1 -- {2,3};
            2 -- 6 -- 5;
            3 -- 7 -- 4;
         };
    \end{tikzpicture}
\end{figure}
4
  • 1
    I recommend: \centering.
    – Unknown
    Commented Feb 12, 2023 at 13:35
  • Indeed that is better, thank you, not sure why I went with the longer way!
    – piJT
    Commented Feb 12, 2023 at 13:36
  • 1
    Thanks for the answer. \tikz and \graph was indeed the problem. An earlier answer also solved the problem so I will mark that one as the solution, sorry.
    – Alakanu
    Commented Feb 12, 2023 at 14:57
  • I just want to add in general that I posted this answer roughly at the same time as the accepted one, if not slightly before as there was no answer when I posted this.
    – piJT
    Commented Feb 12, 2023 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .