# How to fix these errors?

Could anyone help fixing the error and shift the table's legend up a bit?
Can you make the table legend with TikZ instead of \legend?

\documentclass[]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,shapes.geometric}
\usepackage{array}

\begin{document}

\begin{figure}[h]
\begin{tabular}{*{2}{>{\centering\arraybackslash}b{\dimexpr0.5\textwidth-2\tabcolsep\relax}}}
\legend{Weighted, complete graph $K_H$}
\begin{tikzpicture}[
state/.append style={minimum size=5mm}]
%\begin{pgfonlayer}{nodelayer}
\node [state] (0) at (-2, 3) [label=left:E] {};
\node [state] (1) at ( 2, 3) [label=right:B]{};
\node [state] (2) at (-1.25, 0.75)[label=left:D] {};
\node [state] (3) at ( 1.25, 0.75) [label=right:C]{};
\node [state] (4) at ( 0, 4.5) [label=above: A]{};
%\end{pgfonlayer}
%\begin{pgfonlayer}{edgelayer}
\draw  (0) to (4);
\draw (4) to (1);
\draw (1) to (3);
\draw (0) to (2);
\draw (3) to (2);
\draw (2) to (4);
\draw (4) to (3);
\draw (0) to (1);
\draw (0) to (3);
\draw (2) to (1);
%\end{pgfonlayer}
\end{tikzpicture}

&
\renewcommand{\arraystretch}{1.3}
\legend{\textbf{THIS IS TABLE LEGEND}}
\begin{tabular}{c|ccccc}
& A  & B & C & D & E  \\
\hline
A & --  & 4 & 7 & 6 & 12 \\
B & 4  & -- & 3 & 5 & 8  \\
C & 7  & 3 & -- & 2 & 5  \\
D & 6  & 5 & 2 & -- & 9  \\
E & 12 & 8 & 5 & 9 & --
\end{tabular}

\end{tabular}
\end{figure}

\end{document}

• standalone is not intended to contain floats. If you persist to contain it, than you need to add option varwidth: \documentclass[varwidth]{standalone}. Where you define \legend˛command? Does it should be caption? Dec 14, 2022 at 5:10
• @Zarko I'm going to export it as PDF and then insert to document as figure so I don't want to add caption. I placed the legend command right at the begining of the table.
– hana
Dec 14, 2022 at 6:05

Possible solution:

\documentclass[varwidth]{standalone}
\usepackage{tikz}
\usetikzlibrary{automata,shapes.geometric}
\usepackage{array}

\begin{document}

\begin{figure}
\begin{tabular}{*{2}{>{\centering\arraybackslash}p{\dimexpr0.5\textwidth-2\tabcolsep\relax}}}
\textbf{Weighted, complete graph $K_H$}
&   \textbf{THIS IS TABLE LEGEND}   \\
\begin{tikzpicture}[baseline=(current bounding box.center),
scale=0.8,
state/.style = {circle, draw, minimum width=5mm, inner sep=0pt}
]
\node [state] (e) at (-2, 3) [label=left:E] {};
\node [state] (b) at ( 2, 3) [label=right:B]{};
\node [state] (d) at (-1.25, 0.75)[label=left:D] {};
\node [state] (c) at ( 1.25, 0.75) [label=right:C]{};
\node [state] (a) at ( 0, 4.5) [label=above: A]{};
\draw   (a) -- (b) -- (c) -- (d) -- (e) -- (a)
(a) -- (c) -- (e) -- (b) -- (d) -- (a);
\end{tikzpicture}
&   \renewcommand{\arraystretch}{1.3}
\begin{tabular}{c|ccccc}
& A & B & C & D & E  \\
\hline
A & --  & 4 & 7 & 6 & 12 \\
B & 4  & -- & 3 & 5 & 8  \\
C & 7  & 3 & -- & 2 & 5  \\
D & 6  & 5 & 2 & -- & 9  \\
E & 12 & 8 & 5 & 9 & --
\end{tabular}
\end{tabular}
\end{figure}

\end{document}


• If you only need <file name>.pfd file, which you will include with \includegraphics{file.name>} in your main document, than is advisable to simplified above MWE to:
% file name
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{automata}

\begin{document}

\begin{tabular}{cc}
\textbf{Weighted, complete graph $K_H$}
&   \textbf{This is table legend}   \\
\begin{tikzpicture}[baseline=(current bounding box.center),
scale=0.8,
state/.style = {circle, draw, minimum width=5mm, inner sep=0pt}
]
\node [state] (e) at (-2, 3) [label=left:E] {};
\node [state] (b) at ( 2, 3) [label=right:B]{};
\node [state] (d) at (-1.25, 0.75)[label=left:D] {};
\node [state] (c) at ( 1.25, 0.75) [label=right:C]{};
\node [state] (a) at ( 0, 4.5) [label=above: A]{};
\draw   (a) -- (b) -- (c) -- (d) -- (e) -- (a)
(a) -- (c) -- (e) -- (b) -- (d) -- (a);
\end{tikzpicture}
&   \renewcommand{\arraystretch}{1.3}
\begin{tabular}{c|ccccc}
& A  & B & C & D & E  \\
\hline
A & --  & 4 & 7 & 6 & 12 \\
B & 4  & -- & 3 & 5 & 8  \\
C & 7  & 3 & -- & 2 & 5  \\
D & 6  & 5 & 2 & -- & 9  \\
E & 12 & 8 & 5 & 9 & --
\end{tabular}
\end{tabular}

\end{document}

• In both cases you need to care, that width of images will not be bigger than \textwidth in the main document.
• May I set the smaller borders? I want to export it as PDF so I don't want it to have large margin.
– hana
Dec 14, 2022 at 6:18
• @hana, you can set any border as you wish. However, now is not clear how you will use this figure, i.e. how it will be insert to your document where will be inserted caption. Dec 14, 2022 at 6:28
• I tried \documentclass[border={1cm}]{standalone} but it doesn't work. The table and figure above is exported as one single PDF file and then I'll use \includegraphics to insert that to my document.
– hana
Dec 14, 2022 at 6:37
• @hana, as @Zarko said, to prepare a clean PDF for including later you should remove the figure environment --- this is doing nothing here. Don't get confused by the name: the figure environment means "add some material that will float around and will be labeled as a figure" --- you will have the figure environment in the document that *includes * your PDF, not here! Dec 14, 2022 at 9:31
• @hana, please ask new question. Where cleare describe what you after. You should be aware, that standalone is designed so, that image canvas is adopted to size of images + declared borders. See package documentation for details. Dec 14, 2022 at 11:08