3

I am trying to add a table to my Latex document but the caption of the table shrunk in the left corner and does not extend to the whole width of the table. This is my table:

\usepackage{graphicx} % More fancy images
\usepackage[showframe=true]{geometry}
\usepackage{changepage}
\usepackage{ctable}% http://ctan.org/pkg/ctable
\usepackage{caption}% http://ctan.org/pkg/caption
\begin{document}

\begin{table}[!ht]
\footnotesize

\begin{adjustwidth}{-0.5cm}{}
  \resizebox{0.7\textwidth}{!}{\begin{minipage}{\textwidth}
\caption{The constraints on the content of the universe based on best fit to the $\Lambda$CDM model. The first column represents the results from adding different cosmological probes.}        
    \begin{tabular}{  l  c  c   c  c  p{5.cm} }
    \hline\hline
    Parameter & P+WP & P+WP+highL & P+WP+highL+L & P+WP+highL+L+BAO & Summary \\ \hline
    $\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon    \\ 
    $\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
    $\Omega_mh^2$& 0.14305 & ... & ... & ... & Total matter \\ 
    $\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
    $\Sigma m_{\nu}$[eV]&0.002&...&0.000&...&neutrino\\
    $H_0$& 67.04&67.15&67.94&67.77&expansion rate \\\hline
    \end{tabular}
      \end{minipage}}
\end{adjustwidth}      
\end{table}
\end{document}

How can I fix this problem?

  • What's the adjustwidth environment for? And why reducing the width to .7\textwidth? – egreg Nov 9 '14 at 13:45
  • @egreg I used it in order to fit the whole table in the width of the page, since it was extended more than the size of page. – Dalek Nov 9 '14 at 13:47
  • That's the main reason for having it pushed on the left. – egreg Nov 9 '14 at 13:58
2

You can set the width of the table to \textwidth by choosing a tabularx environment. The only trick, so to say, is that whereas the body of the four middle columns ought to be specified to have column type S (from the siunitx package, so that the numbers are aligned on the decimal columns), the column headers should be specified to have (a modified form of the) column type X (provided by the tabularx package); this allows the long header lines to be split easily into two parts, while letting LaTeX figure out the column widths for you.

I also suggest you (a) use the rule-drawing macros of the booktabs package -- \toprule, \midrule, and \bottomrule -- to obtain well-spaced horizontal lines and (b) replace the ... ellipses with $\cdots$.

With this setup, either specifying \footnotesize or using the \resizebox macro to make the table fit inside the text block turns out to be unnecessary. Put differently, it's feasible to typeset the table without a change in font size. Keeping font sizes constant across major elements of a document is usually considered desirable from a typographic point of view.

enter image description here

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{booktabs} % \toprule, \midrule, and \bottomrule macros
\usepackage{siunitx}  % for "S" column type
\usepackage{tabularx} % for "tabularx" environment
\usepackage[justification=justified, singlelinecheck=false]{caption}
\newcolumntype{C}{>{\arraybackslash\centering}X}
\newcommand\MC[1]{\multicolumn{1}{C}{#1}}  % handy shortcut macro

\begin{document}
\begin{table}[!ht]
\sisetup{table-format=2.6, group-digits=false}

\caption{Constraints on the content of the universe based on best fit to the $\Lambda$CDM model. 
    The first column represents the results from adding different cosmological probes.}

\begin{tabularx}{\textwidth}{@{} l *{4}{S}  l @{}}
\toprule
Parameter & \MC{P$+$WP} & \MC{P$+$WP$+$highL} & \MC{P$+$WP$+$highL $+$L} & \MC{P$+$WP$+$highL $+$L$+$BAO} & Summary \\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon\\ 
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 &  {$\cdots$}  &  {$\cdots$}  &  {$\cdots$}  & Total matter \\ 
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV]&0.002& {$\cdots$} &0.000& {$\cdots$} &Neutrino\\
$H_0$& 67.04&67.15&67.94&67.77&Expansion rate \\
\bottomrule
\end{tabularx} 
\end{table}
\end{document}

Addendum: I understand from your comments that you may have a very old TeX distribution which doesn't feature the siunitx package and hence doesn't know about that package's S column type. I'm posting a second solution, which features the dcolumn package and its d column type to achieve decimal alignment of the numbers. Since the dcolumn package has been around for 20 years or so, I'm hopeful that your TeX distribution does include this package. (If it seems to you that the screenshot below looks exactly the same as the one above, that's indeed correct.)

