Tables – How to Wrap and Align Text Within a Specified Cell Size in LaTeX Tables

alignmentlongtabletables

I am a beginner in Latex and I am struggling about doing tables. I have in my project several long and wide tables that span over several pages. I am finding difficult in aligning them within the page size which is A4 landscape.

Also, I am finding difficult on how to wrap the text within a cell of the table with a specified size. Finally, I am finding difficult to align the text in the headers and in the text so I make it all centered

I really appreciate if someone can help me so I can use and adapt my code for subsequent tables I have to do. I would appreciate if the code suggested is simple but generalisable enough so I can apply to as many further tables I have. Thank you!

Please see below my example code

\documentclass{article}
\usepackage{multirow}
\usepackage{lscape} 
\usepackage{comment}
\usepackage{longtable}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[T1]{fontenc}
\usepackage[style=nejm,backend=biber, maxbibnames=99, minnames=3, maxnames=20, terseinits=true, giveninits=true]{biblatex}
\usepackage{rotating}
\usepackage{float}
\usepackage{textgreek}
\usepackage[english]{babel}
\usepackage{pdflscape}
\usepackage[a4paper,width=150mm,top=25mm,bottom=35mm, right=25mm,left=25mm, bindingoffset=6mm]{geometry}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{ltablex,booktabs}
\usepackage{color,soul}
\usepackage{rotating}

\begin{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{landscape}
\centering

\renewcommand{\arraystretch}{0.9}%
\begin{longtable}{|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{1cm}|
>{\centering\arraybackslash}m{2cm}|
>{\centering\arraybackslash}m{1.5cm}|
}

%{|p{2cm}|p{1cm}|p{1cm}|p{1cm}|p{2cm}|p{2cm}|p{1cm}|p{2cm}|p{2cm}|p{1cm}|p{1cm}|p{2cm}|p{1.5cm}|} 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\hline

\multirow{3}{2cm}{Study} & 
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Phase}} & 
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Blinding}} &
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Design}} & 
\multirow{3}{2cm}{Disease} & 
\multicolumn{3}{m{5cm}|}{\small Investigational group} & 
\multicolumn{3}{m{5cm}|}{\small Reference group} & 
\multicolumn{2}{m{4cm}|}{\small PFS/DFS} \\

\cline{5-13}

& & & & &

\multirow{1}{2cm}{\small Treatment} & 
\multirow{1}{1cm}{N} & 
\multirow{1}{2cm}{\small Median PFS months} & 
\multirow{1}{2cm}{\small Treatment} & 
\multirow{1}{1cm}{N} & 
\multirow{1}{2cm}{\small Median PFS months} & 
\multirow{1}{2cm}{PFS HR (95\% CI)} & 
\multirow{1}{1.5cm}{PFS p-value} \\

& & & & & & & & & & & & \\

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
        \endfirsthead
        
        \multicolumn{13}{l}%
        {{\bfseries \tablename\ \thetable{} -- (Continued from previous page)}} \tabularnewline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

\hline

\multirow{3}{2cm}{Study} & 
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Phase}} & 
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Blinding}} &
\multirow{3}{1cm}{\rotatebox[origin=c]{90}{Design}} & 
\multirow{3}{2cm}{Disease} & 
\multicolumn{3}{m{5cm}|}{\small Investigational group} & 
\multicolumn{3}{m{5cm}|}{\small Reference group} & 
\multicolumn{2}{m{4cm}|}{\small PFS/DFS} \\

\cline{5-13}

& & & & &

\multirow{1}{2cm}{\small Treatment} & 
\multirow{1}{1cm}{N} & 
\multirow{1}{2cm}{\small Median PFS months} & 
\multirow{1}{2cm}{\small Treatment} & 
\multirow{1}{1cm}{N} & 
\multirow{1}{2cm}{\small Median PFS months} & 
\multirow{1}{2cm}{PFS HR (95\% CI)} & 
\multirow{1}{1.5cm}{PFS p-value} \\

