Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to construct a table look like the picture below:

wanted

The code below represents my best attempt so far (the words in the table don't really mean anything):

\resizebox{9cm}{!} {
\begin{tabular}{l*{2}{c}r} 
Model              & Linear Regression && MLE \\
\hline
fakedata50  & -1.037 & &4   \\
fakedata100 & -1.038 && 3  \\
fakedata500  & -0.7859 && 2 \\
fakedata5000  &-0.7742 && 2  \\
fakedata10000 &-0.7665&&o \\
fakedata100000 &-0.7206&&j \\
fakedata1000000 &-0.6939&&j \\
\end{tabular} }

However, I would like to center my table but the command \centering is not working.

Also, when I tried to enlarge my graph by changing the value in \resizebox, the font size increased as well, which is undesirable. Is there a way of increasing the font size proportionally to how much I enlarge my table?

Lastly, how can I typeset a title for my table?

share|improve this question
    
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. A suggestion: Do us a favour and change your username to something more telling than "user1234". –  Martin Schröder May 12 at 0:41

3 Answers 3

up vote 7 down vote accepted

When constructing tables, the booktabs package is your best friend; in your particular case, I'd also recommend the siunitx package to help with decimal alignment (of course, the package can do a lot more besides column alignment).

screenshot

The S column type takes an optional argument - this allows you to tweak each individual column; I have used table-format=1.4 to detail that this column has 1 number before the decimal, and 4 numbers after it. You'll note that I had to enclose the column head in {Linear Regression} to 'escape' it from math mode and the decimal alignment routine.

Here's the complete code for you to play with.

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}
\begin{tabular}{lS[table-format=1.4]r}
    \toprule
    Model           & {Linear Regression} & MLE \\
    \midrule
    fakedata50      & -1.037              & 4   \\
    fakedata100     & -1.038              & 3   \\
    fakedata500     & -0.7859             & 2   \\
    fakedata5000    & -0.7742             & 2   \\
    fakedata10000   & -0.7665             & o   \\
    fakedata100000  & -0.7206             & j   \\
    fakedata1000000 & -0.6939             & j   \\
    \bottomrule
\end{tabular}
\end{document}

If you're wanting to make a 'title' for your table, then you should use the caption command; you will either have to put your tabular environment inside of a table environment, or otherwise use the captionof command from the caption package.

screenshot with caption

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}

\begin{document}

\begin{table}[!ht]
    \centering
    \caption{Your caption goes here}
    \begin{tabular}{lS[table-format=1.4]r}
        \toprule
        Model           & {Linear Regression} & MLE \\
        \midrule
        fakedata50      & -1.037              & 4   \\
        fakedata100     & -1.038              & 3   \\
        fakedata500     & -0.7859             & 2   \\
        fakedata5000    & -0.7742             & 2   \\
        fakedata10000   & -0.7665             & o   \\
        fakedata100000  & -0.7206             & j   \\
        fakedata1000000 & -0.6939             & j   \\
        \bottomrule
    \end{tabular}
\end{table}
\end{document}
share|improve this answer
    
does that matter if I am using \documentclass[a4paper,12pt]{amsart} ? and how to do the doulbe line on the top and bottom of the graph in latex? –  user3579282 May 9 at 22:57
    
@user3579282 no, it should be fine; it is typically best to provide a complete minimal working example (MWE) so that folks don't have to guess at your documentclass. you can get two horizontal lines by using toprule twice (personally, I wouldn't recommend it) –  cmhughes May 9 at 23:00
    
@cmhugues it works perfectly! thanks a lot! –  user3579282 May 9 at 23:03
    
Shouldn't it be table-format=-1.4? Or may be table-format=+1.4? Or you did it on purpose? –  Manuel May 10 at 13:46

Even the standard tabular environment provides you with tools for adjusting the size of your table. Most notably, the length tabcolsep adjusts the padding on the side of each column. In my example, I needed to shrink it to make the table fit in the standard \textwidth, but in your case, you could increase its value. Likewise, I changed the font size to \small, but one need not do that, unless space is an issue, or to maintain consistency throughout a document.

For vertical space, I use \renewcommand\arraystretch{} to stretch the space between table rows. Once in an environment, \centering works just fine. When using it outside of an environment, use {\centering my table stuff \par}. The braces will limit the extent of the centering to just the embraced stuff. And the \par is needed to activate the \centering.

