1

I am trying to make a table with multiline values in a cell. Following is the MWE:

\documentclass[12pt]{article}
\usepackage[a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{booktabs}
\usepackage{multirow}

\pagestyle{empty}

\begin{document}

\begin{table}[ht]
\centering
\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tabular}{p{3.8cm}p{2cm}p{4cm}p{5cm}}
\hline
 {\bf Study} & {\bf Study} & {\bf Lead time}  & {\bf Methodology for}\\
{\bf location} & {\bf Period} & {\bf (days)} &  {\bf qualifying lead time.}\\
\hline 
City 1, Country 1 & Jun--Aug 2020 & 2 & \textit{Method 1}: attribute 1 vs attribute 2 \\
City 2, Country 2 (detail A, detail b) & Aug 2020--Jan 2021 &
0 to 6 (different sublocations) & \textit{Method 2}: attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
\hline
\end{tabular}
\end{table}

\end{document}

The output has issues and is as follows: enter image description here

Instead of the above, I want to obtain a neat-looking table with the following features:

  1. Table spanning the text (line) width
  2. Reasonable spacing between the table caption and the body
  3. Word wrapping wherever the content goes beyond column width (including the table headers).
  4. First column: I want the city and the country in separate lines. If there is additional text, it should come further below with word wrapping.
  5. Left justification for everything (I am okay if we have centre-justified column headers)
  6. No underfull/overfull warnings
  7. Sufficient space between the rows (The above table looks bad if we add more rows)

An example of a desirable table (generated in MS-Word), but without caption, looks like this: enter image description here

Can anyone suggest how to get such a table?

2
  • 2
    Unrelated: do remember that \bf has not been recommended since the early 90s. Use \textbf{....} instead.
    – daleif
    Commented Oct 30, 2023 at 15:02
  • The default value of \belowcaptionskip is 0pt, but several packages etc. swap with \abovecaptionskip for tables. Commented Oct 30, 2023 at 20:00

2 Answers 2

1

Your requirements can be easy fulfil by use of the tabularray and booktabs packages:

\documentclass[12pt]{article}
\usepackage[showframe,          % in real document remove this option
            a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{tabularray}         % new
\UseTblrLibrary{booktabs}       % booktabs package load as  tabularray library
\usepackage[skip=1ex,
            font=small,
            labelfont=bf]{caption}  % for caption formating

\begin{document}
    \begin{table}[ht]
\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tblr}{colspec = {@{} X[1.8, l] X[2, l] X[2, l] X[2.5,l] @{}},
             rowsep=5pt,
             row{1}  = {font=\bfseries,rowsep=2pt},
             }
    \toprule
{Study\\  location}
    &   {Study\\ Period}
        &   {Lead time\\ days}   
            &   {Methodology for\\ qualifying lead time.}   \\
    \midrule
{City 1,\\ Country 1}
    & Jun--Aug 2020 
        & 2 & \textit{Method 1}: attribute 1 vs attribute 2 \\
{City 2,\\ Country 2\\ 
(detail A, detail b)}
    & Aug 2020--Jan 2021 
        & 0 to 6 (different sublocations) 
            & \textit{Method 2}: attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
    \bottomrule
\end{tblr}
    \end{table}
\end{document}

enter image description here

3
  • I think the OP requires a line break between "City n" and "Country n".
    – Mico
    Commented Oct 30, 2023 at 15:44
  • 1
    @Mico, apparently I misunderstood this. Corrected now. Thank you very much!
    – Zarko
    Commented Oct 30, 2023 at 17:16
  • @Zarko Thanks a lot for the solution. It met my requirements. Thanks to Mico as well. I wish there were an option to accept both solutions as the answers.
    – TRK
    Commented Oct 31, 2023 at 10:47
1

Here's a solution that employs a tabularx environment (with a target width of \textwidth) and a variable-width version of the X column type for all four columns. Do also note (a) the judicious use of \newline directives to create line breaks within cells and (b) the use of \addlinespace to insert whitespace between rows.

enter image description here

\documentclass[12pt]{article}
\usepackage[a4paper, total={170mm,262mm}, left=20mm, top=20mm]{geometry}
\usepackage{booktabs}
% \usepackage{multirow} % (not needed)

% new code:
\usepackage[skip=0.333\baselineskip]{caption} % space below caption
\usepackage{tabularx} % for 'tabularx' env. and 'X' column type
\newcolumntype{L}[1]{%
  >{\raggedright\arraybackslash\hsize=#1\hsize}X}
\usepackage{newtxtext,newtxmath} % optional (Times Roman font clones)

\pagestyle{empty} % why?

\begin{document}

\begin{table}[htbp]

\caption{Studies that made a quantitative estimate of the parameters for the analysis on the blah blah blah blah}
\begin{tabularx}{\textwidth}{@{} L{0.8}L{0.6}L{0.9}L{1.7} @{}} % 0.8+0.6+0.9+1.7 = 4 = # of X-type columns
\toprule
\bfseries Study location & \bfseries Study Period & \bfseries Lead time, in days & \bfseries Methodology for qualifying lead time\\
\midrule
City 1, \newline Country 1 & Jun--Aug 2020 & 2 & \textit{Method 1}: Attribute 1 vs attribute 2 \\
\addlinespace
City 2, \newline Country 2 \newline (detail A, detail B) & Aug 2020--\hspace{0pt}Jan 2021 &
0 to 6 (various sublocations) & \textit{Method 2}: Attribute 1 vs attribute 2 given a certain preprocessing (7-day) \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}
1
  • Mico: Thanks for the solution. Your solution also worked for me. Amazing...
    – TRK
    Commented Oct 31, 2023 at 10:48

You must log in to answer this question.

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