# Convert table from landscape to portrait

I am unable to make a table in portrait because I have some large col headings (some with hyphenated words) that don't wrap. The only way I could create this table was by making it in landscape orientation. Please point out how to modify my code so that I can make the table below in portrait orientation.

\documentclass{report}
\usepackage{booktabs, multirow, pdflscape}
\begin{document}
\begin{landscape}
\begin{table}[t]
\caption{This is my table and I hope you can help me improve
it. How do I make it in portrait?}
\label{tab:testtab}
\begin{tabular*}{\textwidth} {p{1.4cm}|p{1cm}p{2.9cm}p{3.5cm}|p{1cm}p{2.9cm}p{3.5cm}|p{2cm}}
\toprule
\multirow{2}[2]{1.4cm} {Col group (years)} & \multicolumn{3}{c}
{{\textbf{Location one}}} & \multicolumn{3}{c}{{\textbf{Location two}}} &
\multirow{2}[2]{2.6cm}{\textbf{Combined conditionX-      associated SOME cases}}\\
& SOME cases & Percent positive for conditionX & conditionX-associated SOME cases & SOME cases & Percent positive for conditionX & conditionX-associated SOME cases & \\
\midrule 0-$<$1  & 455 & 10.4 (15/144) & 47  & 381 & 10.0 (2/20) & 38 & 86 (25\%)\\
1-4     & 376 & 10.9 (19/175) & 41  & 256 & 20.8 (10/48)& 53 & 94 (28\%)\\
5-15    & 91  & 10.0 (1/10)   & 9   & 157 & 30.0 (3/10) & 47 & 56 (17\%)\\
16-24   &     &                &    & 91  & 12.0 (3/25) & 11 & 11 (3\%)\\
25-49   &     &                &    & 244 & 11.4 (8/70) & 28 & 28 (8\%)\\
50-64   &     &                &    & 280 & 10.0 (3/30) & 28 & 28 (8\%)\\
$\geq$65 &    &                &    & 334 & 11.1 (5/45) & 37 & 37 (11\%)\\
\hline
Total   & 922 & 10.6 (35/329) & 97 & 1,743& 13.7 (34/248) &     242 & 340
(100\%)\\ \bottomrule
\end{tabular*}
\end{table}
\end{landscape}
\end{document}


To convert this table to portrait I need columns 3 and 4 to wrap (text should display in 3 lines not 2). Once that is done the last column would look alright (ie., text should spread over 4 lines). Note: I listed several packages used elsewhere in the document. Hope someone can help.

• I also need contents for cols 2-7 centered horizontally and vertically. Cells that have a fraction in parenthesis need to be on the same line as the preceding number. Thanks! – epdnm Oct 9 '18 at 2:50
• Do please provide information about the page size and margin widths. – Mico Oct 9 '18 at 4:48
• A4 page. I am using \usepackage{geometry} as follows: \geometry{includehead} \geometry{hmargin={3.5cm,2.3cm}} \geometry{vmargin={1.6cm,2.5cm}} \geometry{headheight=15pt} \sloppy \hyphenation{} \hyphenpenalty=10000 \exhyphenpenalty=10000 – epdnm Oct 9 '18 at 5:17

Some suggestions:

• Instead of guessing the column widths of the p columns, measure them out using \settowidth instructions. There are four separate widths to calculate.

• Since you wish to use a tabular* environment, set the \tabcolsep parameter to 0pt and let LaTeX figure out the amounts of intercolumn whitespace by providing the directive @{\extracolsep{\fill}}.

• Provide more visual structure in the table's header. Use \cmidrule directives.

• Don't use vertical lines in the table. They're not needed.

• Last I checked, 47 + 38 \ne 86, and 97 + 242 \ne 340. :-)

• Write conditionX- associated instead of conditionX-associated. That way, a line break is still possible after conditionX- in the headers of columns 4, 7, and 8 even though you've suppressed hyphenation.

• Incidentally, I believe it's highly questionable to suppress all hyphenation and then restore some semblance of decent typesetting via the \sloppy directive. If you really can't abide hyphenation, just make the entire document \raggedright and live with the consequences. For that matter, what's so awful about hyphenation that makes you willing to abandon decent typesetting output?

\documentclass[a4paper]{report}
\usepackage{geometry}
hmargin={3.5cm,2.3cm},vmargin={1.6cm,2.5cm}}
\sloppy %% highly questionable!
%\hyphenation{}
\hyphenpenalty=10000
\exhyphenpenalty=10000
\usepackage[skip=0.333\baselineskip]{caption} % optional
\usepackage{booktabs}
\usepackage{array} % for "\newcolumntype" macro
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
% Measure four separate column widths:
\newlength\lena  \settowidth{\lena}{(years)}
\newlength\lenb  \settowidth{\lenb}{SOME}
\newlength\lenc  \settowidth{\lenc}{10.9 (19/175)}
\newlength\lend  \settowidth{\lend}{SOME cases}

