6

Since I have multiple column I want to wrap text but can't find suitable code for it. How can I do that in Latex?

I used the following command:

\begin{table}
\resizebox{1\textwidth}{!}{
\begin{tabular}{lcccccc} \hline  \hline 
Sample window & Probability cutoff value & Crises Correctly called (\%) & Non-crises correctly called (\%) & Missed crises & False Alarm &  \\ \hline

 \hline
\end{tabular}}
\end{table}

And that gives something like below:

enter image description here

I want to create something like below instead:

enter image description here

  • 1
    Don't use resizebox on a tablar as this will lead to inconsistent and sometimes illegibly small font sized. – leandriis Dec 27 '19 at 12:41
  • 1
    You might wan to use fixed width p or m type columns instead of c columns. If your table will also contain numbers, you might want to ge for S type columns from siunitx (alignment of numbers) in combination with \thead from makecell for the table headers. – leandriis Dec 27 '19 at 12:42
  • 1
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. Please also go through your previous questions and mark useful answers there, too. – leandriis Jan 17 at 8:37
7

With \thead from makecell (as I suspect that there will be numbers in the table, S type columns from siunitx might come in handy to align them):

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{makecell}

\begin{document}

\begin{table}
\begin{tabular}{lcccccc} 
\toprule
\thead{Sample\\ window} & \thead{Probability\\ cutoff value} & \thead{Crises\\ Correctly\\ called (\%)} & \thead{Non-crises\\ correctly\\ called (\%)} & \thead{Missed\\ crises} & \thead{False\\ Alarm} &  \\ 
\midrule

\end{tabular}
\end{table}

\end{document}
6

Maybe with tabularx?

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{table} 
 \begin{tabularx}{\textwidth}{X*{5}{>{\centering\arraybackslash}X}} 
 \hline \hline 
 Sample window & Probability cutoff value & Crises Correctly called (\%) &
 Non-crises correctly called (\%) & Missed crises & False Alarm  \\ 
 \hline
\end{tabularx} 
\end{table}
\end{document}

enter image description here

5

