-2

enter image description hereI tried to do this table below in LaTeX, but I didn't figure it out, can someone help me to do that, and to look professional in the same time.

  • 2
    Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.`` – Alessandro Cuttin Apr 16 '20 at 12:53
  • 4
    please take a look at this answer and see if it fits your needs: Color rows in table – Jojo Apr 16 '20 at 12:54
  • 4
    Please show what you tried so far. If you want alternating rowcolors, adding \usepackage[table]{xcolor} and using \rowcolors{1}{gray}{white} might give you a place to start from. Regariding: "look professional " you might consider not using color, using horizontal lines from the booktabs package, siunintx to align the numbers and remove the use of bold text inside of the table. – leandriis Apr 16 '20 at 12:57
  • You probably know that Mathematica has the capability of copying a cell into a LaTeX format which you can then paste into your LaTeX editor, right? – Xavier Apr 16 '20 at 16:40
  • The siunitx package has some table formatting, the booktabs is the professional way of styling the tables. I'll do two examples in a moment. – Oleg Lobachev Apr 16 '20 at 19:00

5 Answers5

3

Example 1

Here are your tabular as in the picture you enclosed. I have set both tabulars inside the same table-environment to keep them together.

In My opinion, they are not professional, but copycats of something knocked together in Word or Excel. For example, the blue row in the first tablar indicates that the row is the heading, which it obviously not is. The "headings" are columns 1 and 3, columns 2 and 4 is data.

The alternating shadowing in the second tabular is also wrong. The first row and first column is the "heading", but that effect is lost because of the shadowing.

I have updated the code so that the two tabulars at least are of equal width, calculated relative to the line width. I have made some other tweaks to add consistence:

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}

\newcolumntype{B}{>{\bfseries}wr{\dimexpr((\linewidth/4))}}
\newcolumntype{R}{>{$}wr{\dimexpr((\linewidth/4)-4\tabcolsep)}<{$}}
\newcolumntype{b}{>{\bfseries}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}}
\newcolumntype{s}{>{$}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}<{$}}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}
\setlength{\tabcolsep}{4pt}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}

\begin{document}

\begin{table}
\small
\rowcolors{3}{white}{lightgray!25}
\begin{tabular}{Bwr{\dimexpr((\linewidth/4)-4\tabcolsep)}BR}
\multicolumn{4}{@{}l}
{\textcolor{darkgray}{\bfseries Probit Regression Results}}\\
\rowcolor{LightBlue}Dep.\,Variable:     &               spi1    &   No.\,Observations:  &   254         \\
        Model:      &               Probit  &       Df Residuals:   &   248         \\
      Methods:      &               MLE     &       Df Model:       &   5           \\
         Date:      &   Thu, 16 Apr 2020    &   Pseudo R-squ.:      &   0.1311      \\
         Time:      &           00:35:44    &   Log-Likelihood:     &   -149.65     \\
    converge:       &           True        &           LL.Null:    &   -172.23     \\
Covariance Type:    &       nonrobust       &       LLR p-value     &   1.341\mbox{e-}08    \\

\end{tabular}

\bigskip

\rowcolors{1}{lightgray!25}{white}
\begin{tabular}{b*{6}{s}}

        &   \thead{coef}        &   \thead{std err} &       \thead{z}   &   \thead{P\pz}    &   \thead{[0.025}  &   \thead{0.975]}  \\
const   &   -5.1870     &   1.270   &   -4.083  &   0.000   &   -7.677  &   -2.697  \\
vol     &   -10.0795    &   18.930  &   -0.532  &   0.594   &   -47.182 &   27.023  \\
rec     &   -0.8608     &   0.394   &   -2.183  &   0.029   &   -1.634  &   -0.088  \\
spread  &   0.3881      &   0.087   &   4.483   &   0.000   &   0.218   &   0.558   \\
DP      &   11.3808 &   2.526   &   4.505   &   0.000   &   6.429   &   16.332  \\
EP      &   -1.3759     &   1.721   &   -0.799  &   0.424   &   -4.750  &   1.998   \\

\end{tabular}

\end{table}

\end{document}

Example 2

Here, I have suggested a more traditional layout, You may consider removing the horizontal rule in the first table, and replace the rule with some white space:

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}

