[Tex/LaTex] Centering text in a table

horizontal alignmenttablestexmaker

I have a table which needs to expand to the width of my page and I do this by

\begin{table}[H]
\centerline{
\begin{tabularx}{\textwidth}{|X|X|}
\hline
\textbf{Number} & \textbf{Probability} \\ \hline
0 & 100 \% \\ \hline
998.13 & 60 \% \\ \hline
1996.26 & 31.37 \% \\ \hline
2994.39 & 16.11 \% \\ \hline
3992.52 & 8.26 \% \\ \hline
4990.64 & 4.23 \% \\ \hline
5988.77 & 2.17\% \\ \hline
6986.90 & 1.11\% \\ \hline
7828.46 & 0.63\% \\ \hline
\end{tabularx}
}
\caption{} 
\end{table}

however, the text is not becoming centered in the table , it is left justified. How can I center the text and get the text to be centered? (As I only managed one at a time)

Best Answer

Nice version

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

\begin{document}
\begin{table}
\centering
\caption{Table example}
\begin{tabular}{
  S[table-format=4.2]
  S[table-format=3.2]
}
\toprule
\textbf{Number} & \textbf{Probability} (\si{\percent}) \\
\midrule
   0    & 100    \\
 998.13 &  60    \\
1996.26 &  31.37 \\
2994.39 &  16.11 \\
3992.52 &   8.26 \\
4990.64 &   4.23 \\
5988.77 &   2.17 \\
6986.90 &   1.11 \\
7828.46 &   0.63 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Result

Two table variant

Remarks:

  • The optional parameter of tabular or tabularx specifies, where the anchor of the table lies. t means the top element of the table (top rule or first row), b means the bottom line or last row. As default the table is vertically centered.

  • Horizontal placement. First use \centering instead of \centerline{...}. \centering centers the lines of the paragraph, whereas \centerline restricts the contents to a \hbox and does not warn, if the elements do not fit in the current line width. If the two tables fit in one line, then \hfill inbetween will move the left table to the left and right table to the right side of the text area. The weaker \hfil can be used, if the free space between the tables should be equally distributed to the left of the left table, the middle between the tables and to the right of the right table.

Full example:

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

\begin{document}
\begin{table}
\centering
\caption{Example with two tables}
\begin{tabular}[t]{
  S[table-format=4.2]
  S[table-format=3.2]
}
\toprule
\textbf{Number} & \textbf{Probability} (\si{\percent}) \\
\midrule
   0    & 100    \\
 998.13 &  60    \\
1996.26 &  31.37 \\
2994.39 &  16.11 \\
3992.52 &   8.26 \\
4990.64 &   4.23 \\
5988.77 &   2.17 \\
6986.90 &   1.11 \\
7828.46 &   0.63 \\
\bottomrule
\end{tabular}\hfill
\begin{tabular}[t]{
  S[table-format=4.2]
  S[table-format=3.2]
}
\toprule
\textbf{Number} & \textbf{Probability} (\si{\percent}) \\
\midrule
   0    & 100    \\
 998.13 &  60    \\
1996.26 &  31.37 \\
2994.39 &  16.11 \\
4990.64 &   4.23 \\
5988.77 &   2.17 \\
7828.46 &   0.63 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}

Result with two tables

Stretched to text width:

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

\begin{document}
\begin{table}
\centering
\caption{Table example}
\begin{tabular*}{\linewidth}{
  l
  @{\extracolsep\fill}
  S[table-format=4.2]
  S[table-format=3.2]
  l
}
\toprule
& \textbf{Number} & \textbf{Probability} (\si{\percent}) \\
\midrule
&    0    & 100    \\
&  998.13 &  60    \\
& 1996.26 &  31.37 \\
& 2994.39 &  16.11 \\
& 3992.52 &   8.26 \\
& 4990.64 &   4.23 \\
& 5988.77 &   2.17 \\
& 6986.90 &   1.11 \\
& 7828.46 &   0.63 \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

Result enlarged

With ugly lines

\documentclass{article}
\usepackage{array}
\usepackage{caption}
\usepackage{siunitx}
\usepackage{tabularx}

\begin{document}
\begin{table}
\centering
\caption{Table example}
\begin{tabularx}{\linewidth}{
  |S[table-format=4.2]|S[table-format=3.2]|
}
\hline
\multicolumn{1}{|X|}{\centering\textbf{Number}}
& \multicolumn{1}{X|}{\centering\textbf{Probability} (\si{\percent})} \\
\hline
   0    & 100    \\ \hline
 998.13 &  60    \\ \hline
1996.26 &  31.37 \\ \hline
2994.39 &  16.11 \\ \hline
3992.52 &   8.26 \\ \hline
4990.64 &   4.23 \\ \hline
5988.77 &   2.17 \\ \hline
6986.90 &   1.11 \\ \hline
7828.46 &   0.63 \\ \hline
\end{tabularx}
\end{table}
\end{document}

Result with ugly lines

Centering without alignment of numbers

\documentclass{article}
\usepackage{array}
\usepackage{caption}
\usepackage{tabularx}

\begin{document}
\begin{table}
\centering
\caption{Table example}
\begin{tabularx}{\linewidth}{
  |>{\centering\arraybackslash}X|
   >{\centering\arraybackslash}X<{\unskip~\%}|
}
\hline
\textbf{Number}
& \multicolumn{1}{X|}{\centering\textbf{Probability}} \\ \hline
   0    & 100    \\ \hline
 998.13 &  60    \\ \hline
1996.26 &  31.37 \\ \hline
2994.39 &  16.11 \\ \hline
3992.52 &   8.26 \\ \hline
4990.64 &   4.23 \\ \hline
5988.77 &   2.17 \\ \hline
6986.90 &   1.11 \\ \hline
7828.46 &   0.63 \\ \hline
\end{tabularx}
\end{table}
\end{document}

Result centered without number alignment

Related Question