0

I was trying to adjust my table column width from a similar post here multicolumn tabularx extending page width. But this got me into another problem where the alignment of the header for the last three column is not centred over the last three columns.

I have been trying to figure this out myself for quite a while without much success. Any help would be highly appreciated.

Here is my code for the table using the package tabularx.

\begin{table}[ht]
\renewcommand\arraystretch{1.5}
\newcolumntype{c}{>{\centering\arraybackslash\hspace{0pt}}X}
\centering
\begin{tabularx}{\textwidth}{@{} l *{2}{c} *{3}{c} @{}}
\toprule
$n$ & 
\multicolumn{1}{c}{Old iteration scheme} & 
\multicolumn{1}{c}{New iteration scheme} & 
\multicolumn{3}{c}{Rate of Convergence} \\ 
\cmidrule(lr){4-6} % left- and right-trimming
& $u_n$ & $x_n$ & $|u_n-1|$ & $|x_n-1|$ & $\frac{|x_n-1|}{|u_n-1|}$ \\ 
\midrule
1 & 1.15489 & 1.04139 & 0.154895 & 0.0413883 & 0.267203\\ 
2 & 1.02536 & 1.00041 & 0.025363 & 0.000411711 & 0.0162327\\
3 & 1.00201 & 1.00000 & 0.00201099 & 4.34451$\times10^{-8}$ & 0.0000216038\\
4 & 1.00012 & 1.00000 & 0.000123976 & 4.44089$\times10^{-16}$ & 3.58206$\times10^{-12}$\\
5 & 1.00001 & 1.00000 & 7.46682$\times10^{-6}$ & 0.00000 & 0.000000\\
6 & 1.00000 & 1.00000 & 4.49177$\times10^{-7}$ & 0.00000 & 0.000000\\
7 & 1.00000 & 1.00000 & 2.70198$\times10^{-8}$ & 0.00000 & 0.000000\\
\bottomrule
\end{tabularx}
\caption{Rate of Convergence}
\end{table}
2
  • 1
    Why did you decide to redefine the c column type?
    – leandriis
    Commented Dec 19, 2020 at 22:07
  • 1
    Please make your code compilable by adding the documentclass as well as the relevant packages. If you use the geometry package, please also include the corresponding settings in your example code.
    – leandriis
    Commented Dec 19, 2020 at 22:08

1 Answer 1

1

Using a newly defined C (upper case) column type instead of redefining the c (lower case) type, results in the following output: (Side note: the table could be too wide for the textwidth. Depending on your documentclass, font and margin sizes, you might have to adjust the code to account for that). In the following MWE, I also included an example based on tabular* instead of tabularx:

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand{\theadfont}{\normalsize}

\begin{document}

\begin{table}[ht]
\renewcommand\arraystretch{1.5}
\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\centering
\begin{tabularx}{\textwidth}{@{} l *{5}{C} @{}}
\toprule
$n$ & 
\multicolumn{1}{C}{Old iteration scheme} & 
\multicolumn{1}{C}{New iteration scheme} & 
\multicolumn{3}{c}{Rate of Convergence} \\ 
\cmidrule(lr){4-6} % left- and right-trimming
& $u_n$ & $x_n$ & $|u_n-1|$ & $|x_n-1|$ & $\frac{|x_n-1|}{|u_n-1|}$ \\ 
\midrule
1 & 1.15489 & 1.04139 & 0.154895 & 0.0413883 & 0.267203\\ 
2 & 1.02536 & 1.00041 & 0.025363 & 0.000411711 & 0.0162327\\
3 & 1.00201 & 1.00000 & 0.00201099 & 4.34451$\times10^{-8}$ & 0.0000216038\\
4 & 1.00012 & 1.00000 & 0.000123976 & 4.44089$\times10^{-16}$ & 3.58206$\times10^{-12}$\\
5 & 1.00001 & 1.00000 & 7.46682$\times10^{-6}$ & 0.00000 & 0.000000\\
6 & 1.00000 & 1.00000 & 4.49177$\times10^{-7}$ & 0.00000 & 0.000000\\
7 & 1.00000 & 1.00000 & 2.70198$\times10^{-8}$ & 0.00000 & 0.000000\\
\bottomrule
\end{tabularx}
\caption{Rate of Convergence}
\end{table}


\begin{table}[ht]
\centering
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} l *{5}{c} @{}}
\toprule
$n$ & 
\multicolumn{2}{c}{iteration scheme} & 
\multicolumn{3}{c}{Rate of Convergence} \\
\cmidrule(r){2-3} \cmidrule(l){4-6}
& old & new \\ 
& $u_n$ & $x_n$ & $|u_n-1|$ & $|x_n-1|$ & $\frac{|x_n-1|}{|u_n-1|}$ \\ 
\midrule
1 & 1.15489 & 1.04139 & 0.154895 & 0.0413883 & 0.267203\\ 
2 & 1.02536 & 1.00041 & 0.025363 & 0.000411711 & 0.0162327\\
3 & 1.00201 & 1.00000 & 0.00201099 & 4.34451$\times10^{-8}$ & 0.0000216038\\
4 & 1.00012 & 1.00000 & 0.000123976 & 4.44089$\times10^{-16}$ & 3.58206$\times10^{-12}$\\
5 & 1.00001 & 1.00000 & 7.46682$\times10^{-6}$ & 0.00000 & 0.000000\\
6 & 1.00000 & 1.00000 & 4.49177$\times10^{-7}$ & 0.00000 & 0.000000\\
7 & 1.00000 & 1.00000 & 2.70198$\times10^{-8}$ & 0.00000 & 0.000000\\
\bottomrule
\end{tabular*}
\caption{Rate of Convergence}
\end{table}
\end{document}
5
  • Thanks so much! This works if run independently. However, when I tried using in my thesis, the same error appears, perhaps a package conflict? The following are the packages I used. \usepackage{ragged2e} \usepackage{amsmath,amssymb,amsfonts,amsthm,mathtools} \usepackage{amscd} \usepackage{graphicx} \usepackage{tabularx,booktabs} \usepackage{float} \usepackage{caption} \captionsetup[table]{skip=20pt} \usepackage{hyperref} Commented Dec 20, 2020 at 9:39
  • Does "the same error" refer to "Rate of Convergence" being still printed in two lines instead of in one? If so, this is not related to the list of packages in your last comment. I guess, you still have a line such as \newcolumntype{c}... somewhere in your thesis. (Do you get the following warning: "Redefining primitive column c" upon compiling your thesis?)
    – leandriis
    Commented Dec 20, 2020 at 9:51
  • Yes, "the same error" refer to "Rate of Convergence" being still printed in two lines. I in fact have two tables, the other one (similar syntax but 5 columns). Since the former one does look fine, I didn't bother to mention. And, no, I don't get the error you mentioned. Commented Dec 20, 2020 at 9:55
  • @markhaokip: With the information your provided so far, I am unfortunately not able to reproduce the issue. Probably it is easier if you prepare a minimal example including the working as well as the non-working table, and ask a follow-up-question.
    – leandriis
    Commented Dec 20, 2020 at 10:08
  • okay, thanks very much once again! Commented Dec 20, 2020 at 10:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .