2

I want to present two big tables (9 columns each) side by side. For that I wrote this code:

\begin{table}
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
& Class 1 & Class 2 & Class 3 & Class 4 & Class 5 & Class 6 & Class 7 & Class 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1  & 0.917  & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0  \\\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|c|c|c|c|c|c|c|c|c|}
\hline
& Class 1 & Class 2 & Class 3 & Class 4 & Class 5 & Class 6 & Class 7 & Class 8\\\hline
TPR & 1 & 1 & 1 & 1 & 1  & 0.917  & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0  \\\hline
\end{tabular}
\caption{Bar}
}
\end{table}

Output: enter image description here

Tables overlap with each other. I cannot reduce the sizes of the tables, their internal texts and respective captions.

Mycode generated two warnings also: enter image description here

Line 264 and 277 contain just closing braces } .


These are the packages I've used so far :

\usepackage[utf8]{inputenc} 
\usepackage[english]{babel} 
\usepackage{amsmath} 
\usepackage{amsfonts} 
\usepackage{amssymb} 
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry} 
\usepackage[paper=portrait,pagesize]{typearea} 
\usepackage{mathtools}% ctan.org/pkg/mathtools 
\usepackage{amsmath} 
\usepackage{graphicx}

Edit

By following Pluton's suggestion in comment section, I've almost solved my problem except the right end of the right table is not aligned with the texts of the document.

enter image description here

Mico
  • 506,678
Debbie
  • 303
  • 3
    For the top rows, do you have to repeat "class" in every cell? If not, that would fix your problem – pluton Sep 26 '20 at 10:03
  • Which documentclass do you use? Do you use the geometry package? If so, please tell us wich options you selected. – leandriis Sep 26 '20 at 10:07
  • The graphicx package is needed if you want to include images in your document. Since your code does not include any image, this package is unrelated to the issue itself. Please show us the line that starts with \documentclass. – leandriis Sep 26 '20 at 10:11
  • Does "I cannot reduce the sizes of the tables, their internal texts and respective captions." mean that you wouldlike to do that but you don't know how to or does it mean you are not allowed to do that but would like th know how you can make your tables fit nontheless? – leandriis Sep 26 '20 at 10:12
  • I'm using LaTex first time. Not well versed with it at all. These are the packages I've used so far :\usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry} \usepackage[paper=portrait,pagesize]{typearea} \usepackage{mathtools}% http://ctan.org/pkg/mathtools \usepackage{amsmath} \usepackage{graphicx} – Debbie Sep 26 '20 at 10:14
  • 1
    @Debbie - I've taken the liberty of copying the contents of your latest comment into the body of your query. There, it'll be seen and read more easily. – Mico Sep 26 '20 at 10:19
  • 2
    Thanks for adding the packages. typearea and geometry contradict each other. What kind of margin size do you actually want to have? The margin size crucially determines how much space is available for the tables and thus how one can make sure thex fit into the avialable space. – leandriis Sep 26 '20 at 10:19
  • There's an edit in my question. Plz see once. – Debbie Sep 26 '20 at 10:23
  • 1
    Your edit does address not @leandriis' comment that the settings of the typearea and geometry packages conflict with each other. – Mico Sep 26 '20 at 10:33

3 Answers3

3

Try this code:

\documentclass{article}
\usepackage{array}
\usepackage{geometry}

\begin{document} \begin{table} \setlength{\tabcolsep}{4pt} \setlength{\extrarowheight}{2pt} \noindent\parbox{.45\linewidth}{ \centering \begin{tabular}{|{9}{c|}} \hline \textbf{Class}& \bfseries 1 & \bfseries 2 & \bfseries 3 & \bfseries4 & \bfseries 5 & \bfseries 6 & \bfseries 7 & \bfseries 8\\hline TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\hline TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\hline FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\hline FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\hline \end{tabular} \caption{Foo} } \hfill \parbox{.45\linewidth}{ \centering \begin{tabular}{|{9}{c|}} \hline

\textbf{Class}& \bfseries 1 & \bfseries 2 & \bfseries 3 & \bfseries 4 & \bfseries 5 & \bfseries 6 & \bfseries 7 & \bfseries 8\\hline TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\hline TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\hline FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\hline FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\hline \end{tabular} \caption{Bar} } \end{table}

\end{document}

enter image description here

Edit: I also propose this variant, which uses theS column type from siunitx:

