1

I am just learning LaTex. I used stata and got the following

{
    \def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{3}{ccc}l}
\hline

\multicolumn{11}{l}{\textit{Panel A: }} \ \hline\hline &\multicolumn{3}{c}{Overall} &\multicolumn{3}{c}{Set Household} &\multicolumn{3}{c}{Non-set Household} & \multicolumn{1}{l}{T-test}\ & count& mean& sd& count& mean& sd& count& mean& sd &\ \hline Average Age of the household respondent & 5000& 44.7& 12.8& 3400& 44.4& 12.5& 1016& 44.9& 13.1& 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married & 4000& 0.86& 0.35& 345& 0.87& 0.34& 800& 0.85& 0.35& -0.014\ & & & & & & & & & & (-0.88) \ Number of family that are dependent in the income & 99& 0.83& 0.67& 94& 0.79& 0.70& 5000& 0.86& 0.65 & 0.070\sym{*} \ & & & & & & & & & & (2.29) \

\hline \multicolumn{11}{l}{\textit{Panel B: }} \ \hline\hline

\hline \multicolumn{11}{l}{\textit{Panel C: }} \ \hline\hline

\hline\hline \multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{2}{l}{\footnotesize \sym{} (p<0.05), \sym{} (p<0.01), \sym{**} (p<0.001)}\ \end{tabular} }

I edited the table and added Panel B and C later on as I generated new tables via stata. I am suing the following tex file to create my document as I have several tables

  \documentclass[a4paper]{article}

\usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{graphicx} \usepackage{booktabs} \usepackage{array} \usepackage{paralist} \usepackage{verbatim} \usepackage{amssymb} \usepackage{amsmath}

 \usepackage{setspace}
 \onehalfspacing
 \usepackage[parfill]{parskip}


%--------------------------------------- Document --------------------------------------%

\title{\huge{}}
 \author{}
  \date{}

  \begin{document}
   \maketitle
    \section*{Introduction}


       \begin{table}[h]
       \centering
       \caption{ }
       \input{d.tex}
        \end{table}

        \begin{table}[h]
         \centering
         \caption{}
         \input{m.tex}
          \end{table}

           \end{document}

I have tried the following codes

1st 
  \begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}| |p{3cm}||p{3cm}|p{3cm}|p{3cm}|     |p{3cm}||p{3cm}|}
    2nd 
                 \begin{tabular}{|l*{3}{ccc}l|}
  3rd
             \\resizebox{\textwidth}{p{3cm}}{\begin{tabular}{l*{3}{ccc}l}
   4th
           \begin{tabular}{\linewidth}{l*{3}{ccc}l}

but i got error and/or it did not solve my issue and the table still spilled out of the page. I hope someone here can help me resolve this issue i would be really thankful.

newbie
  • 11
  • Since you used 10 columns, that are 3 cm wide each, your table is wider than 30cm. As a start, you could reduce the width of the columns. – leandriis Jul 31 '20 at 16:12
  • Also, please don't use resizebox on a table as thiw will only lead to inconsistent font sizes. – leandriis Jul 31 '20 at 16:18
  • If it has no importance for your question, you should collect everything in one file and only include relevant code. Have a look at beautiful tables: https://tex.stackexchange.com/questions/112343/beautiful-table-samples . I believe that your table is for scientific purpose, so do not use any colors, fancy bordes and important: Keep the number of lines to a minimum - No vertical lines. – hpekristiansen Jul 31 '20 at 16:18
  • Hi thank you for your comment but i am so new to latex i dont know how to reduce the width of the columns or how to resize or set the width of the columns. I tried using { |p{3cm}||p{3cm}|p{3cm}|p{3cm}| |p{3cm}||p{3cm}|p{3cm}|p{3cm}| |p{3cm}||p{3cm}|} but this gave me an error. I know i should set the width in \begin{tabular}{l*{3}{ccc}l} but i am not sure exactly how to do it. – newbie Jul 31 '20 at 16:18

2 Answers2

1

enter image description here

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{graphicx} \usepackage{booktabs} \usepackage{array} \usepackage{paralist} \usepackage{verbatim} \usepackage{amssymb} \usepackage{amsmath}

\usepackage{setspace} \onehalfspacing \usepackage[parfill]{parskip}

\def\sym#1{\ifmmode^{#1}\else(^{#1})\fi}

\usepackage{siunitx} % for improved alignment of numbers in tables using the S type column \usepackage{tabularx} % to make sure the table is as wide as the textwidth, for teh X type column \usepackage{booktabs} % for horizontal lines with improved spacing \usepackage{geometry} % for smaller margins, otherwise the table wouldn't fit \usepackage{multirow} % for the multirow command %--------------------------------------- Document --------------------------------------%

\begin{document}

\begin{table}[h] \small \setlength{\tabcolsep}{4pt} \centering \caption{ } \begin{tabularx}{\linewidth}{@{}X {3}{ S[table-format=4] S[table-format=2.2] S[table-format=2.2] } S[table-format=-1.3, input-symbols = {()}, table-space-text-pre=), table-space-text-post=\sym{}, table-align-text-post=false, ]@{}} \toprule \multicolumn{11}{@{}l@{}}{\textit{Panel A: }} \ \midrule & \multicolumn{3}{c}{Overall} & \multicolumn{3}{c}{Set Household} & \multicolumn{3}{c}{Non-set Household} & \multicolumn{1}{l}{T-test}\ \cmidrule(r){2-4} \cmidrule(lr){5-7} \cmidrule(lr){8-10} & {count} & {mean} & {sd} & {count} & {mean} & {sd} & {count} & {mean} &{sd} & \ \midrule \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \multirow[t]{2}{=}{Number of family that are dependent in the income}
& 99 & 0.83 & 0.67 & 94 & 0.79 & 0.70 & 5000 & 0.86 & 0.65 & 0.070\sym{*} \ & & & & & & & & & & (2.29) \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel B: }} \ \midrule

