2

I am just learning how to use LaTex so please excuse my newbie question. I am trying to create a table, that I exported from excel and everything works except that my titles for the columns are too long so that they do not fit the page. I tried the p{3cm} code and also \textwidth but neither of them work. Every tip would be very much appreciated.

Also a general question: what is [htbp] in \begin{table}[htbp] for?

This is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\begin{document}
\begin{table}[htbp]
  \centering
  \setlength{\tabcolsep}{0.75cm}
  \caption{Descriptive Table}
   \begin{tabular}{lrr}
     \toprule
    Variable & \multicolumn{1}{l}{Imputed and Weighted Means} & \multicolumn{1}{l}{Standard deviation} \\
    \midrule
    Distress & 3.18  & 0.04 \\
    Life satisfaction & 7.27  & 0.04 \\
    Age   & 0     & 0.01 \\
    Health & 0     & 0.01 \\
    Resilience & 3.67  & 0.13 \\
    Self esteem & -0.01 & 0.02 \\
    Arrger & 0.01  & 0.02 \\
    Course participation & 0.01  & 0.02 \\
    Time countor & 0     & 0.01 \\
    Time ger & 0     & 0.01 \\
    Time oth & 0     & 0.01 \\
    Meanger & 0     & 0.01 \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%
\end{document}
0

1 Answer 1

1

You are already following a lot of best practices here (e.g. booktabs). Just a few adjustments needed:

  • I loaded 'siunitx' to get the 'S' columntype for proper decimal alignment.

  • Remove your \setlength{\tabcolsep}{0.75cm}. \tabcolsep is the space between columns, and this was adding a lot of unneeded extra space.

  • For the labels, I showed two different techniques. Technique 1: shorten the label! So perhaps standard deviation can just become sigma: everyone will know what that means. This is the best way: short headings. Technique 2: set the relevant label in a \parbox{}{} of appropriate length. This isn't actually different from using a p column really, but it enables us to have that for the header row only, so that we can use the appropriate S type for the rest.

Code as follows:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{booktabs}
\usepackage{siunitx}
\begin{document}
\begin{table}[htbp]
  \centering
  \caption{Descriptive Table}
   \begin{tabular}{lSS}
     \toprule
     Variable &
     \multicolumn{1}{c}{\parbox{3cm}{\raggedright Imputed and weighted mean}} &
     \multicolumn{1}{c}{$\sigma$} \\
    \midrule
    Distress             & 3.18  & 0.04 \\
    Life satisfaction    & 7.27  & 0.04 \\
    Age                  & 0     & 0.01 \\
    Health               & 0     & 0.01 \\
    Resilience           & 3.67  & 0.13 \\
    Self esteem          & -0.01 & 0.02 \\
    Arrger               & 0.01  & 0.02 \\
    Course participation & 0.01  & 0.02 \\
    Time countor         & 0     & 0.01 \\
    Time ger             & 0     & 0.01 \\
    Time oth             & 0     & 0.01 \\
    Meanger              & 0     & 0.01 \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%
\end{document}

Result:

table image

For your other question, those letters are ways of telling LaTeX where it may put the table, which is a "float" (i.e. is placed separately from the text, not necessarily where it appears in the manuscript). There is a good explanation of this, which is not at all intuitive, in answers to this question.

PS: When working with tables you will do yourself a big favour spending some time getting the code nicely aligned. It's horrible editing a table or checking it when the code is a mess.

3
  • Using \thead from the makecell package would save you to guess the width of the \parbox for the second column head.
    – Bernard
    Commented Apr 21, 2018 at 11:57
  • Thanks a lot for your help! Is there a way to sill let the columns be right adjusted when using the S type?
    – Julia
    Commented Apr 21, 2018 at 18:25
  • I don't think so. The whole point of S is to align on the decimal, which is correct and makes the table much easier to read. I suppose if you added padding decimals they would align! Commented Apr 23, 2018 at 13:36

You must log in to answer this question.

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