\begin{table}
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\sisetup{table-number-alignment=center}
\noindent\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{5}{c|}S[table-format=1.4]|S[table-format=1.3]|c|c|}
\hline
\multicolumn{1}{|c}{\bfseries Class}& \head{1} & \head{2} & \head{3} & \head{4} & \head{5}& \head{6} & \head{7}& \multicolumn{1}{c|}{\bfseries 8}\\ \hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Foo}
}
\hfill
\parbox{.45\linewidth}{
\centering
\begin{tabular}{|*{5}{c|}S[table-format=1.4]|S[table-format=1.3]|c|c|}
\hline
\multicolumn{1}{|c}{\bfseries Class}& \head{1} & \head{2} & \head{3} & \head{4} & \head{5}& \head{6} & \head{7}&\multicolumn{1}{c|}{\bfseries 8}\\
\hline
TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \\\hline
TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \\\hline
FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \\\hline
FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \\\hline
\end{tabular}
\caption{Bar}
}
\end{table}

enter image description here

Bernard
  • 271,350
3

Here is my suggestion using siunitx for the alignment of the numbers, booktabs for the horizontal lines and tabular* to make sure the tables fit into the available space. Additionally, I have replaced the \parbox commands with two minipage environments:

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{caption}

\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}

\begin{document}

\begin{table} \begin{minipage}{0.45\textwidth} \setlength{\tabcolsep}{0pt} \sisetup{table-format=1} \caption{Foo} \label{tab:foo} \begin{tabular}{\linewidth}{@{\extracolsep{\fill}}cSSSSS[table-format=1.4]S[table-format=1.3]SS} \toprule & \multicolumn{8}{c}{Class}\ \cmidrule{2-9} & {1} & {2} & {3} & {4} & {5} & {6} & {7} & {8}\ \midrule TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \ TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \ FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \ FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \ \bottomrule \end{tabular} \end{minipage} \hfill \begin{minipage}{0.45\textwidth} \setlength{\tabcolsep}{0pt} \sisetup{table-format=1} \caption{Bar} \label{tab:bar} \begin{tabular}{\linewidth}{@{\extracolsep{\fill}}cSSSSS[table-format=1.4]S[table-format=1.3]SS} \toprule & \multicolumn{8}{c}{Class}\ \cmidrule{2-9} & {1} & {2} & {3} & {4} & {5} & {6} & {7} & {8}\ \midrule TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \ TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \ FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \ FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \ \bottomrule \end{tabular} \end{minipage} \end{table}

\end{document}

leandriis
  • 62,593
2

I suggest you re-organize the header material to that one sees only one instance of "Class" per table. I'd also get rid of all vertical lines and most horizontal lines in order to give the tables a more open and inviting "look".

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc} 
\usepackage[english]{babel} 
\usepackage{amsmath} 
%%\usepackage{amsfonts} % is loaded automatically by amssymb
\usepackage{amssymb} 
\usepackage[margin=2cm]{geometry} 
\usepackage[paper=portrait,pagesize]{typearea} 
\usepackage{mathtools} % ctan.org/pkg/mathtools 
%%\usepackage{amsmath} % is loaded automatically by mathtools
\usepackage{graphicx}
\usepackage{booktabs} % <-- new

\begin{document} \begin{table} \setlength\tabcolsep{0pt} \begin{minipage}{0.475\textwidth} \begin{tabular}{1\textwidth}{@{\extracolsep{\fill}} l {8}{c}} \toprule & \multicolumn{8}{c}{Class} \ \cmidrule{2-9} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \ \midrule TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \ TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \ FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \ FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \ \bottomrule \end{tabular} \caption{Foo} \end{minipage} \hfill \begin{minipage}{0.475\textwidth} \begin{tabular}{1\textwidth}{@{\extracolsep{\fill}} l {8}{c}} \toprule & \multicolumn{8}{c}{Class} \ \cmidrule{2-9} & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 \ \midrule TPR & 1 & 1 & 1 & 1 & 1 & 0.917 & 1 & 1 \ TNR & 1 & 1 & 1 & 1 & 0.99 & 1 & 1 & 1 \ FPR & 0 & 0 & 0 & 0 & 0.0095 & 0 & 0 & 0 \ FNR & 0 & 0 & 0 & 0 & 0 & 0.083 & 0 & 0 \ \bottomrule \end{tabular} \caption{Bar} \end{minipage} \end{table}

\end{document}

Mico
  • 506,678