6

I'm looking for a way to align the numbers of the table by decimals. Any thoughts of what could be wrong?

\documentclass[12pt,a4paper]{article}
\usepackage{apacite}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{psfrag}
\usepackage{amssymb}
\usepackage[left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
\usepackage{graphicx}
\usepackage{float}
\usepackage[flushleft]{threeparttable}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage[english,spanish]{babel} 
\usepackage{mathptmx}
\usepackage{setspace}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{\cdot}{#1}}
\begin{document}
\begin{tabular}{l*{1}{ccccc}}
    \multicolumn{5}{c}{Cuadro I}\\
    \multicolumn{5}{c}{Estadísticas descriptivas GEIH} \\
    \hline\hline
    &\multicolumn{5}{c}{}                                            \\
    &           Obs&        Media&          Desviación Estándar&         Min&         Máx\\
    \hline
    Ocupados    &   96703         &  .896&    .306&           0&           1\\
    Años de educación         &  96703          &    10.583&    4.331&           0&          26\\
    Soltero     &  96703          &    .281&     .449&           0&           1\\
    Jefe de hogar  &  96703          &    .435&    .496&           0&           1\\
    Edad       &    96703        &    39.301&    13.824&          18&         100\\
    \hline
    Ocupados según nivel de estudio       &      &            &            &            &            \\
    Ninguno     & 586599            &    .546&    .498&           0&           1\\
    Bachiller   &  586599          &    .203&    .402&           0&           1\\
    Técnico    &  586599          &    .161&    .367&           0&           1\\
    Universitario& 586599           &    .065&    .246&           0&           1\\
    Postgrado   &  586599          &    .000&    .007&           0&           1\\
    \hline
\end{tabular}
\end{document}
4
  • Sorry I forgot to include the d column type, now is there. May 5, 2020 at 14:49
  • it's always easier for people to see the issue and test answers if you priovide a complete test document not a fragment as above which needs to be edited to make run May 5, 2020 at 14:56
  • Actually I get a text error for \begin{tabular}{l*{1}{ccccc}}{d{3.2}}\multicolumn{5 the d{3.2} May 5, 2020 at 15:06
  • the error is because that is typesetting d3.2 in the first cell, so \multicolumn comes too late, you intended the d column to be inside the first argument, bot after it. May 5, 2020 at 15:17

3 Answers 3

6

You declared the d type but did not use it, something like

enter image description here

\documentclass[12pt,a4paper]{article}

% use utf-8 \usepackage[latin1]{inputenc}
\usepackage{dcolumn}
\newcolumntype{d}[1]{D{.}{\cdot}{#1}}
\begin{document}
% l*{1}{ccccc} is the same as lccccc and doesn't use d!

\begin{center}
\begin{tabular}{@{}l d{6.0} d{2.3} d{2.3} d{2.0} d{3.0} @{}}
    \multicolumn{6}{c}{Cuadro I}\\
    \multicolumn{6}{c}{Estadísticas descriptivas GEIH} \\
    \hline\hline
\\
\multicolumn{1}{c}{Obs}& 
\multicolumn{1}{c}{Media}&
\multicolumn{1}{c}{\begin{tabular}{@{}c@{}}Desviación\\Estándar\end{tabular}}&
\multicolumn{1}{c}{Min}&
\multicolumn{1}{c}{Máx}\\
    \hline
    Ocupados    &   96703         &  .896&    .306&           0&           1\\
    Años de educación         &  96703          &    10.583&    4.331&           0&          26\\
    Soltero     &  96703          &    .281&     .449&           0&           1\\
    Jefe de hogar  &  96703          &    .435&    .496&           0&           1\\
    Edad       &    96703        &    39.301&    13.824&          18&         100\\
    \hline
    \multicolumn{6}{@{}c@{}}{Ocupados según nivel de estudio}\\
    Ninguno     & 586599            &    .546&    .498&           0&           1\\
    Bachiller   &  586599          &    .203&    .402&           0&           1\\
    Técnico    &  586599          &    .161&    .367&           0&           1\\
    Universitario& 586599           &    .065&    .246&           0&           1\\
    Postgrado   &  586599          &    .000&    .007&           0&           1\\
    \hline
\end{tabular}

\end{center}

\end{document}

but the horizontal rules would look better if you used \midrule from the booktabs package.

17
  • a prefix \cdot with no preceding 0 looks a lot like a - are you sure you want this format? May 5, 2020 at 15:20
  • How can I change the prefix \cdot to look like decimal, instead of - ? @DavidCarlisle May 5, 2020 at 15:25
  • 1
    @FelipeTorresCruz use . (or perhaps , since this is Spanish?) instead of \cdot May 5, 2020 at 15:27
  • Is spanish. Its weird when I use \newcolumntype{d}[1]{D{.}{.}{#1}} I get a text error but when i use \newcolumntype{d}[1]{D{.}{,}{#1}} it works. However I need . instead of the , . Any ideas of what could be the problem? Or how it could be solved? @DavidCarlisle May 5, 2020 at 15:34
  • 1
    same result as what? @FelipeTorresCruz May 5, 2020 at 15:56
3

Here's how I would present the table, with siunitx. This has several advantages, for instance automatic uniformization of the decimal separator and automatic formatting of large numbers.

Also numbers should always show the integer part and siunitx adds it automatically, if it is absent.

\documentclass[12pt,a4paper]{article}
\usepackage[spanish]{babel}
\usepackage{siunitx,booktabs}

\sisetup{output-decimal-marker={,}}

\begin{document}

\begin{table}[htp]
\centering

\caption{Estadísticas descriptivas GEIH}\label{add-here-a-label}

\medskip

\begin{tabular}{
  @{}
  l
  S[table-format=6.0]
  S[table-format=2.3]
  S[table-format=2.3]
  S[table-format=2.0]
  S[table-format=3.0]
  @{}
}
\toprule
& {Obs} & {Media} & {DE} & {Min} & {Máx} \\
\midrule
Ocupados          &  96703 &   .896 &   .306 &  0 &   1 \\
Años de educación &  96703 & 10.583 &  4.331 &  0 &  26 \\
Soltero           &  96703 &   .281 &   .449 &  0 &   1 \\
Jefe de hogar     &  96703 &   .435 &   .496 &  0 &   1 \\
Edad              &  96703 & 39.301 & 13.824 & 18 & 100 \\
\midrule
\multicolumn{6}{@{}l@{}}{\itshape Ocupados según nivel de estudio} \\
Ninguno           & 586599 &   .546 &   .498 &  0 &   1 \\
Bachiller         & 586599 &   .203 &   .402 &  0 &   1 \\
Técnico           & 586599 &   .161 &   .367 &  0 &   1 \\
Universitario     & 586599 &   .065 &   .246 &  0 &   1 \\
Postgrado         & 586599 &   .000 &   .007 &  0 &   1 \\
\bottomrule
\multicolumn{6}{@{}l@{}}{\footnotesize DE: Desviación Estándar} \\
\end{tabular}

\end{table}

\end{document}

enter image description here

4
  • Is there any way to change , for . ? @egreg May 5, 2020 at 15:52
  • @FelipeTorresCruz Remove the \sisetup line.
    – egreg
    May 5, 2020 at 15:56
  • I tried to use \usepackage{siunitx,booktabs} but i get the following error "File `l3backend-pdfmode.def' not found." @egreg May 5, 2020 at 16:00
  • Thank you for your help @egreg May 5, 2020 at 17:15
0

Some suggestions and comments, in no particular order:

  • You define the d column type but then don't seem to use it in the header of the tabular environment. I suggest that you replace

    \begin{tabular}{l*{1}{ccccc}}
    

    with

    \begin{tabular}{@{} l d{6.0} d{2.3} d{2.3} d{2.0} d{3.0} @{}}
    
  • Since the contents of the five header cells are not numeric, you need to smuggle them past dcolumn. See the use of \mc in the example below.

  • If you want to use commas as decimal separators in the pdf file, you should also change \newcolumntype{d}[1]{D{.}{\cdot}{#1}} to \newcolumntype{d}[1]{D{.}{,}{#1}}.

  • Use a table environment, and use the machinery of the caption package to customize the appearance of the captions. That way, you can cross-reference the table elsewhere in the document via LaTeX's \label-\ref system.

  • Replace

    \usepackage[left=2.5cm,right=2.5cm,top=2.5cm,bottom=2.5cm]{geometry}
    

    with

    \usepackage[margin=2.5cm]{geometry}
    
  • Replace

    Ocupados según nivel de estudio & & & & & \\
    

    with

    \multicolumn{6}{@{}l}{Ocupados según nivel de estudio} \\
    

    to give the table a much more compact appearance. Relatedly, do split Desviación Estándar across 2 lines.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathptmx}
\usepackage[english,spanish]{babel} 
\usepackage[margin=2.5cm]{geometry}

\usepackage{amsmath}
%%%\usepackage{amsfonts} % is loaded automatically by 'amssymb'
\usepackage{psfrag}  % are you sure you need this package?
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{float}
\usepackage{apacite}
\usepackage[flushleft]{threeparttable}
\usepackage{enumitem}
\usepackage{setspace}

\usepackage{dcolumn}
\newcolumntype{d}[2]{D{.}{,}{#1}}

%% new material:
\usepackage{booktabs}
\usepackage[labelsep=newline,skip=0.333\baselineskip]{caption}
\renewcommand\thetable{\Roman{table}}
\newcommand\mc[2]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\usepackage[colorlinks]{hyperref}
\usepackage[spanish,nameinlink]{cleveref} % optional

\begin{document}
\begin{table}[ht!]
\centering
\caption{Estadísticas descriptivas GEIH} \label{tab:GEIH}
\begin{tabular}{@{} l d{6.0} d{2.3} d{2.3} d{2.0} d{3.0} @{}}
\toprule
    & \mc{Obs}& \mc{Media} & \mc{Desviación}& \mc{Min} & \mc{Máx}\\
    &&& \mc{Estándar}\\
\midrule
    Ocupados          &  96703 &  0.896 &  0.306 &  0 &   1\\
    Años de educación &  96703 & 10.583 &  4.331 &  0 &  26\\
    Soltero           &  96703 &  0.281 &  0.449 &  0 &   1\\
    Jefe de hogar     &  96703 &  0.435 &  0.496 &  0 &   1\\
    Edad              &  96703 & 39.301 & 13.824 & 18 & 100\\
\midrule
    \multicolumn{6}{@{}l}{Ocupados según nivel de estudio} \\
    Ninguno           & 586599 &  0.546 &  0.498 &  0 &   1\\
    Bachiller         & 586599 &  0.203 &  0.402 &  0 &   1\\
    Técnico           & 586599 &  0.161 &  0.367 &  0 &   1\\
    Universitario     & 586599 &  0.065 &  0.246 &  0 &   1\\
    Postgrado         & 586599 &  0.000 &  0.007 &  0 &   1\\
\bottomrule
\end{tabular}
\end{table}

Una referencia cruzada al \cref{tab:GEIH}.
\end{document}
4
  • 1
    Mik text doesnt recognize some of the packages, so I get an error @Mico May 5, 2020 at 16:24
  • @FelipeTorresCruz all those packages are in an up to date miktex, you may need to update using miktex package manager (in user and admin modes if you hav eboth) May 5, 2020 at 16:52
  • Thank you for all your help @Mico May 5, 2020 at 17:16
  • @FelipeTorresCruz - Which packages aren't recognized?
    – Mico
    May 5, 2020 at 17:16

You must log in to answer this question.

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