\newcolumntype{A}{>{\itshape}wl{\dimexpr((\linewidth/4))}}
\newcolumntype{Q}{>{$}wr{\dimexpr((\linewidth/4)-4\tabcolsep)}<{$}}
\newcolumntype{a}{>{\itshape}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}}
\newcolumntype{q}{>{$}wr{\dimexpr((\linewidth/7)-2\tabcolsep)}<{$}}
\newcommand{\thead}[1]{\multicolumn{1}{r}{\itshape #1}}
\newcommand{\pz}{$<|z|$}
\arrayrulecolor{lightgray}
\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}

\begin{document}

\begin{table}
\small
\setlength{\tabcolsep}{10pt}

\begin{tabular}{@{}Awr{\dimexpr((\linewidth/4)-4\tabcolsep)}|AQ@{}}
\multicolumn{4}{@{}l}
{\textcolor{darkgray}{\bfseries Probit Regression Results}}\\ \hline
Dep.\,Variable      &               spi1    &   No.\,Observations   &   254         \\
        Model       &               Probit  &       Df Residuals    &   248         \\
      Methods       &               MLE &           Df Model    &   5           \\
         Date       &   Thu, 16 Apr 2020    &   Pseudo R-squ.       &   0.1311      \\
         Time       &           00:35:44    &   Log-Likelihood      &   -149.65     \\
    Converge        &           True        &           LL.Null     &   -172.23     \\
Covariance Type &       nonrobust       &       LLR p-value     &   1.341\mbox{e-}08    \\

\end{tabular}

\bigskip
\bigskip

\setlength{\tabcolsep}{6pt}
\begin{tabular}{@{}a|*{6}{q}@{}}

        &   \thead{coef}        &   \thead{std err} &       \thead{z}   &   \thead{P\pz}    &   \thead{[0.025}  &   \emph{0.975]}   \\ \hline
const   &   -5.1870     &   1.270   &   -4.083  &   0.000   &   -7.677  &   -2.697  \\
vol     &   -10.0795    &   18.930  &   -0.532  &   0.594   &   -47.182 &   27.023  \\
rec     &   -0.8608     &   0.394   &   -2.183  &   0.029   &   -1.634  &   -0.088  \\
spread  &   0.3881      &   0.087   &   4.483   &   0.000   &   0.218   &   0.558   \\
DP      &   11.3808 &   2.526   &   4.505   &   0.000   &   6.429   &   16.332  \\
EP      &   -1.3759     &   1.721   &   -0.799  &   0.424   &   -4.750  &   1.998   \\

\end{tabular}

\end{table}


\end{document}
Sveinung
  • 20,355
3

My proposal is to equalize the widths. Alternating colors might seem appealing, but they're not really helpful. (Data taken from the other answers.)

\documentclass{article}
\usepackage{booktabs,siunitx}

\newsavebox{\toptabular}

\begin{document}

\begin{table}
\centering

\sbox{\toptabular}{%
  \begin{tabular}{ @{} l r @{\hspace{3em}} l r @{} }
  \toprule
  \multicolumn{4}{@{}l}{\bfseries Probit Regression Results} \\
  \midrule
  Dep.\@ Variable &             spi1   &   No.\@ Observations &       \num{254} \\
  Model           &           Probit   &   Df Residuals       &       \num{248} \\
  Methods         &              MLE   &   Df Model           &         \num{5} \\
  Date            & Thu, 16 Apr 2020   &   Pseudo R-squ.      &    \num{0.1311} \\
  Time            &         00:35:44   &   Log-Likelihood     &   \num{-149.65} \\
  Converge        &             True   &   LL-Null            &   \num{-172.23} \\
  Covariance Type &        nonrobust   &   LLR p-value        & \num{1.341e-08} \\
  \bottomrule
  \end{tabular}%
}
\usebox{\toptabular}

\bigskip

\begin{tabular*}{\wd\toptabular}{
  @{\extracolsep{\fill}}
  l
  S[table-format=-2.4]
  S[table-format=2.3]
  S[table-format=-1.3]
  S[table-format=1.3]
  S[table-format=-2.3,table-align-text-pre=false,table-space-text-pre={[}]
  S[table-format=-1.3,table-space-text-post={]}]% the minus also covers the second digit
  @{}
}
\toprule
       &   {coef} & {std err} &  {$z$} & {$P>|z|$} &  [0.025 &  0.975] \\
\midrule
const  &  -5.1870 &     1.270 & -4.083 &     0.000 &  -7.677 & -2.697  \\
vol    & -10.0795 &    18.930 & -0.532 &     0.594 & -47.182 & 27.023  \\
rec    &  -0.8608 &     0.394 & -2.183 &     0.029 &  -1.634 & -0.088  \\
spread &   0.3881 &     0.087 &  4.483 &     0.000 &   0.218 &  0.558  \\
DP     &  11.3808 &     2.526 &  4.505 &     0.000 &   6.429 & 16.332  \\
EP     &  -1.3759 &     1.721 & -0.799 &     0.424 &  -4.750 &  1.998  \\
\bottomrule
\end{tabular*}

\end{table}

\end{document}

enter image description here

egreg
  • 1,121,712
1

Are you looking for something like this?

\documentclass[a4paper,12pt]{article}
\usepackage{fontspec}
\setmainfont{Calibri}
\begin{document}
\begin{tabular}{r r r r} 
    \textbf{Dep. Veriable:} & spi\textbf{1} & \textbf{No. Observation:} & 254\\ 
    \textbf{Model:} & Probit & \textbf{Df Residual:} & 248\\ 
    \textbf{Method:} & MLE & \textbf{Df Model:} & 5\\ 
    \textbf{Date:} & Thu, 16 Apr 2020 & \textbf{Pseudo R-squ:} & 0.1311\\
    \textbf{Time:} & 00:35:44 & \textbf{Log-Likelihood:} & -149.65\\ 
    \textbf{Converged:} & True & \textbf{LL-Null:} & -172.23\\
    \textbf{Convariance Type:} & nonorobust & \textbf{LLR p-value:} & 1.31e-08
\end{tabular}
\end{document}

enter image description here

Soyeb Jim
  • 475
1

Another way is to convert your DataFrame into LaTeX. This is more of a Python / Pandas solution, but in your case it would probably be the easiest way.

Assuming you are using a Pandas DataFrame which is called 'df', this would be your solution in Jupyter Notebook (this is Python, not LaTeX!):

print(df.to_latex())

This would give you the LaTeX Code, which would look like something similar to this (this is LaTeX!):

\begin{tabular}{llllr}
\toprule
{} &                  0 &                1 &                2 &             3 \\
\midrule
0 &     Dep. Veriable: &             spi1 &  No.Observation: &  2.540000e+02 \\
1 &             Model: &           Probit &     Df Residual: &  2.480000e+02 \\
2 &            Method: &              MLE &        Df Model: &  5.000000e+00 \\
3 &              Date: &  Thu 16 Apr 2020 &    Pseudo R-squ: &  1.311000e-01 \\
4 &              Time: &         00:35:44 &  Log-Likelihood: & -1.496500e+02 \\
5 &         Converged: &             True &         LL-Null: & -1.722300e+02 \\
6 &  Convariance Type: &       nonorobust &     LLR p-value: &  1.310000e-08 \\
\bottomrule
\end{tabular}

And if you add this LaTeX Code to your document, it would look similar to this as an end result:

enter image description here

owmal
  • 325
1

I modified Sveinung's answer to use booktabs and siunitx. First, the layout:

\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}

