2

How do I right align in the following example, given that I am using p{} for column widths?

Ideally I'd like the right hand side four columns to be right aligned, and the others left aligned.

I can get a workaround, using \multicolumn{1}{r}{...} for each individual cell, but it's a bit long tbf...

Cheers

\begin{table}[htbp!]
  \centering
  \captionsetup{justification=centering}
  \caption*{\textbf{Table 1.1: Summary Statistics}}
  \vspace{-10pt}
  \begin{threeparttable}
    \begin{tabular}{p{1.5cm} p{2cm} p{2cm} p{1.5cm} p{2cm} p{1.8cm} p{1.8cm}}
    \hline\hline
             &              &                 &           &           &         &           \\
    Variable & Number of    & Unit of         &           & Standard  & Minimum & Maximum   \\
    name     & observations & observation     & Mean      & deviation & value   & value     \\
    \hline
    \hline\hline
    \end{tabular}
  \end{threeparttable}
\end{table}
Torbjørn T.
  • 206,688
eBopBob
  • 93
  • 2
  • 9
  • Welcome to TeX.SE!. Please help us help you and add to your code the preamble with necessary packages. 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}. – Cragfelt Dec 06 '17 at 10:38
  • 3
    Why not just use an r specification for these columns? – Andrew Swann Dec 06 '17 at 10:40
  • If \multicolumn is a real alternative, then Andrew's suggestion is the most logical. Otherwise: https://tex.stackexchange.com/questions/338009/right-alignment-for-plength-box-in-tabular – Torbjørn T. Dec 06 '17 at 10:42
  • Thanks. I thought, though, that using {r} instead of p{2cm} for example doesn't then force the column to be 2cm, for example?

    Do you mean replace p{2cm} with r?

    – eBopBob Dec 06 '17 at 10:55
  • 1
    That is correct, with r instead of p{2cm}, the column will have the width of the widest cell in it. Did you look at the link I posted though? – Torbjørn T. Dec 06 '17 at 11:47
  • 1
    A separate issue: You're not using the threeparttable environment properly; it should include the \caption* statement. – Mico Dec 06 '17 at 16:15

1 Answers1

3

I would like to suggest that you use a tabularx environment, with a width set to \textwidth. Left- and right-alignment may be achieved by definining suitably modified versions of the X column type. Among other things, such a setup greatly simplifies inputting of cell contents that require line-breaks.

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage{geometry} % set page parameters suitably
\usepackage{threeparttable,caption,booktabs}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}

\begin{document} \begin{table}[htbp] \setlength\tabcolsep{3pt} % default value: 6pt \captionsetup{justification=centering,skip=0.25\baselineskip} \begin{threeparttable} \caption*{\textbf{Table 1.1: Summary Statistics}} \begin{tabularx}{\textwidth}{@{} LLL RRRR @{}} \toprule Variable Name & Number of observations & Unit of observation & Mean & Standard deviation & Minimum value & Maximum value \ \midrule bla & bla & bla & blu & blu & blu & blu \ \bottomrule \end{tabularx} \end{threeparttable} \end{table} \end{document}

Danferno
  • 105
Mico
  • 506,678
  • What is the purpose of the @{} in the tabularx definition? Removing these made no changes to my table – Danferno Dec 13 '21 at 11:11
  • 1
    @Danferno It suppresses leading space. See, there is no space before 'Variable Name' or after 'Maximum Value' in the added image. If you remove those, then there will be spaces there. – Imran Dec 13 '21 at 12:09
  • Leading space as in the margins? – Danferno Dec 13 '21 at 15:13
  • 1
    @Danferno I am not sure what you meant with the comment. You may take a look at this https://imgur.com/3TknIqT – Imran Dec 13 '21 at 17:29
  • 1
    @Danferno - Not sure what you mean by "made no changes to my table". For sure, including the @{} particles should have removed the whitespace padding at the left-hand and right-hand edges of the table. (That's what user Imran meant by "suppress leading space".) – Mico Dec 13 '21 at 17:47