4

I am working on a seminar paper, and I would like to code the following type of table into my LaTeX document. I was only able to create this table by first entering in and formatting everything in Microsoft Excel. Then, I copied and pasted the table from Excel into Word (Keep source formatting). From there, I saved the Word document as a PDF, and in the PDF, I cut out the table by taking a "snapshot" (possible in Adobe Acrobat Reader), then pasting that into Word again. Then I could save the copied-back-into-Word picture of the table as a PNG (.png). From there, I used \includegraphics[.]{Table1.png}. I got my "desired" result, however the table looks rather sloppy, as you can see below.

I am looking for any tips on how I can program the table in LaTeX in such a way that it looks nice (with all of the same formatting, lines, etc.) as seen in my version below! I've tried exhaustively all of the tables, tabular, tabbing, array commands in LaTeX without any such success.

Data table for seminar paper

  • Have you not made a table before? What part is proving to be the roadblock? You might want to look at booktabs for the horizontal rules, siunitx for aligning the numbers better, and maybe threeparttable for the 'notes' under the table proper. But you should, as a courtesy, post a minimal example of what you have tried so far. At the very least, it is not pleasant for people to have to type out all the words you clearly have already typed out! – jon Dec 11 '16 at 17:37
  • I'll add the caption package. – Bernard Dec 11 '16 at 17:39
  • Off-topic: The acronym for "Taxpayer identification number" should be "TIN", not "NIT", right? – Mico Dec 11 '16 at 17:48
  • Hello, happy tax-payer! (Droopy) :o) – Bernard Dec 11 '16 at 17:57
  • In case you are using R to estimate your models, the R-package stargazer can generate such a table for you (it will generate the LaTeX code). – Jan van der Laan Dec 12 '16 at 7:32
9

Some suggestions, in no particular order.

  • Engage in as little visual formatting as possible. It's much better to set some top-level parameters and let LaTeX do the formatting.

  • Numeric data with decimal markers tend to be easier to read if they're aligned on the decimal markers.

  • Use the facilities of the caption package to format the caption to your liking.

  • You'll get a much better look the line-drawing macros of the booktabs package -- \toprule, \cmidrule, \midrule and \bottomrule -- than with the basic LaTeX commands \hline and \cline.

  • Don't overuse bold-face. For readability, a table primarily needs to be laid out very well. Bold-facing should hardly ever be necessary.

  • Using a tabularx environment instead of the more basic tabular environment will simplify making the 6 data columns all have the same widths.

  • For footnote-like material at the bottom of a table, use \footnotesize -- or \small if you think that \footnotesize makes the contents look to, well, small.

  • One formatting choice I'd question in the screenshot you posted is the left-aligning of the entries in the data columns. I think that centering would be a more natural choice. The following code and screenshot shows both looks. I think the second table, which centers the contents of the data columns, is more visually balanced. In terms of code, it differs from the first tabularx environment only in the use of \mC instead of \mX wrapper macros. (\mC and \mX are defined in the preamble as handy shortcut macros.)

enter image description here

\documentclass{article}
\usepackage{tabularx,dcolumn,booktabs,caption,ragged2e,geometry}
\geometry{a4paper,margin=2.5cm} % set page and textblock parameters
\captionsetup{justification = RaggedRight, singlelinecheck=false,
              font = bf, skip = 0.33\baselineskip}
