如何在长表的单元格中换行

如何在长表的单元格中换行

请帮我格式化这个表格,以便我可以正确换行文本并使表格适合 A4 页面。我使用了 tablesgenerator.com,但它的格式并不完美。您可以看到一张图片,显示了我想要实现的格式类型。此外,我希望表格以横向显示并跨越多页。

我需要用 latex 创建表

非常感谢能修复我的代码的人。我花了很多时间试图修复这个问题,但没有成功

\documentclass[12pt,a4paper, twoside]{report}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{lscape}
\usepackage{longtable}
\usepackage{array}


\begin{document}


\begin{landscape}
\begin{longtable}{
>{\centering\hspace{0pt}}m{0.10\linewidth}
>{\centering\hspace{0pt}}m{0.10\linewidth}
>{\centering\hspace{0pt}}m{0.09\linewidth}
>{\centering\hspace{0pt}}m{0.106\linewidth}
>{\centering\hspace{0pt}}m{0.096\linewidth}
>{\centering\hspace{0pt}}m{0.15\linewidth}
>{\centering\hspace{0pt}}m{0.15\linewidth}
>{\centering\arraybackslash\hspace{0pt}}m{0.15\linewidth}
} 

\toprule
This is   the first title of this table               & This is the   second title of this table                                  & This is the   third title of this table                             & This is the   fourth title of this table & This is the   ffth title of this table                                             & This is the   sixth title of this table            & This is the   seventh title of this table          & This is the   eighth title of this table                                                                                        \\* \midrule
\endfirsthead

            \multicolumn{8}{l}%
            {{\bfseries \tablename\ \thetable{} -- (Continued from previous page)}} \tabularnewline
%
\toprule
This is   the first title of this table               & This is the   second title of this table                                  & This is the   third title of this table                             & This is the   fourth title of this table & This is the   ffth title of this table                                             & This is the   sixth title of this table            & This is the   seventh title of this table          & This is the   eighth title of this table                                                                                        \\* \midrule

\endhead
%
        
            %&     & \tabularnewline
            \hline 
            \multicolumn{8}{l}{{(Continued on next page)}} \tabularnewline
            \endfoot
            
            \hline
            \endlastfoot