Here's another tabularx-based solution, which shows how to set up unequal column widths. Furthermore, it employs the caption package to format the appearance of the caption to the style shown in the screenshot, and it employs the line-drawing macros of the booktabs package (\toprule, \midrule, and \bottomrule) instead of \hline\hline.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e,booktabs}
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\newcolumntype{L}{>{\RaggedRight\arraybackslash}X}
\newcolumntype{C}[1]{>{\Centering\arraybackslash\hsize=#1\hsize}X}

\usepackage{caption}
\captionsetup{justification=RaggedRight, 
              singlelinecheck=false,
              labelfont=bf,
              labelsep=period,
              skip=0.25\baselineskip}

\usepackage{newtxtext,newtxmath} % optional

\begin{document}
\setcounter{table}{2} % optional
\renewcommand{\tabcolsep}{3pt} % default: 6pt
\begin{table}
\caption{Logit EWS: In-sample and out-of-sample performances}
\begin{tabularx}{\textwidth}{@{} L *{3}{C{1}} *{2}{C{0.9}} C{1.2} @{}} 
\toprule
Sample windows & Probability cutoff value & 
Crises correctly called (\%) & Non-crises correctly called (\%) & 
Missed crises & False alarms & Crisis prob.\ given alarm \\ 
\midrule
1995--06 & 0.06 & 83.3 & 53.5 & 16.7 & 46.5 & 0.26 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}
4

If you want to set up the columns for the data, use \multicolumn for the titles. This divides the columns evenly.

\documentclass{article}
\usepackage{showframe}% alignment tool
\newlength{\tempdima}
\begin{document}

\begin{table}
\tempdima=\dimexpr \textwidth/7 - 2\tabcolsep\relax
\small
\begin{tabular}{lcccccc} \hline\hline 
\multicolumn{1}{p{\tempdima}}{Sample window} & 
\multicolumn{1}{p{\tempdima}}{Probability cutoff value} & 
\multicolumn{1}{p{\tempdima}}{Crises Correctly called (\%)} & 
\multicolumn{1}{p{\tempdima}}{Non-crises correctly called (\%)} & 
\multicolumn{1}{p{\tempdima}}{Missed crises} & 
\multicolumn{1}{p{\tempdima}}{False Alarm} &
\multicolumn{1}{p{\tempdima}}{(not used)}  \\ \hline
 \\
 \hline
\end{tabular}
\end{table}

\end{document}

demo

4

Here is three different examples on how you can set your tabulars either

  1. using tabularx,
  2. a combination of tabularx and fixed width w-columns from the array packages or
  3. a combination of tabular* and dcolumn.

In the two first example and except for the first column, I suggest setting all columns right aligned, also the headings. The are are so dominant so I reduced the font size. As you will see, I aligned all headings at the bottom of the cells.

The document class scrartcl from the KOMA-script has built in commands for setting captions and more sensible margins as standard.

Example 1 – tabularx

I defined three new column types F, R and L, for first column, the seven with decimal separators and the last one.

For the first and last column width, set by the \hsize=1.2\hsize it is important that the sum is 2, see the tabularx manual. Headings are \footnotesize to avoid overfull hbox warnings.

enter image description here

\documentclass[UKenglish]{scrartcl}
\usepackage{tabularx,ragged2e,booktabs,babel}
\usepackage{lmodern}
\usepackage{microtype}
\renewcommand{\tabularxcolumn}[1]{b{#1}}   % Table headings align at the bottom
\newcolumntype{F}{>{\RaggedRight\arraybackslash\hsize=1.2\hsize}X}
\newcolumntype{L}{>{\bfseries\RaggedLeft\arraybackslash\hsize=0.8\hsize}X}
\newcolumntype{R}{>{\RaggedLeft\arraybackslash}X}

\KOMAoptions{captions=tableheading,%
                captions=nooneline}

\addtokomafont{captionlabel}{\small\sffamily\bfseries}
\addtokomafont{caption}{\small\sffamily}

\begin{document}

\setcounter{table}{2} % Increase the couter so next table is table 3
\setlength{\tabcolsep}{0.4em} % Slightly decrease the space between columns to allow more space for the columns

\begin{table}
\caption{Logit EWS: In-sample and out-of-sample performances}
\begin{tabularx}{\textwidth}{@{} F *{7}{R} L @{}}
\toprule
\footnotesize Sample windows &                 % `footnotesize` to avoid overful hboxes
\footnotesize Proba\-bility cutoff value &
\footnotesize Crises correctly called\,(\%) &
\footnotesize Non-crises correctly called\,(\%) &
\footnotesize Missed crises Episodes (\%) &
\footnotesize False alarms &
\footnotesize Crisis prob.\ given alarm &
\footnotesize Crisis prob.\ given no\,alarm &
\footnotesize TME\\ 
\midrule
&&&
\multicolumn{3}{@{}>{\small\Centering\hsize=3\hsize}X@{}}{Panel A: $\eta=2$ \par In-sample}
&&&\\\addlinespace[0.75ex]
1995--06 & 0.06 & 83.3 & 53.5 & 16.7 & 46.5 & 0.26 & 0.06 & 63\\
1995--07 & 0.08 & 73.4 & 61.0 & 26.6 & 39.0 & 0.25 & 0.07 & 66\\ 
1995--08 & 0.07 & 63.4 & 61.4 & 36.6 & 38.6 & 0.29 & 0.13 & 75\\ 
1995--09 & 0.11 & 60.9 & 62.7 & 39.1 & 37.3 & 0.28 & 0.13 & 76\\ 
1995--10 & 0.09 & 60.8 & 59.5 & 33.2 & 40.0 & 0.27 & 0.11 & 74\\ 
\midrule
&&&
\multicolumn{3}{>{\small\Centering\hsize=3\hsize}X}{\small Out-of-sample}
&&&\\\addlinespace[0.75ex]

\bottomrule
\end{tabularx}
\end{table}

\end{document}

Example 2 – tabularx and array

The w-column is not a paragraph column, so the headings has to go into \parboxes to set the multiline headings. For the seven w columns, I use the same fixed width, for the first and last, the \hsize picks up the column from tablarx, which also ensure that the tabular fills the full line width.

enter image description here

\documentclass[UKenglish]{scrartcl}
\usepackage{tabularx,ragged2e,booktabs}
\usepackage{lmodern}
\usepackage{microtype}
\usepackage{babel}

\newcolumntype{F}{>{\RaggedRight\arraybackslash\hsize=1.2\hsize}X}
\newcolumntype{L}{>{\bfseries\RaggedLeft\arraybackslash\hsize=0.8\hsize}X}

\KOMAoptions{captions=tableheading,%
                captions=nooneline}

\addtokomafont{captionlabel}{\small\sffamily\bfseries}
\addtokomafont{caption}{\small\sffamily}

\begin{document}

\newcommand{\head}[1]{\parbox[b]{1.35cm}{\RaggedLeft\small #1} }

\setcounter{table}{2} % Increase the table number 
\renewcommand{\tabcolsep}{0.45em} % reduce distance between columns

\begin{table}
\caption{Logit EWS: In-sample and out-of-sample performances}
\begin{tabularx}{\textwidth}{@{} F*{7}{wr{1.35cm}}L@{}}
\toprule
\parbox[b]{\hsize}{\small\raggedright Sample windows} &
\head{Probabi\-lity cutoff value} &
\head{Crises correctly called\,(\%)} &
\head{Non-crises correctly called\,(\%)} &
\head{Missed crises \mbox{Episodes} (\%)} &
\head{False alarms} &
\head{Crisis prob.\ given alarm} &
\head{Crisis prob.\ given no alarm} &
\parbox[b]{\hsize}{\RaggedLeft\small TME}\\ 
\midrule
&&&
\multicolumn{3}{wc{4.15cm}}{\parbox{4.15cm}{\Centering\small Panel A: $\eta=2$ \par In-sample}}
&&&\\\addlinespace[0.75ex]
1995--06 & 0.06 & 83.3 & 53.5 & 16.7 & 46.5 & 0.26 & 0.06 & 63\\
1995--07 & 0.08 & 73.4 & 61.0 & 26.6 & 39.0 & 0.25 & 0.07 & 66\\ 
1995--08 & 0.07 & 63.4 & 61.4 & 36.6 & 38.6 & 0.29 & 0.13 & 75\\ 
1995--09 & 0.11 & 60.9 & 62.7 & 39.1 & 37.3 & 0.28 & 0.13 & 76\\ 
1995--10 & 0.09 & 60.8 & 59.5 & 33.2 & 40.0 & 0.27 & 0.11 & 74\\ 
\midrule
&&&
\multicolumn{3}{wc{4.15cm}}{\parbox{4.15cm}{\Centering\small Out-of-sample}}
&&&\\\addlinespace[0.75ex]

\bottomrule
\end{tabularx}
\end{table}
\end{document}

Example 3 – tabular* and dcolumn

This tabular utilises a combination of zero \tabcolsep and tabular*’s ability to expand the space between columns stretching the tabular to the given width (here \linewidth). Thanks to Mico for sharing this trick in this answer.

Since D-columns are maths columns, the headings have to be set in \multicolumn to protect them and put the \parbox inside those. I defined three different table heading commands (\thead), left, centre and right aligned.

The new column types (Y and Z) are D-columns for one or two decimals.

enter image description here

\documentclass[UKenglish]{scrartcl}
\usepackage{ragged2e,booktabs,babel, dcolumn}
\usepackage{lmodern}
\usepackage{microtype}

\newcolumntype{Y}{D{.}{.}{1.2}}
\newcolumntype{Z}{D{.}{.}{2.1}}

\KOMAoptions{captions=tableheading,%
                captions=nooneline}

\addtokomafont{captionlabel}{\small\sffamily\bfseries}
\addtokomafont{caption}{\small\sffamily}

\begin{document}

\setcounter{table}{2}       % Increase the counter so next table is table 3
\setlength{\tabcolsep}{0em} % Remove tabcolsep, will be compensated by tabular* 
                            % and @{\extracolsep{\fill}}, thanks to Mico
                            % (see https://tex.stackexchange.com/a/518032/9632)

\begin{table}
\newcommand{\thead}[1]{\multicolumn{1}{c}{\parbox[b]{1.35cm}{\Centering\small #1}}}
\newcommand{\theadl}[1]{\multicolumn{1}{l}{\parbox[b]{1.75cm}{\RaggedRight\small #1}}}
\newcommand{\theadr}[1]{\multicolumn{1}{r}{\parbox[b]{1cm}{\bfseries\RaggedLeft\small #1}}}

\caption{Logit EWS: In-sample and out-of-sample performances}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}} lY*{4}{Z}*{2}{Y}>{\bfseries}r}
\toprule

\theadl{Sample windows} &
\thead{Probabili\-ty cutoff value} &
\thead{Crises correctly called\,(\%) } &
\thead{Non-crises correctly called\,(\%) } &
\thead{Missed crises Episodes\,(\%)} &
\thead{False alarms} &
\thead{Crisis prob. given alarm} &
\thead{Crisis prob. given no\,alarm} &
\theadr{TME} \\
\midrule
&&&
\multicolumn{3}{@{}>{\small\Centering}p{3cm}@{}}{Panel A: $\eta=2$ \par In-sample}
&&&\\\addlinespace[0.75ex]
1995--06 & 0.06 & 83.3 & 53.5 & 16.7 & 46.5 & 0.26 & 0.06 & 63\\
1995--07 & 0.08 & 73.4 & 61.0 & 26.6 & 39.0 & 0.25 & 0.07 & 66\\ 
1995--08 & 0.07 & 63.4 & 61.4 & 36.6 & 38.6 & 0.29 & 0.13 & 75\\ 
1995--09 & 0.11 & 60.9 & 62.7 & 39.1 & 37.3 & 0.28 & 0.13 & 76\\ 
1995--10 & 0.09 & 60.8 & 59.5 & 33.2 & 40.0 & 0.27 & 0.11 & 74\\ 
\midrule
&&&
\multicolumn{3}{>{\small\Centering}p{3cm}}{Out-of-sample}
&&&\\\addlinespace[0.75ex]

\bottomrule
\end{tabular*}
\end{table}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.