[Tex/LaTex] Table width is not wide enough

tablestwo-column

I have a problem in the table width, it is very small as shown below and I need it to fit in two column paper. Any help please?

enter image description here

\begin{table}[H]
\centering
\begin{tabular}{|r|r|r|r|}
\hline
    &    Class 1 &    Class 2 &    Class 3 \\
\hline
   Class 1 &         0 &         0 &        262 \\
\hline
   Class 2 &         0 &         0 &        295 \\
\hline
   Class 3 &        0 &         0 &        320 \\
\hline
\end{tabular}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}

Best Answer

You can use the tabularx package and use a width of \columnwidth so your table will span the whole width of the column:

\documentclass[twocolumn]{article}
\usepackage{tabularx}

\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}

\begin{document}

\begin{table}
\centering
\begin{tabularx}{\columnwidth}{|Y|Y|Y|Y|}
\hline
    &    Class 1 &    Class 2 &    Class 3 \\
\hline
   Class 1 &         0 &         0 &        262 \\
\hline
   Class 2 &         0 &         0 &        295 \\
\hline
   Class 3 &        0 &         0 &        320 \\
\hline
\end{tabularx}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}

\end{document}

enter image description here

You can also set the appropriate width for the columns manually:

\documentclass[twocolumn]{article}
\usepackage{array}

\begin{document}

\begin{table}
\centering
\begin{tabular}{*{4}{|>{\raggedleft\arraybackslash}p{\dimexpr\columnwidth/4-2\tabcolsep-\fboxrule\relax}}|}
\hline
    &    Class 1 &    Class 2 &    Class 3 \\
\hline
   Class 1 &         0 &         0 &        262 \\
\hline
   Class 2 &         0 &         0 &        295 \\
\hline
   Class 3 &        0 &         0 &        320 \\
\hline
\end{tabular}
\caption{Confusion Matrix}
\label{tab:template}
\end{table}

\end{document}