\newcolumntype{B}{>{\bfseries}r}
\newcolumntype{R}{>{$}r<{$}}

\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}

\usepackage{booktabs}
\usepackage{siunitx}

\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}

\begin{document}
\centering
\begin{table}
  \begin{tabular}{BrBR}
    \multicolumn{4}{c}{Probit Regression Results}\\
    \toprule
Dep. Variable:      &               spi1    &   No. Observations:   &   \tablenum[table-figures-integer = 3]{254}         \\
        Model:      &               Probit  &       Df Residuals:   &   \tablenum[table-figures-integer = 3]{248}         \\
      Methods:      &               MLE &           Df Model:   &   \tablenum[table-figures-integer = 3]{5}           \\
         Date:      &   Thu, 16 Apr 2020    &   Pseudo R-squ.:      &   \tablenum[table-figures-integer = 3]{0.1311}      \\
         Time:      &           00:35:44    &   Log-Likelihood:     &   \tablenum[table-figures-integer = 3]{-149.65}     \\
    converge:       &           True        &           LL.Null:        &   \tablenum[table-figures-integer = 3]{-172.23}     \\
Covariance Type:    &       nonrobust       &       LLR p-value     &   \tablenum[table-figures-integer = 3]{1.341e-08}   \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{lSSSSSS}
\toprule
  &   \multicolumn{1}{c}{coef}        &   \multicolumn{1}{c}{std err} &       \multicolumn{1}{c}{z}   &   \multicolumn{1}{c}{P\pz}    &   \multicolumn{1}{c}{[0.025}  &   \multicolumn{1}{c}{0.975]}  \\
  \midrule