\midrule \multicolumn{11}{@{}l@{}}{\textit{Panel C: }} \ \midrule

\bottomrule \multicolumn{11}{@{}l@{}}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{11}{@{}l@{}}{\footnotesize \sym{} (p<0.05), \sym{} (p<0.01), \sym{**} (p<0.001)}\ \end{tabularx} \end{table} \end{document}

Since you mentioned in the comments, that Panel B and Panel C will contain entries belonging to the same columns as the entries in panel A, I would suggest the following slightly different layout versions. In both, I have places "Panel A" belos the column headers in order to avoid repeating them for every panel. In order to visually separate them "Panel x" from the surrounding table, I have either used two horizontal lnes or a ombination of indentation and small vertical white space:

enter image description here

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc} \usepackage[english]{babel} \usepackage{graphicx} \usepackage{booktabs} \usepackage{array} \usepackage{paralist} \usepackage{verbatim} \usepackage{amssymb} \usepackage{amsmath}

\usepackage{setspace} \onehalfspacing \usepackage[parfill]{parskip}

\def\sym#1{\ifmmode^{#1}\else(^{#1})\fi}

\usepackage{siunitx} % for improved alignment of numbers in tables using the S type column \usepackage{tabularx} % to make sure the table is as wide as the textwidth, for teh X type column \usepackage{booktabs} % for horizontal lines with improved spacing \usepackage{geometry} % for smaller margins, otherwise the table wouldn't fit \usepackage{multirow} % for the multirow command %--------------------------------------- Document --------------------------------------%

\begin{document}

\begin{table}[h] \small \setlength{\tabcolsep}{4pt} \centering \caption{ } \begin{tabularx}{\linewidth}{@{}X {3}{ S[table-format=4] S[table-format=2.2] S[table-format=2.2] } S[table-format=-1.3, input-symbols = {()}, table-space-text-pre=), table-space-text-post=\sym{}, table-align-text-post=false, ]@{}} \toprule & \multicolumn{3}{c}{Overall} & \multicolumn{3}{c}{Set Household} & \multicolumn{3}{c}{Non-set Household} & \multicolumn{1}{l}{T-test}\ \cmidrule(r){2-4} \cmidrule(lr){5-7} \cmidrule(lr){8-10} & {count} & {mean} & {sd} & {count} & {mean} & {sd} & {count} & {mean} &{sd} & \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel A: }} \ \midrule \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \multirow[t]{2}{=}{Number of family that are dependent in the income}
& 99 & 0.83 & 0.67 & 94 & 0.79 & 0.70 & 5000 & 0.86 & 0.65 & 0.070\sym{} \ & & & & & & & & & & (2.29) \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel B: }} \ \midrule \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel C: }} \ \midrule \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \bottomrule \multicolumn{11}{@{}l@{}}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{11}{@{}l@{}}{\footnotesize \sym{
} (p<0.05), \sym{} (p<0.01), \sym{*} (p<0.001)}\ \end{tabularx} \end{table}

