当我在表格中输入超过 3 行文本时,表格就会折叠,有人可以帮忙吗?
以下是代码:
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{longtable}
\usepackage{textalpha}
\usepackage{ragged2e}
\usepackage[T1]{fontenc} % <-- new
\usepackage{xltabular}
\usepackage{amsmath}
\usepackage{caption}
\begin{document}
\newcolumntype{L}{>{\RaggedRight}X} % suspend full justification
% special new column type for header row:
\newcolumntype{C}{>{\centering\arraybackslash}%
p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}}
\begin{xltabular}{\textwidth}{ | l | L | c | } % <-- new
%% headers and footers
\caption{DUSP genes in normal and pathological brain function\label{long4}}\\
\hline
\multicolumn{3}{ | C | }{DUSP genes in normal and pathological brain function}\\
\hline
Isoforms & Function in Normal Brain & Function in Pathological Brain \\
\hline
\endfirsthead
\hline
\multicolumn{3}{|c|}{Continuation of Table \ref{long4}}\\
\hline
Isoforms & Function in Normal Brain& Function in Pathological Brain \\
\hline
\endhead
\hline
\endfoot
\hline
\multicolumn{3}{| c |}{End of Table}\\
\hline
\hline
\endlastfoot
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
\end{xltabular}
% This is footnote
\begin{minipage}{10cm}
\small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.
\end{minipage}
\end{document}
这是理想的形式。
代码给出了折叠的表格:
答案1
您需要允许第 2 列和第 3 列自动换行。因此,我建议您更换
\begin{xltabular}{\textwidth}{ | l | L | c | }
和
\begin{xltabular}{\textwidth}{ | l | L | L | }
如果出于某种原因,您希望第 3 列单元格的内容居中而不是左对齐,我建议您执行
\newcolumntype{Z}{>{\Centering}X}
在序言中并使用
\begin{xltabular}{\textwidth}{ | l | L | Z | }
代替\begin{xltabular}{\textwidth}{ | l | L | L | }
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{ragged2e}
\usepackage[T1]{fontenc}
\usepackage{xltabular}
\usepackage{amsmath}
\usepackage{caption}
\newcolumntype{L}{>{\RaggedRight}X} % suspend full justification
% special new column type for header row:
\newcolumntype{C}{>{\centering\arraybackslash}%
p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}}
\begin{document}
\begin{xltabular}{\textwidth}{ | l | L | L | } % <-- new
%% headers and footers
\caption{DUSP genes in normal and pathological brain function\label{long4}}\\
\hline
\multicolumn{3}{ | C | }{DUSP genes in normal and pathological brain function}\\
\hline
Isoforms & Function in Normal Brain & Function in Pathological Brain \\
\hline
\endfirsthead
\hline
\multicolumn{3}{|c|}{Continuation of Table \ref{long4}}\\
\hline
Isoforms & Function in Normal Brain& Function in Pathological Brain \\
\hline
\endhead
\hline
\endfoot
\hline
\multicolumn{3}{| C |}{End of Table}\\
\hline
\endlastfoot
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
\end{xltabular}
\noindent
\begin{minipage}{\textwidth}
\small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.
\end{minipage}
\end{document}
答案2
稍微扩展一下米科的答案。原因是您的屏幕截图中的两列宽度不同。X
如果您指定明确的比例,则可以使用 -type columns 实现此效果。格式为
\newcolumntype{Y}{>{\hsize=<factor>\hsize\linewidth=\hsize}X}
\newcolumntype{Z}{>{\hsize=<2-factor>\hsize\linewidth=\hsize}X}
请注意,因子应加起来等于类型的列数X
,如2
示例中所示。解释如下文档第 4.3 节。在下面的例子中,我选择了相对宽度 0.8 和 1.2(请注意,根据要求,0.8+1.2=2),这意味着可用的第二个X型柱的宽度比第一个大50%。
我会将注释作为表格的一部分,以避免分页时意外分离。我还会将表格拉伸一点,为行添加更多空间。
这是最终的代码
\documentclass{report}
\usepackage{graphicx} % Required for inserting images
\usepackage{setspace}% for\doublespacing
\usepackage{indentfirst}% for indent the first line
\usepackage{textalpha}
\usepackage{ragged2e}
\usepackage[T1]{fontenc} % <-- new
\usepackage{xltabular}
\usepackage{amsmath}
\usepackage{caption}
\newlength\tabwidth
\newcolumntype{Y}{>{\RaggedRight\hsize=.8\hsize\linewidth=\hsize}X}
\newcolumntype{Z}{>{\RaggedRight\hsize=1.2\hsize\linewidth=\hsize}X}
\newcolumntype{C}{>{\centering\arraybackslash}p{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth}}
\begin{document}
\begingroup
\renewcommand{\arraystretch}{1.25}
\begin{xltabular}{\textwidth}{|l|Y|Z|} % <-- new
%% headers and footers
\caption{DUSP genes in normal and pathological brain function\label{long4}}\\
\hline
\multicolumn{3}{|C|}{DUSP genes in normal and pathological brain function}\\
\hline
Isoforms
& Function in Normal Brain
& Function in Pathological Brain \\
\hline
\endfirsthead
\hline
\multicolumn{3}{|C|}{Continuation of Table \ref{long4}}\\
\hline
Isoforms
& Function in Normal Brain
& Function in Pathological Brain \\
\hline
\endhead
\hline
\endfoot
\hline
\multicolumn{3}{|C|}{End of Table}\\
\hline
\hline
\multicolumn{3}{@{}p{\linewidth}@{}}{%
\rule{0pt}{0.7\normalbaselineskip}%
\footnotesize
AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.}
\endlastfoot
SSH1
& long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2
& long text long text long text long text
& long text long text long text long text \\
SSH1
& long text long text long text long text long text long text
& long textlong text long text long text long textlong text long text long text \\
\end{xltabular}
\endgroup
\end{document}
答案3
longtblr
您也可以使用包来编写表格tabularray
。代码更短,但编译时间稍长:
\documentclass{report}
\usepackage[T1]{fontenc}
%\usepackage{graphicx} % Required for inserting images
%\usepackage{indentfirst}% for indent the first line
\usepackage{ragged2e}
\usepackage{tabularray}
\UseTblrLibrary{amsmath, booktabs}
\SetTblrStyle{contfoot}{font=\footnotesize\itshape}
\SetTblrStyle{note}{font=\small}
\begin{document}
\begin{longtblr}[
caption = {DUSP genes in normal and pathological brain function},
label = {long4},
note{} = {AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.},
]{vlines,
colspec = { l *{2}{X[cmd=\RaggedRight]} },
cell{1,Z}{1} = {c=3}{c},
row{3-Y} = {rowsep=3pt},
row{2} = {c},
rowhead = 2
}
% column headers
\toprule
DUSP genes in normal and pathological brain function
& & \\
\midrule
Isoforms
& Function in Normal Brain
& Function in Pathological Brain \\
\midrule
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
% table body
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text \\
SSH2 & long text long text long text long text
& long text long text long text long text \\
SSH1 & long text long text long text long text long text long text
& long textlong text long text long text long text long text long text long text
\\
\midrule
End of Table
& & \\
\bottomrule
\end{longtblr}
\end{document}
如您所见,上面的 MWE 与您的 MWE 相比进行了以下更改:
- 对于水平表格线,使用
booktabs
包中定义的规则。 - 对于单元格中对齐的文本使用
ragged2e
包
如果您希望第二列和第三列的宽度不同,您可以获取此 pa 缩放X
说明符,例如:
colspec = { l X[cmd=\RaggedRight]
X[1.3, cmd=\RaggedRight]},
第三列比第二列宽 30%。
答案4
另一个答案基于tabularray
包裹。
\documentclass{report}
\usepackage{tabularray}
% Header for pages other than the 1st one
\DefTblrTemplate{conthead-text}{MyContTxt}{
\begin{tblr}{
colspec={lX[l]X[l]},
vlines,
}
\hline
\SetCell[c=3]{c}Continuation of Table & & \\
\hline
Isoforms & Function in Normal Brain & Function in Pathological Brain\\
\end{tblr}
}
\DefTblrTemplate{capcont}{MyTmp}{%
\UseTblrTemplate{conthead-text}{MyContTxt}
}
% Remove the "Continued on next page" text from footer
\DefTblrTemplate{firstfoot, middlefoot}{MyFoot}{%
%
}
\NewTblrTheme{MyTheme}{
\SetTblrTemplate{capcont}{MyTmp}%
\SetTblrTemplate{firstfoot, middlefoot}{MyFoot}%
}
\begin{document}
\begin{longtblr}[
theme = MyTheme,
caption = {DUSP genes in normal and pathological brain function},
label = {long4},
headsep= -1pt,
note{} = {\small AD: Alzheimer’s disease; HD: Huntingdon’s disease; CMT4: Charcot-Marie-Tooth Neuropathy Type 4.},
]{
colspec = {lX[l]X[l]},
width = \textwidth,
vlines,
}
\hline
\SetCell[c=3]{c}DUSP genes in normal and pathological brain function & &\\
\hline
Isoforms & Function in Normal Brain & Function in Pathological Brain \\
\hline
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text \\
SSH2 & long text long text long text long text & long text long text long text long text \\
SSH1 & long text long text long text long text long text long text & long textlong text long text long text long textlong text long text long text \\
\hline
\SetCell[c=3]{c} End of Table & &\\
\hline
\end{longtblr}
\end{document}
礼貌:这个答案随后在除第一页之外的页面中添加了不同的页眉。