& & & & & & & & & & & & \\
\hline
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
        \endhead
        
         &     & \tabularnewline
        \hline 
        \multicolumn{13}{l}{{(Continued on next page)}} \tabularnewline
        \endfoot
        
        \hline
        \endlastfoot
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
\cline{1-13}

\multicolumn{13}{|l|}{Primary endpoint DFS} \\

\cline{1-13}

\citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox[origin=c]{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94,0.70,1.26) & 0.679 \\

\cline{1-13}

\citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox[origin=c]{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\

\cline{1-13}

\citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox[origin=c]{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94,0.70,1.26) & 0.679 \\

\cline{1-13}

\citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox[origin=c]{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\

\cline{1-13}

\multicolumn{13}{|l|}{Primary endpoint Other} \\

\cline{1-13}

\citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox[origin=c]{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94,0.70,1.26) & 0.679 \\

\cline{1-13}

\citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox[origin=c]{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\

\cline{1-13}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{longtable}
\end{landscape}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

This is part of the table latex produces

latex version

This is the nice table I would like to produce with everything well aligned (using excel)

ideal table

In particular I would like the following to be improved

in circles are the things that do not come correctly

you can see that "superiority" is not centered. "Cediranib 20mg+FOLFOX6" go over to the next cell. "Bevacizumab+FOLFOX6" go over the next cell.

How do I improve these aspects in such a way that regardless of the text the text gets wrapped within the limits of the cell of the table?

Many thanks

Andy

Best Answer

I have spent a little time to make you table better (I hope).

  1. Text produced by \citeyear, seems is one word, and it can't be wrapped, so the only solution is to increase width of column. By the way, some column can be made narrower.
  2. To fit rotated "Blinding" in head of table,\vrule can be palced before "Investigation group" - \vrule height 14pt depth 10pt width 0pt Investigational group. This increases height of very first row.
  3. Do not use p{} as specification of multicolum if text fits in one line, use c to center text in whole width of multicolum span.
  4. You may specify \hsize as width of content for \multirow \multirow{3}{\hsize}{\centering Study}. Multirow ignores alignment specification for cell, so it need to be specified explicitly.
  5. With {\arraystretch}{0.9} lines touch text, seems, it is not wery good, better to increase value of \arraystretch.
  6. Width of similar columns (Treatment, for ex.), of course, should be the same.

And here is how the table looks enter image description here

  1. I have removed \multirow for some cells and origin for ratated words in body of the table.

Here is code of the table (only body, no changes to preamble)

\begin{document}
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{landscape}
        \centering
        
        \renewcommand{\arraystretch}{1.2}%
        \tabcolsep=3pt
        \begin{longtable}{|
                >{\centering\arraybackslash}m{3.0cm}| %  1 Study
                >{\centering\arraybackslash}m{0.7cm}| %  2 Phase
                >{\centering\arraybackslash}m{0.7cm}| %  3 Blinding
                >{\centering\arraybackslash}m{0.7cm}| %  4 Design
                >{\centering\arraybackslash}m{2.0cm}| %  5 Desease
                >{\centering\arraybackslash}m{2.3cm}| %  6 Treatment
                >{\centering\arraybackslash}m{0.7cm}| %  7 N
                >{\centering\arraybackslash}m{2.0cm}| %  8 Median PFS
                >{\centering\arraybackslash}m{2.0cm}| %  9 Treatment
                >{\centering\arraybackslash}m{0.7cm}| % 10 N
                >{\centering\arraybackslash}m{2.0cm}| % 11 Median PFS
                >{\centering\arraybackslash}m{2.5cm}| % 12 PFS HR
                >{\centering\arraybackslash}m{1.5cm}| % 13 PFS p-value
            }
            
            %{|p{2cm}|p{1cm}|p{1cm}|p{1cm}|p{2cm}|p{2cm}|p{1cm}|p{2cm}|p{2cm}|p{1cm}|p{1cm}|p{2cm}|p{1.5cm}|} 
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            
            \hline
            
            \multirow{3}{\hsize}{\centering Study} & 
            \multirow{3}{\hsize}{\centering \rotatebox[origin=c]{90}{Phase}} & 
            \multirow{3}{\hsize}{\centering\rotatebox[origin=c]{90}{Blinding}} &
            \multirow{3}{\hsize}{\centering\rotatebox[origin=c]{90}{Design}} & 
            \multirow{3}{\hsize}{\centering Disease} & 
            \multicolumn{3}{c|}{\small \vrule height 14pt depth 10pt width 0pt Investigational group} & 
            \multicolumn{3}{c|}{\small Reference group} & 
            \multicolumn{2}{c|}{\small PFS/DFS} \\
            
            \cline{6-13}
            
            & & & & &
            
            \small Treatment & 
            N & 
            \small Median PFS months & 
            \small Treatment & 
            N & 
            \small Median PFS months & 
            PFS HR (95\%~CI) & 
            PFS p-value \\
            %& & & & & & & & & & & & \\
            
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
            \endfirsthead
            
            \multicolumn{13}{l}%
            {{\bfseries \tablename\ \thetable{} -- (Continued from previous page)}} \tabularnewline
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
            
            \hline
            
            \multirow{3}{\hsize}{\centering Study} & 
            \multirow{3}{\hsize}{\centering\rotatebox[origin=c]{90}{Phase}} & 
            \multirow{3}{\hsize}{\centering\rotatebox[origin=c]{90}{Blinding}} &
            \multirow{3}{\hsize}{\centering\rotatebox[origin=c]{90}{Design}} & 
            \multirow{3}{\hsize}{\centering Disease} & 
            \multicolumn{3}{c|}{\small \vrule height 14pt depth 10pt width 0pt Investigational group} & 
            \multicolumn{3}{c|}{\small Reference group} & 
            \multicolumn{2}{c|}{\small PFS/DFS} \\
            
            \cline{6-13}
            
            & & & & &
            
            \small Treatment & 
            N & 
            \small Median PFS months & 
            \small Treatment & 
            N & 
            \small Median PFS months & 
            PFS HR (95\%~CI) & 
            PFS p-value \\
            
            %& & & & & & & & & & & & \\
            \hline
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%         
            \endhead
            
            %&     & \tabularnewline
            \hline 
            \multicolumn{13}{l}{{(Continued on next page)}} \tabularnewline
            \endfoot
            
            \hline
            \endlastfoot
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
            \cline{1-13}
            
            \multicolumn{13}{|l|}{Primary endpoint DFS} \\
            
            \cline{1-13}
            
            \citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94,0.70,1.26) & 0.679 \\
            
            \cline{1-13}
            
            \citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\
            
            \cline{1-13}
            
            \citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94,0.70,1.26) & 0.679 \\
            
            \cline{1-13}
            
            \citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\
            
            \cline{1-13}
            \noalign{\penalty-10000}
            \multicolumn{13}{|l|}{Primary endpoint Other} \\
            
            \cline{1-13}
            
            \citeauthor*{pmid25403578}, \citeyear{pmid25403578} \parencite{pmid25403578}; \citeauthor*{pmid27310205}, \citeyear{pmid27310205} \parencite{pmid27310205} & 3 & No & \rotatebox{90}{Superiority} & Resectable metastatic colorectal cancer & FOLFOX7+FOLFORI & 140 & 24.5 & FOLFOX4 & 142 & 22.4 & 0.94 (0.94, 0.70, 1.26) & 0.679 \\
            
            \cline{1-13}
            
            \citeauthor*{pmid28634730}, \citeyear{pmid28634730} \parencite{pmid28634730} & 3 & No & \rotatebox{90}{Non-inferiority} & IIB/III colorectal cancer & Oral adjuvant uracil and tegafur plus protein-bound polysaccharide K (UFT + PSK) & 151 & NR, 3 years DFS=72.6\% & Oral adjuvant uracil and tegafur plus leucovorin (UFT/LV) & 161 & NR, 3 years DFS=82.3\% & NR & NR \\
            
            \cline{1-13}
            
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        \end{longtable}
    \end{landscape}
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
\end{document}
Related Question