\multicolumn{8}{l}{Primary endpoint XXXXX}                                                                              \\* \midrule
\multicolumn{1}{c}{\multirow{4}{*}{author1, year}}  & \multicolumn{1}{c}{\multirow{4}{*}{Quality   of life instrument 233454}} & \multicolumn{1}{c}{\multirow{4}{*}{Utility   index global scale}}  & \multicolumn{1}{c}{0}                   & \multicolumn{1}{c}{\multirow{4}{*}{OR   (95\%CI) (from xyz regression) {[}N{]}}}  & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{\multirow{4}{*}{p\textgreater{}0.10   for all visits-specific comparisons; p=0.534 for overall comparison}} \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 1}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 2}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 3}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \midrule
\multicolumn{1}{c}{\multirow{4}{*}{author2, year}}  & \multicolumn{1}{c}{\multirow{4}{*}{Quality   of life instrument 233454}} & \multicolumn{1}{c}{\multirow{4}{*}{Utility   index global scale}}  & \multicolumn{1}{c}{0}                   & \multicolumn{1}{c}{\multirow{4}{*}{OR   (95\%CI) (from xyz regression) {[}N{]}}}  & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{\multirow{4}{*}{p\textgreater{}0.10   for all visits-specific comparisons; p=0.534 for overall comparison}} \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 1}              & \multicolumn{1}{c}{}                                                              & \multicolumn{2}{c}{0.762(0.703,0.823) {[}NR{]}}                                                        & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 2}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 3}              & \multicolumn{1}{c}{}                                                              & \multicolumn{2}{c}{0.762(0.703,0.823) {[}NR{]}}                                                        & \multicolumn{1}{c}{}                                                                                                           \\* \midrule
\multicolumn{1}{c}{\multirow{12}{*}{author3, year}} & \multicolumn{1}{c}{\multirow{12}{*}{FACT}}                               & \multicolumn{1}{c}{\multirow{6}{*}{Utility   index global scale1}} & \multicolumn{1}{c}{0}                   & \multicolumn{1}{c}{\multirow{12}{*}{OR   (95\%CI) (from xyz regression) {[}N{]}}} & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{\multirow{6}{*}{p\textgreater{}0.10   for all visits-specific comparisons; p=0.534 for overall comparison}} \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{1 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{2 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{3 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{4 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{5 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){3-4} \cmidrule(l){6-8} 
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{\multirow{6}{*}{Utility index global scale2}}   & \multicolumn{1}{c}{0}                   & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{\multirow{6}{*}{p\textgreater{}0.10   for all visits-specific comparisons; p=0.534 for overall comparison}} \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{1 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{2 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{3 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{4 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{5 week}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \midrule
\multicolumn{1}{c}{\multirow{4}{*}{author4, year}}  & \multicolumn{1}{c}{\multirow{4}{*}{Quality   of life instrument 233454}} & \multicolumn{1}{c}{\multirow{4}{*}{Utility   index global scale}}  & \multicolumn{1}{c}{0}                   & \multicolumn{1}{c}{\multirow{4}{*}{OR   (95\%CI) (from xyz regression) {[}N{]}}}  & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{\multirow{4}{*}{p\textgreater{}0.10   for all visits-specific comparisons; p=0.534 for overall comparison}} \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 1}              & \multicolumn{1}{c}{}                                                              & \multicolumn{2}{c}{0.762(0.703,0.823) {[}NR{]}}                                                        & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 2}              & \multicolumn{1}{c}{}                                                              & \multicolumn{1}{c}{0.762(0.703,0.823)   {[}NR{]}} & \multicolumn{1}{c}{0.849(0.803,0.893)   {[}NR{]}} & \multicolumn{1}{c}{}                                                                                                           \\* \cmidrule(lr){4-4} \cmidrule(lr){6-7}
\multicolumn{1}{c}{}                                & \multicolumn{1}{c}{}                                                     & \multicolumn{1}{c}{}                                               & \multicolumn{1}{c}{Week 3}              & \multicolumn{1}{c}{}                                                              & \multicolumn{2}{c}{0.762(0.703,0.823) {[}NR{]}}                                                        & \multicolumn{1}{c}{}                                                                                                           \\* \midrule
\\* \bottomrule

\end{longtable}
\end{landscape}


\end{document}

答案1

根据我的经验,除了最简单和最琐碎的情况外,表格生成器仍然无法为所有情况生成合适的表格。(而且,在这种情况下,人们可能一开始就不需要表格生成器的帮助,对吧?)无论如何,对于 OP 查询中显示的情况,就表格生成器运行的输出而言,几乎所有可能出错的事情都出错了,以至于无法使用。例如,通过将几乎每个单元格都封装在包装器中\multicolumn{1}{c}{...},代码确保不会发生自动换行。

不幸的是,不仅表格生成器实用程序的输出基本上无法使用,表格的设计也引发了一些问题。我尝试对代码进行一些彻底的清理,但我不确定我是否理解了表格的设计。(当然,标题单元格没有信息是没有帮助的。)据我所知,主要信息包含在第 4、6 和 7 列中。问题:为什么不把这些列放在一起?换句话说,交换第 4 列和第 5 列的顺序不是更好吗?

无论如何,以下代码可能有助于您开始构建一个完全可用的表格。

在此处输入图片描述

\documentclass[12pt,a4paper,twoside]{report}
\usepackage{booktabs,pdflscape,longtable,array,ragged2e}
   % note: 'multirow' package is no longer used

%% Define two new column types:
\newcolumntype{L}[1]{>{\RaggedRight}p{#1\linewidth}}
\newcolumntype{C}[1]{>{\Centering}p{#1\linewidth}}
%% Create two handy shortcut macros:
\newcommand\mytabc[1]{\begin{tabular}[t]{@{} c @{}} #1 \end{tabular}}
\newcommand\mytabcc[1]{\multicolumn{2}{@{}c@{}}{%
     \begin{tabular}[t]{@{} c@{\hspace{12pt}}c @{}} #1 \end{tabular}}}

\begin{document}

\begin{landscape}

\begingroup % limit the scope of the next two instructions
\footnotesize % switch to 10pt font
\setlength\tabcolsep{3pt} % default: 6pt
\begin{longtable}{@{} L{0.08} L{0.08} L{0.10} C{0.09} 
                      L{0.11} C{0.15} C{0.15} L{0.16} @{}} 

%% headers and footers:
\caption{This is the table caption}\\
\toprule
Source & % I've tried to come up with more meaningful labels
Quality of life instrument  & 
This is the third title of this table  & 
This is the fourth title of this table [N] & 
This is the fifth title of this table  & 
This is the sixth title of this table  [NR] & 
This is the seventh title of this table [NR] & 
$p$-values \\ 
\midrule
\endfirsthead

\multicolumn{8}{@{}l}{{\bfseries\tablename\ \thetable}, 
   continued from previous page} \\
\addlinespace
\toprule
Source  & 
Quality of life instrument  & 
This is the third title of this table  & 
This is the fourth title of this table [N] & 
This is the fifth title of this table  & 
This is the sixth title of this table [NR] & 
This is the seventh title of this table [NR] & 
$p$-values \\ 
\midrule
\endhead

\midrule
\multicolumn{8}{r@{}}{(Continued on next page)} \\
\endfoot
  
\bottomrule
\endlastfoot

%% body of table:
\multicolumn{8}{@{}l}{Primary endpoint XXXXX} \\ 
\midrule
author1, year & 233454 & Utility index global scale & 
  \mytabc{%
     0\\ 
     Week 1\\ 
     Week 2\\ 
     Week 3} & 
  OR (95\%CI) (from xyz regression) & 
  \mytabcc{%
     0.762 (0.703,0.823) & 0.849 (0.803,0.893) \\
     0.762 (0.703,0.823) & 0.849 (0.803,0.893) \\
     0.762 (0.703,0.823) & 0.849 (0.803,0.893) \\
     0.762 (0.703,0.823) & 0.849 (0.803,0.893)} &
  $p>0.10$ for all visit-specific comparisons; 
  $p=0.534$ for overall comparison \\ 
\midrule
author2, year & 233454 & Utility index global scale & 
  \mytabc{%
     0\\ 
     Week 1\\ 
     Week 2\\ 
     Week 3} & 
  OR (95\%CI) (from xyz regression) & 
  \mytabcc{%
     0.762 (0.703,0.823) & 0.849 (0.803,0.893) \\
     \multicolumn{2}{c}{0.762(0.703,0.823)} \\
     0.762 (0.703,0.823) & 0.849 (0.803,0.893)\\
     \multicolumn{2}{c}{0.762(0.703,0.823)}} &
  $p>0.10$ for all visit-specific comparisons; 
  $p=0.534$ for overall comparison \\ 
%\midrule

\end{longtable}
\endgroup

\end{landscape}

\end{document}

相关内容