1

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)

2
  • Put in preambel \newcolumntype{C}{>{\centering\arraybackslash}X} and then instead of X for column type use C.
    – Zarko
    May 23 '15 at 15:45
  • can you give me the whole line where I need to put it? since I am getting errors May 23 '15 at 15:49
4

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

3
  • Is there a way how I can insert 2 tables next to each other instead of enlarge it to text width please? (i.e. have 2 of the first example next to each other) That would be perfect! May 23 '15 at 15:52
  • how can I remove the alignment of number from the first table(the midrule etc one)? May 23 '15 at 16:19
  • @user1930901 Just use cc instead of S[...]S[...] as specification for the columns. May 23 '15 at 16:31
1

Ok, here is complete example which should clarify may comment:

\documentclass{article}
    \usepackage{tabularx}
% definition of new column type should be in preamble of your document
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}
\begin{table}%[H] if possible, avoid to use ``[H] 
             % option for placing of table, better is [htbp]
    \centering
\begin{tabularx}{\textwidth}{|C|C|}
\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{An important table ...}
\end{table}
    \end{document}

and lookout of table:

enter image description here

To my taste, the lookout of table in Heiko Oberdiek answer is (far) better.

2
  • please help me with this tex.stackexchange.com/questions/246594/… since I still cannot get it how I want to May 23 '15 at 16:11
  • But this question is removed from SE. For any help I need MWE in form as I give you in my answer (and Heiko Oberdiek). This is standard way to ask for help on SE.
    – Zarko
    May 23 '15 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.