\begin{document}
\begin{table}[t]
\setlength\tabcolsep{0pt} % default: 6pt
\caption{This table now fits in portrait mode}
\label{tab:testtab}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
P{\lena} P{\lenb}P{\lenc}P{\lend}
P{\lenb}P{\lenc}P{\lend} P{\lend} }
\toprule
& \multicolumn{3}{c}{Location one}
& \multicolumn{3}{c}{Location two}
& Combined\\
\cmidrule{2-4} \cmidrule{5-7} \cmidrule(l){8-8}
Col group (years)
& SOME cases
& Percent positive for conditionX
& conditionX- associated SOME cases
& SOME cases
& Percent positive for conditionX
& conditionX- associated SOME cases
& conditionX- associated SOME cases \\
\midrule
0--$<$1  & 455 & 10.4 (15/144) & 47 & 381 & 10.0 (2/20) & 38 & 86 (25\%)\\
1--4     & 376 & 10.9 (19/175) & 41 & 256 & 20.8 (10/48)& 53 & 94 (28\%)\\
5--15    &  91 & 10.0 (1/10)   &  9 & 157 & 30.0 (3/10) & 47 & 56 (17\%)\\
16--24   &     &               &    &  91 & 12.0 (3/25) & 11 & 11 (3\%)\\
25--49   &     &               &    & 244 & 11.4 (8/70) & 28 & 28 (8\%)\\
50--64   &     &               &    & 280 & 10.0 (3/30) & 28 & 28 (8\%)\\
$\geq$65 &     &               &    & 334 & 11.1 (5/45) & 37 & 37 (11\%)\\
\midrule
Total    & 922 & 10.6 (35/329) & 97 &1,743&13.7 (34/248)&242 &340 (100\%) \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}

• Thanks @Mico. I didn't know I had suppressed hyphenation. Where did I do that? PS:Thanks for picking up on the maths issue. I had decimals that were rounded incorrectly – epdnm Oct 9 '18 at 7:15
• @epdnm - The instructions \hyphenpenalty=10000 and \exhyphenpenalty=10000 serve to make hyphenation (essentially) infinitely costly during the paragraph building phase. As a result, very few hyphenations will occur; as you've discovered, not even conditionX-associated will get line-broken. The \sloppy directive would appear to serve mainly to suppress all sorts of warnings TeX would issue about underfull lines and over-stretched interword spaces. Incidentally, it's never a good idea to include TeX directives in the preamble if one doesn't have a good sense of what these directives do. – Mico Oct 9 '18 at 7:21
• I see. The use of tabular* is not compulsory for me, if you suggest i use something else to simplify this code, i would be more than happy to try it. I've been using LaTeX for only a few weeks, hence, this clumsy code (and the use of directives that help me once but hinder me later on). In the table you created, how can I have the heading for col 1 covering 4 lines (not 3) & how to remove line under 'Combined'? – epdnm Oct 9 '18 at 7:23
• @epdnm - Think what \cmidrule(l){8-8} might do... – Mico Oct 9 '18 at 7:34
• Hi @Mico to get closer to what I am after I had to 'mangle' your work a bit. To get the headings in first and last columns centered across 4 rows I used: \multirow{4}{1.6cm}{Col group (years)} also: & \multirow{4}{2.7cm}{Combined conditionX- associated SOME cases}\\ then: \cmidrule{2-4} \cmidrule{5-7} %but no separation between this hline and previous is achieved...So what would be the appropriate way to do this? – epdnm Oct 9 '18 at 12:28

Another suggestion: swap rows and columns and add some colour.

\documentclass{report}
\usepackage{booktabs, multirow, caption, array, makecell}
\usepackage{siunitx}
\usepackage[svgnames, table]{xcolor}

\begin{document}

\begin{table}[!htb]
\setlength{\extrarowheight}{2pt}
\setlength{\tabcolsep}{5pt}
\sisetup{range-phrase=--}
\caption{Table in portrait – rows and columns are swapped}
\centering
\begin{tabular}{*{8}{c}}
\toprule
\multicolumn{8}{c}{Col group (years)}\\
\numrange[range-phrase=\,--\textless]{0}{1} & \numrange{1}{4} & \numrange{5}{15} & \numrange{16}{24}
& \numrange{25}{49} & \numrange{50}{64} & $\geq 65$ & \textbf{Total} \\
\rowcolor{WhiteSmoke!50! Lavender}\multicolumn{8}{@{}c@{}}{\textbf{Location one}}\\
\multicolumn{8}{@{}l}{SOME cases}\\
555 & 376 & 91 & & & & & 922 \\
\multicolumn{ 8}{@{}l}{Percent positive for conditionX}\\
\makecell{10.4 \\ (15/144) } & \makecell{10.9\\ (19/175)} & \makecell{10.0 \\ (1/10)} & & & & & \makecell{10.6\\ (35/329)} \\
\multicolumn{8}{@{}l}{ConditionX-associated SOME cases} \\
47 & 41 & 9 \\
\rowcolor{WhiteSmoke!50! Lavender}\multicolumn{ 8}{@{}c@{}}{\textbf{Location two}} \\
\multicolumn{8}{@{}l}{SOME cases}\\
381 & 256 & 157 & 91 & 244 & 280 & 334 & 1{,}743 \\
\multicolumn{8}{@{}l}{Percent positive for conditionX}\\
\makecell{10.0 \\ (2/20)} & \makecell{20.8 \\ (10/48) } & \makecell{30.0\\ (3/10)} & \makecell{12.0\\ (3/25)}& \makecell{11.4\\ (8/70)} & \makecell{10.0 \\ (3/30)} & \makecell{11.1\\ (5/45)} & \makecell{13.7\\ (34/248)} \\
\multicolumn{8}{@{}l}{ConditionX-associated SOME cases} \\
38 & 53 & 47 & 11 & 28 & 28 & 37 & 242 \\

• siunitx is not really needed here – it just slightly simplifies the code (in particular for spacing problems. You also may code, say $16\textendash24$. For the options conflict you may either write \documentclass[svgnames, table, other options]{report} or add to your preamble \PassOptionsToPackage{svgnames,table}{xcolor}. B.t.w., with the table option, needless to load colortbl. – Bernard Oct 9 '18 at 11:57