const   &   -5.1870     &   1.270   &   -4.083  &   0.000   &   -7.677  &   -2.697  \\
vol     &   -10.0795    &   18.930  &   -0.532  &   0.594   &   -47.182 &   27.023  \\
rec     &   -0.8608     &   0.394   &   -2.183  &   0.029   &   -1.634  &   -0.088  \\
spread  &   0.3881      &   0.087   &   4.483   &   0.000   &   0.218   &   0.558   \\
DP      &   11.3808 &   2.526   &   4.505   &   0.000   &   6.429   &   16.332  \\
EP      &   -1.3759     &   1.721   &   -0.799  &   0.424   &   -4.750  &   1.998   \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

screenshot of a b/w solution

The first table shows that you can integrate siunitx into an arbitrary table, the second table shows that you can use siunitx's own column type for better and easier typesetting. I also support the idea to generate the basic table programmatically, it's a nice start, but you'd want some more styling which is mostly done manually, except the case when you have hundreds of such tables.

Now, let's add color, even if it is seems less professional:

\documentclass{article}
\usepackage{array}
\usepackage{lmodern}
\usepackage[svgnames, table]{xcolor}

\newcolumntype{B}{>{\bfseries}r}
\newcolumntype{R}{>{$}r<{$}}

\setlength{\extrarowheight}{2pt}
\renewcommand{\arraystretch}{1.2}

\usepackage{booktabs}
\usepackage{siunitx}

\newcommand{\thead}[1]{\multicolumn{1}{r}{\bfseries #1}}
\newcommand{\pz}{\boldmath{$<|z|$}}

\begin{document}
\centering
\begin{table}
  \begin{tabular}{BrBR}
    \multicolumn{4}{c}{Probit Regression Results}\\
    \toprule
Dep. Variable:      &               spi1    &   No. Observations:   &   \tablenum[table-figures-integer = 3]{254}         \\
        Model:      &               Probit  &       Df Residuals:   &   \tablenum[table-figures-integer = 3]{248}         \\
      Methods:      &               MLE &           Df Model:   &   \tablenum[color=red,table-figures-integer = 3]{5}           \\
         Date:      &   Thu, 16 Apr 2020    &   Pseudo R-squ.:      &   \tablenum[table-figures-integer = 3]{0.1311}      \\
         Time:      &           00:35:44    &   Log-Likelihood:     &   \tablenum[color=green,table-figures-integer = 3]{-149.65}     \\
    converge:       &           True        &           LL.Null:        &   \tablenum[negative-color = red,table-figures-integer = 3]{-172.23}     \\
Covariance Type:    &       nonrobust       &       LLR p-value     &   \tablenum[negative-color = red,table-figures-integer = 3]{1.341e-08}   \\
\bottomrule
\end{tabular}

\bigskip

\begin{tabular}{lS[color=purple]SS[color=orange]SS[color=blue]S}
\toprule
  &   \multicolumn{1}{c}{coef}        &   \multicolumn{1}{c}{std err} &       \multicolumn{1}{c}{z}   &   \multicolumn{1}{c}{P\pz}    &   \multicolumn{1}{c}{[0.025}  &   \multicolumn{1}{c}{0.975]}  \\
  \midrule
const   &   -5.1870     &   1.270   &   -4.083  &   0.000   &   -7.677  &   -2.697  \\
vol     &   -10.0795    &   18.930  &   -0.532  &   0.594   &   -47.182 &   27.023  \\
rec     &   -0.8608     &   0.394   &   -2.183  &   0.029   &   -1.634  &   -0.088  \\
spread  &   0.3881      &   0.087   &   4.483   &   0.000   &   0.218   &   0.558   \\
DP      &   11.3808 &   2.526   &   4.505   &   0.000   &   6.429   &   16.332  \\
EP      &   -1.3759     &   1.721   &   -0.799  &   0.424   &   -4.750  &   1.998   \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

screenshot with color

Obviously, colors can be specified "globally" for each column or individually, or based on some conditions in siunitx.