\newcolumntype{d}[1]{D..{#1}} % for aligning numerical data on decimal marker
\newcommand\mX[1]{\multicolumn{1}{X}{#1}} % handy shortcut macro
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} % another handy shortcut macro

\begin{document}
\begin{table}[t]
\caption{Baseline OLS and 2SLS results. Contents of data columns left-aligned.}
\begin{tabularx}{\textwidth}{@{}l*{6}{d{1.5}}@{}}
\toprule
& \mX{(1)} & \mX{(2)} & \mX{(3)} & \mX{(4)} & \mX{(5)} & \mX{(6)}  \\
\cmidrule(l){2-7}
& \mX{OLS} & \mX{OLS} & \mX{OLS} & \mX{OLS} & \mX{2SLS} & \mX{2SLS}  \\
\midrule
Tax identification number (TIN) 
   & 0.533^{***} & & & & & 1.376^{**}\\
   & (0.12)      & & & & & (0.66)\\
Constant 
   & 8.020^{***} & & & & & 7.367^{***} \\
   & (0.58)      & & & & & (0.86)\\
Observations & \mX{404} & & & & & \mX{369}\\
R\textsuperscript{2} & \mX{0.21} & & & & & \\
First-stage F-statistic & & & & & 10.77 & 12.77 \\
\bottomrule
\end{tabularx}

\medskip
\footnotesize
Dependent variable: log monthly profits.

Robust standard errors in parentheses.

$^{*}$, $^{**}$ and $^{***}$ indicate significance \dots

Instrument in columns (5) \dots

(1) Baseline OLS, (2) \dots

Control estimates left out of table.

See: Table 4 in \dots
\end{table}


\begin{table}[h]
\caption{Baseline OLS and 2SLS results. Contents of data columns centered.}
\begin{tabularx}{\textwidth}{@{}l*{6}{d{1.5}}@{}}
\toprule
& \mC{(1)} & \mC{(2)} & \mC{(3)} & \mC{(4)} & \mC{(5)} & \mC{(6)}  \\
\cmidrule(l){2-7}
& \mC{OLS} & \mC{OLS} & \mC{OLS} & \mC{OLS} & \mC{2SLS} & \mC{2SLS}  \\
\midrule
Tax identification number (TIN) 
   & 0.533^{***} & & & & & 1.376^{**}\\
   & (0.12)      & & & & & (0.66)\\
Constant 
   & 8.020^{***} & & & & & 7.367^{***} \\
   & (0.58)      & & & & & (0.86)\\
Observations & \mC{404} & & & & & \mC{369}\\
R\textsuperscript{2} & \mC{0.21} & & & & & \\
First-stage F-statistic & & & & & 10.77 & 12.77 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
  • Hi, thanks for your input! This table looks really nice and the code for it is quite basic (which I really like because I'm quite new to LaTeX!) In its own document, this table works our perfectly. However, when I added the packages to my preamble and pasted your code (above) into my document, some of the lines shifted strangely (particularly the one just below the column numbers. I'm not sure what would be the cause of this, but I was able to use parts of your code as well as from the others below to get a nice table. Thanks again! :) – Zatchet09 Dec 12 '16 at 10:53
  • @Zatchet09 - You're most welcome. You may want to post a follow-up query in which you show your actual preamble. Maybe, just maybe, there's a conflict between the packages you're already loading and the ones that are needed for the code of my answer to be compilable? – Mico Dec 12 '16 at 11:18
8

Here a solution using tabularx and siunitx which has a nice facility to reserve space after entries with table-space-text-post. One nasty trick this solution uses is \noalign{\vspace{3pt}} to add some additional vertical space after \bottomrule. I like to set footnotes in tables over the length of the table and use \quad instead of line breaks, but that's a matter of taste, I think.

\documentclass{article}

\usepackage[showframe]{geometry}

\usepackage{tabularx,booktabs,ragged2e,caption}
\usepackage{siunitx}
\usepackage[inline]{enumitem}

\begin{document}

\noindent
\begin{table}[t]
  \caption{Baseline OLS and 2SLS results}
  \label{tab:foo}
  \footnotesize
  \sisetup{%
    table-number-alignment=left,
    table-format=1.3,
    table-space-text-post=***}%
  \begin{tabularx}{1.0\linewidth}{@{}X*{6}{S}}
    \toprule
    & {(1)} & {(2)} & {(3)} & {(4)} & {(5)} & {(6)} \\
    \cmidrule{2-7}
    & {OLS} & {OLS} & {OLS} & {OLS} & {2SLS} & {2SLS} \\
    \midrule
    Tax identification numbeer (NIT)
    & 0.533 *** & 0.0835 & 0.431*** & 0.0119 & 1.173** & 1.376** \\
    & \multicolumn{1}{l}{(0.12)}
            & \multicolumn{1}{l}{(0.13)}
                    & \multicolumn{1}{l}{(0.12)}
                            & \multicolumn{1}{l}{(0.12)}
                                    & \multicolumn{1}{l}{(0.69)}
                                            & \multicolumn{1}{l}{(0.66)} \\
    Constant & 8.020*** & 6.122*** & 7.794 *** \\
    \bottomrule
    \noalign{\vspace{3pt}}
    \multicolumn{7}{@{}p{\linewidth}@{}}{\RaggedRight %
    Dependent variable: log monthly profits.\quad
    Robust standard erros in parentheses.\quad
    *, **, *** indicate significance at \SI{10}{\percent},
    \SI{5}{\percent} and \SI{1}{\percent}, respectively. \newline
    \begin{enumerate*}[label=(\arabic*), itemjoin={{, }}]
    \item Baseline OLS
    \item OLS with firm size control
    \item OLS with background characteristics
    \item OLS with firm size and background controls
    \item 2LSL ...
    \end{enumerate*}  
    }
  \end{tabularx}
\end{table}

\end{document}

enter image description here

5

A variation of combination Arash Esbati and Mico answers:

\documentclass{article}

\usepackage[showframe]{geometry}

\usepackage{tabularx,booktabs,ragged2e,caption}
\usepackage{siunitx}
\usepackage[inline]{enumitem}

\newcommand\br{)}
\newcommand\mcs[1]{\multicolumn{1}{l}{(#1)}}
\newcommand\mcx[1]{\multicolumn{1}{>{\centering\arraybackslash}X}{#1}}

\begin{document}

\noindent
\begin{table}[t]
  \caption{Baseline OLS and 2SLS results}
  \label{tab:foo}
  \footnotesize
  \sisetup{%
    table-number-alignment=left,
    table-format=1.3,
    table-space-text-post=***}%
  \begin{tabularx}{\linewidth}{l*{6}{S}}
    \toprule
    & \mcx{1}   & \mcx{2}   & \mcx{3}   & \mcx{4}   & \mcx{4}   & \mcx{5}   \\
    \cmidrule(lr){2-7}
    & {OLS}     & {OLS}     & {OLS}     & {OLS}     & {2SLS}    & {2SLS}    \\
    \midrule
    Tax identification number (TIN)
    & 0.533 *** & 0.0835    & 0.431*** & 0.0119 & 1.173** & 1.376** \\
    & \mcs{0.12} & \mcs{0.13} & \mcs{0.12} & \mcs{0.12} & \mcs{0.69} & \mcs{0.66} \\
Constant & 8.020*** & 6.122*** & 7.794 *** \\
    \midrule[0.8pt]
    \multicolumn{7}{p{\dimexpr\linewidth-2\tabcolsep\relax}}{\RaggedRight %
    Dependent variable: log monthly profits.

    Robust standard erros in parentheses.

    *, **, *** indicate significance at \SI{10}{\percent},

    \SI{5}{\percent} and \SI{1}{\percent}, respectively. 

    \begin{enumerate*}[label=(\arabic*), itemjoin={{, }}]
    \item Baseline OLS
    \item OLS with firm size control
    \item OLS with background characteristics
    \item OLS with firm size and background controls
    \item 2LSL with all firm size and background controls
    \end{enumerate*}

    Control estimates left out of table.
    }
  \end{tabularx}
\end{table}
\end{document}

which gives:

enter image description here

  • Could you explain please what this "\label{tab:foo}" command does? Also, how does "\footnotesize" work in your code? Finally, I have my document's line-spacing set at 1.5 (necessary for submission), so when I use your code the text of the entire table continues to take the 1.5 spacing, which makes the table extra tall. Do you have any suggestions as to how I could override the document's line spacing in my tables to have single spacing? Thanks! – Zatchet09 Dec 12 '16 at 10:58
  • 1
    (i) Command label{tab:foo} is for referencing table in text, for example ... see Table \ref{tab:foo} ... (ii) table is relative large, for the placing it text with I reduce its size. You can try change it to \small or even omit font size change, (iii) search in this site, how to reduce 1.5 spicing in tables. one possibilities probably is \linespread{1} added after \begin{table} (not tested, I'm on the way ...). – Zarko Dec 12 '16 at 13:57
1

You could also try using some online Latex Table converter or even the Excel to Latex plugin:

https://www.ctan.org/tex-archive/support/excel2latex/?lang=en

This con­verter al­lows you to write a ta­ble in Ex­cel, and ex­port the cur­rent se­lec­tion as LATEX markup which can be pasted into an ex­ist­ing LATEX doc­u­ment, or ex­ported to a file and in­cluded via the \input com­mand.

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.