\documentclass{article}
\usepackage{lipsum}
\usepackage{stackengine}
\def\twostack#1#2{\addstackgap{\stackanchor[0pt]{\strut#1}{\strut#2}}}
\begin{document}
\lipsum[1]
\begin{table}[ht]
\centering
\renewcommand\arraystretch{2}
\tabcolsep 3pt
\caption{Chi-square analysis for data set Total Site 1}
\medskip\small
\begin{tabular}{ccccccc}
\hline
\hline
\textbf{Model} & \textbf{Parameters} & \textbf{Estimates} & 
  \textbf{\twostack{Chi-square}{statistic}} & 
  \textbf{P-value} & \textbf{Outcome} & \\
\hline
Geometric Series & $k$ & 0.129202 & 17019.3 & $<$0.001 & Reject\\
\twostack{Broken Stick}{(binned)} & - & - & 1430.72 & $<$0.001 & Reject\\
\twostack{Broken Stick}{(rank abundance)} & - & - & 120085 & $<$0.001 & Reject\\
Logseries & \twostack{$\alpha$}{$x$} & &&&...\\
\hline
\hline
\end{tabular}
\end{table}
\lipsum[2]
\end{document}

enter image description here

share|improve this answer
    
thank really helps a lot thank you! –  user3579282 May 9 at 23:32
    
@user3579282 You are most welcome. While thanks are appreciated, the customary way of "really" saying thanks on this site is to click the up-arrow to the left of any answer which you find "useful". Then, as you have already done, select the "best" answer to your question by clicking the check mark. –  Steven B. Segletes May 9 at 23:35
    
Yes I am aware of that, since I don't have enough reputation to vote up (requires 15) that's why I have chosen to say thanks in here. I will vote once I have 15 reputation! –  user3579282 May 9 at 23:38
    
@user3579282 It's so long ago, I forgot about such things. Of course you are right. –  Steven B. Segletes May 9 at 23:39

I tried to redo the beginning of the table given as an example by the O.P., as it is more complex. I used the booktabs, caption, makecell and numprint packages, for the column for which alignment on the decimal dot makes sense. Note that whenever in such a column (of type n{…}{…}), a cell contains non-numeric material, it has to be enclosed between brackets.

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe, nomarginpar, noheadfoot]{geometry} 
\usepackage{fourier}
\usepackage{heuristica} 
\usepackage{array}
\usepackage{booktabs}
\usepackage{caption} 
\captionsetup{labelfont = sc, labelsep = period, justification = centering }
\usepackage{makecell}

\renewcommand\theadfont{\bfseries}
\usepackage[autolanguage, np]{numprint}
\usepackage{stackengine} 
\usepackage[english]{babel} 

\begin{document}

\begin{table}[!h]
\centering
\caption{Chi-square analysis for data set\enspace  Total Site 1}
\begin{tabular}{c >{$}c<{$} n{2}{7}c >{$}n{1}{5}<{$} c}
\toprule\midrule
\thead{Model}  & \thead{\textbf{Parameters}} & {\thead{Estimates}} & \thead{Chi-square \\ statistics}  & \thead{\textbf{P-value}} & \thead{Outcome} \\
\midrule
\addlinespace
Geometric Series & k & 0.129202 & 17019.3 &  < 0.001 & Reject \\
\makecell*{Broken Stick \\ (binned)} & - & {\makecell{--}} & 1430.72 &  < 0.001 &     Reject \\
\makecell{Broken Stick \\ (rank abundance)} & - &  {\makecell{--}} &  120085 &  < 0.001 & Reject \\
\multirowcell{2}{Logseries}  & α &7.287017 & \multirowcell{2}{20.44702} & {\multirowcell{2}{0.11667}} & Cannot \\%
   & x  & 0.9999327 &  &  & reject \\[3pt]
\multirowcell{2}{Lognormal} & M & 6.67146 & \multirowcell{2}{30.8349} &  {\multirowcell{2}{< 0.001}} & \multirowcell{2}{Reject} \\
 & V & 16.0206 \\
 \midrule\bottomrule
\end{tabular}
\end{table}

\end{document} 

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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