\begin{table}[h] \small \setlength{\tabcolsep}{3.5pt} \centering \caption{ } \begin{tabularx}{\linewidth}{X {3}{ S[table-format=4] S[table-format=2.2] S[table-format=2.2] } S[table-format=-1.3, input-symbols = {()}, table-space-text-pre=), table-space-text-post=\sym{}, table-align-text-post=false, ]@{}} \toprule & \multicolumn{3}{c}{Overall} & \multicolumn{3}{c}{Set Household} & \multicolumn{3}{c}{Non-set Household} & \multicolumn{1}{l}{T-test}\ \cmidrule(r){2-4} \cmidrule(lr){5-7} \cmidrule(lr){8-10} & {count} & {mean} & {sd} & {count} & {mean} & {sd} & {count} & {mean} &{sd} & \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel A: }} \ \addlinespace \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \multirow[t]{2}{=}{Number of family that are dependent in the income}
& 99 & 0.83 & 0.67 & 94 & 0.79 & 0.70 & 5000 & 0.86 & 0.65 & 0.070\sym{} \ & & & & & & & & & & (2.29) \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel B: }} \ \addlinespace \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \midrule \multicolumn{11}{@{}l@{}}{\textit{Panel C: }} \ \addlinespace \multirow[t]{2}{=}{Average Age of the household respondent} & 5000 & 44.7 & 12.8 & 3400 & 44.4 & 12.5 & 1016 & 44.9 & 13.1 & 0.511 \ & & & & & & & & & & (0.87) \ Percentage of Married
& 4000 & 0.86 & 0.35 & 345 & 0.87 & 0.34 & 800 & 0.85 & 0.35 & -0.014 \ & & & & & & & & & & {(}-0.88) \ \bottomrule \multicolumn{11}{@{}l@{}}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{11}{@{}l@{}}{\footnotesize \sym{
} (p<0.05), \sym{} (p<0.01), \sym{*} (p<0.001)}\ \end{tabularx} \end{table} \end{document}

leandriis
  • 62,593
  • Thank you so much for your response. I have two different files one with codes generated by stata and another my report document should i write the code after \def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} in my tables code file or in my other file. I am a little bit confused. Looking forward to your advice. this command in the document where i have the report or in table – newbie Jul 31 '20 at 20:43
  • Do you refer to the 5 \usepackage commands I added? They belong to the preamble of your report document. – leandriis Jul 31 '20 at 21:01
  • If you want to keep the contents of the table in an external file, place \begin{tabularx}.....\end{tabularx} in the file mytable.tex and use \begin{table}[h] \small \setlength{\tabcolsep}{4pt} \centering \caption{ }\input{mytable.tex}\end{table} in your report document. You can of course alter the name of the file to suit your needs. – leandriis Jul 31 '20 at 21:07
  • What kind of contents will the cells in "Panel B" and "Panel C" contain? Will they also contain data corresponding teh the columns in panel A or will you have different column names in the other two panels? Depending on the contents, some changes in the code might be needed. – leandriis Jul 31 '20 at 21:09
  • It has the same structure as panel A "overall" "set HH" "non-set HH" "ttest" and same options below count mean sd. I will follow your advice and redo. Thank you. – newbie Aug 01 '20 at 00:09
  • I was wondering if it is possible for me to format \begin{tabular}{l*{3}{ccc}l} code and resize the width of the columns as i have many tables and i don't want to edit each .Thank you so much for your help! I appreciate it very much. – newbie Aug 03 '20 at 14:05
  • @1049: Resizing a table as in changing the scale of the whole table is generally a bad idea as this will lead to inconsistent font sizes, especially if you hae multiple tables throughout your document. In my example, I have used tabularx and an X type column. The width of the X type column automatically adapts to \linewidth minus the sum of all other column's widths. – leandriis Aug 03 '20 at 15:31
  • The S type columns and espacially the values of table-format should be adapted to each table individually. If you have the same number of integer and decimal places in all tables in your document as you have in the table in your question, you can use \begin{tabularx}{\linewidth}{@{}X*{3}{ S[table-format=4] S[table-format=2.2]S[table-format=2.2] } S[table-format=-1.3, input-symbols = {()}, table-space-text-pre=), table-space-text-post=\sym{*}, table-align-text-post=false, ]@{}} for all other tables as well. – leandriis Aug 03 '20 at 15:32
  • Thank you so much for all your advice they have been very helpful!! – newbie Aug 09 '20 at 16:01