enter image description here

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{booktabs} % \toprule, \midrule, and \bottomrule macros
\usepackage{dcolumn}  % decimal alignment of numbers
\newcolumntype{d}[1]{D{.}{.}{#1}}
\usepackage{tabularx} % for "tabularx" environment
\usepackage[justification=justified, singlelinecheck=false]{caption}
\newcolumntype{C}{>{\arraybackslash\centering}X}
\newcommand\MC[1]{\multicolumn{1}{C}{#1}}  % handy shortcut macro

\begin{document}
\begin{table}[!ht]

\caption{Constraints on the content of the universe based on best fit to the $\Lambda$CDM model. 
    The first column represents the results from adding different cosmological probes.}

\begin{tabularx}{\textwidth}{@{} l *{4}{d{2.6}}  l @{}}
\toprule
Parameter 
& \MC{P$+$WP} 
& \MC{P$+$WP$+$highL} 
& \MC{P$+$WP$+$highL $+$L} 
& \MC{P$+$WP$+$highL $+$L$+$BAO} 
& Summary \\
\midrule
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon\\ 
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 &  \MC{$\cdots$}  &  \MC{$\cdots$}  &  \MC{$\cdots$}  & Total matter \\ 
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV]&0.002& \MC{$\cdots$} &0.000& \MC{$\cdots$} &Neutrino\\
$H_0$& 67.04&67.15&67.94&67.77&Expansion rate \\
\bottomrule
\end{tabularx} 
\end{table}
\end{document}
  • The caption in your answer is centred, how could I change it to adjust from left and extend to the whole table – Dalek Nov 9 '14 at 14:48
  • @dalek - Since the caption package is being loaded, it suffices to specify the options justification=justified and singlelinecheck=false when loading that package. I will update the example to reflect this requirement. – Mico Nov 9 '14 at 14:51
  • where should I use justification=justified option? – Dalek Nov 9 '14 at 14:55
  • @Dalek - I've updated the code. The options justification=justified and singlelinecheck=false should be set when loading the caption package. – Mico Nov 9 '14 at 15:01
  • 1
    @Dalek - Note that \Centering and \centering are not the same. You mentioned earlier that you have "an old version of linux". Which TeX distribution, and which vintage, do you have? Any chance you could upgrade to the latest version of, say, TeXlive? For what it's worth, the tabularx package and the array package (which is loaded by tabularx) have been around for a very long time as well. If your TeX installation can't handle these packages, it's really time for an update. By the way, do you use LaTeX2e (which has been the standard since 1994) or the older LaTeX2.09? – Mico Nov 9 '14 at 17:11
3

In general it's better to avoid rescaling a table; never rescale the caption.

Your main problem is due to the wide column headers; I don't think it's useful to have the table extending to the whole line width, so I'll present two solutions.

\documentclass{article}
\usepackage[showframe=true]{geometry}
\usepackage{caption}% http://ctan.org/pkg/caption
\usepackage{booktabs,tabularx,siunitx}

\newcommand{\splitcell}[1]{%
  \begin{tabular}{@{}c@{}}\strut#1\strut\end{tabular}%
}
\newcommand{\var}[1]{\mathit{#1}}

\begin{document}

\begin{table}[!htp]
\centering\small

\caption{The constraints on the content of the universe based on best fit to the 
$\Lambda$CDM model. The first column represents the results from adding different 
cosmological probes.}

\begin{tabularx}{\textwidth}{
  l
  S[table-format=2.6]
  S[table-format=2.6]
  S[table-format=2.6]
  S[table-format=2.6]
  X
}
\toprule
Parameter
 & {$P+\var{WP}$}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}$}}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L$}}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L+\var{BAO}$}}
 & Summary
\\
\midrule
$\Omega_bh^2$              & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\ 
$\Omega_ch^2$              & 0.12038  & 0.12025  & 0.11847  & 0.11889  & Cold dark matter \\
$\Omega_mh^2$              & 0.14305  & {\dots}  & {\dots}  & {\dots}  & Total matter \\ 
$\Omega_{\Lambda}$         & 0.6817   & 0.6830   & 0.6939   & 0.6914   & Dark energy \\
$\Sigma m_{\nu}$ (\si{eV}) & 0.002    & {\dots}  & 0.000    & {\dots}  & neutrino \\
$H_0$                      & 67.04    & 67.15    & 67.94    & 67.77    & expansion rate $i$ \\
\bottomrule
\end{tabularx}

\end{table}

\begin{table}[!htp]
\centering

\caption{The constraints on the content of the universe based on best fit to the 
$\Lambda$CDM model. The first column represents the results from adding different 
cosmological probes.}

\begin{tabular}{
  l
  S[table-format=2.6]
  S[table-format=2.6]
  S[table-format=2.6]
  S[table-format=2.6]
  l
}
\toprule
Parameter
 & {$P+\var{WP}$}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}$}}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L$}}
 & {\splitcell{$P+\var{WP}\:+$\\$\var{highL}+L+\var{BAO}$}}
 & Summary