1

Here is a proposition with makecell, multirow, booktabs and geometry, reducing slightly the font size and the intercolumn spacing:

\documentclass[a4paper]{article}

\usepackage[showframe]{geometry} \usepackage[english]{babel} \usepackage{booktabs} \usepackage{array, makecell, multirow}% \usepackage{siunitx} \usepackage{amssymb}

\usepackage{setspace} \onehalfspacing \usepackage[parfill]{parskip}

\begin{document}

{ \def\sym#1{\ifmmode^{#1}\else(^{#1})\fi} \setlength{\tabcolsep}{3pt} \renewcommand{\theadset}{\renewcommand{\arraystretch}{0.6}} \renewcommand{\theadalign}{lc} \renewcommand{\theadfont}{\small} \sisetup{table-format=2.2, table-number-alignment=center} \small \begin{center} \begin{tabular}{@{}l{3}{S[table-format=4.0]SS}S[table-format=-1.3, table-space-text-post=\sym{}]} \toprule \multicolumn{11}{l}{\textit{Panel A: }} \ \midrule\midrule &\multicolumn{3}{c}{Overall} &\multicolumn{3}{c}{Set Household} &\multicolumn{3}{c}{Non-set Household} & {\multirowcell{2}{T-test}}\ \cmidrule(lr){2-4}\cmidrule(r){5-7}\cmidrule(r){8-10} & {count} & {mean} & {sd} & {count} & {mean} & {sd} & {count} & {mean} & {sd}&\ \midrule \multirowthead{2}{Average Age of the \ household respondent} & 5000& 44.7& 12.8& 3400& 44.4& 12.5& 1016& 44.9& 13.1& 0.511 \ & & & & & & & & & &{$ (0.87) $} \ \addlinespace \thead { Percentage of Married} & 4000& 0.86& 0.35& 345& 0.87& 0.34& 800& 0.85& 0.35& -0.014\ & & & & & & & & & & {$ (-0.88) $} \ \addlinespace \multirowthead{2}{Number of families that are\ dependent in the income} & 99& 0.83& 0.67& 94& 0.79& 0.70& 5000& 0.86& 0.65 & 0.070\sym{*} \ & & & & & & & & & &{$ (2.29) $} \ \addlinespace[2ex] \midrule \multicolumn{11}{l}{\textit{Panel B: }} \ \midrule\midrule \ \midrule \multicolumn{11}{l}{\textit{Panel C: }} \ \midrule\midrule \

\bottomrule \multicolumn{3}{@{}l}{\footnotesize \textit{t} statistics in parentheses}\ \multicolumn{3}{@{}l}{\footnotesize \sym{} (p<0.05), \sym{} (p<0.01), \sym{**} (p<0.001)}\ \end{tabular} \end{center} }

\end{document}

enter image description here

Bernard
  • 271,350
  • Thank you so much for your response. When i try your code it says siunitx not found so i removed it but then i got another error message theadset undefined. If i copy and paste it as it is it works but maybe since i have two files one with the table codes generated by stata and one document file where i am writing my report. it does not work. I am not sure maybe you can advice me. thank you again – newbie Jul 31 '20 at 20:39
  • siunitx not found’ means it's not installed on your system, as probably makecell. Which distribution do you have? – Bernard Jul 31 '20 at 20:42
  • I have MikTeX. How do i install it? I am sorry for asking basic questions. i just started using it so i dont know much. When i ran the exact same you have above it worked and did not say siunitx not found. thank you. – newbie Jul 31 '20 at 20:55
  • You have a MiKTeX Package Manager in the console.Select the missing packages in the list and ask to install them. In MiKTeX Settings you also have the possibility to make it install missing packages on the fly, possibly without asking you the permission. – Bernard Jul 31 '20 at 21:00
  • Thank you. i will do as your advice. – newbie Jul 31 '20 at 21:01
  • If you meet any problem, feel free to ask. – Bernard Jul 31 '20 at 21:04