\\
\midrule
$\Omega_bh^2$              & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\ 
$\Omega_ch^2$              & 0.12038  & 0.12025  & 0.11847  & 0.11889  & Cold dark matter \\
$\Omega_mh^2$              & 0.14305  & {\dots}  & {\dots}  & {\dots}  & Total matter \\ 
$\Omega_{\Lambda}$         & 0.6817   & 0.6830   & 0.6939   & 0.6914   & Dark energy \\
$\Sigma m_{\nu}$ (\si{eV}) & 0.002    & {\dots}  & 0.000    & {\dots}  & neutrino \\
$H_0$                      & 67.04    & 67.15    & 67.94    & 67.77    & expansion rate $i$ \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

As you see, you don't need to rescale the table.

  • Thanks it looks promising but when I add it to my latex document, I get these errors ! LaTeX Error: \splitcell undefined. l.49 \renewcommand{\splitcell} [1]{% Try typing <return> to proceed. If that doesn't work, type X <return> to quit. ! LaTeX Error: \var undefined. See the LaTeX manual or LaTeX Companion for explanation. Type H <return> for immediate help. ... l.52 \renewcommand{\var} [1]{\mathit{#1}} Try typing <return> to proceed. If that doesn't work, type X <return> to quit. (./aas_macros.sty) (./thesis.aux) \openout1 = thesis.aux'. ` – Dalek Nov 9 '14 at 14:33
  • @Dalek - Did you copy the contents of the preamble of the code given above into the preamble of your document? – Mico Nov 9 '14 at 14:42
  • @Mico yes I did. But it seems it doesn't recognize siunitx package. – Dalek Nov 9 '14 at 14:45
  • @Dalek - The siunitx package has been around for several years now and is distributed with both TeXlive and MiKTeX. Which TeX distribution do you use? – Mico Nov 9 '14 at 15:04
  • In first table, it's not necessary to specify \centering if the width of the tabularx environment is set to \textwidth. Separately, the table will fit inside the text block even if \small is omitted. – Mico Nov 9 '14 at 15:09
1

Here is a solution that doesn't require \resizebox nor adjustwidth nor loading the changepage package, using the tabularx environment, plus booktabs,makecell(allowing for linebreaks in cells) andsiunitx` to improve the look of the table:

\documentclass{article}
\usepackage{mathtools}
\usepackage{booktabs}
\usepackage{graphicx} %
\usepackage[showframe=true]{geometry}
\usepackage{caption}%
\usepackage{array, tabularx}
\setlength\extrarowheight{2pt}
\usepackage{makecell}
\renewcommand\theadalign{rc}
\usepackage{siunitx}
\sisetup{table-format=1.6,table-number-alignment=center}

\begin{document}
\vspace*{1cm}


\begin{table}[!ht]
\footnotesize\captionsetup{format = hang, font=footnotesize, labelfont=sc}
\caption{The constraints on the content of the universe based on best fit to the $\Lambda$CDM model. The first column represents the results from adding different cosmological probes.}
\begin{tabularx}{\linewidth}{l *{4}{S}X}
\toprule\midrule
Parameter & {P+WP} & {\thead{P+WP+highL}} & {\thead{P+WP+highL\\+L\mbox{\hskip1.9em}}} & {\thead{P+WP+highL\\+L+BAO}} & Summary \\
\midrule\addlinespace
$\Omega_bh^2$ & 0.022032 & 0.022069 & 0.022199 & 0.022161 & Baryon \\
$\Omega_ch^2$& 0.12038 & 0.12025 & 0.11847 & 0.11889 & Cold dark matter \\
$\Omega_mh^2$& 0.14305 & {...} & {...} & {...} & Total matter \\
$\Omega_{\Lambda}$ & 0.6817 & 0.6830 & 0.6939 & 0.6914 & Dark energy \\
$\Sigma m_{\nu}$[eV] & 0.002 & {...} & 0.000 & {...} &neutrino\\
$H_0$& {67.04} & {67.15} & {67.94} & {67.77} & expansion rate \\
\bottomrule
\end{tabularx}
\end{table}

\end{document} 

enter image description here

  • The ... strings should probably be replaced with typographically appropriate ellipses. – Mico Nov 9 '14 at 15:28
  • @Mico: I just copied the O.P. code, not knowing what he/she meant exactly with these dots. – Bernard Nov 9 '14 at 15:29
  • @Bernard Plus I don't think $\Omega_b$ can be written the way you wrote it. – Dalek Nov 9 '14 at 15:43
  • @ Dalek: Oops! Sorry, it's my editor that's configured to display greek letters such as Ω while saving them as \Omega, &c. Corrected. – Bernard Nov 9 '14 at 15: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.