我有多行表。
\documentclass[11pt,table,a5paper]{article}
\usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
\usepackage{collcell}
\usepackage{longtable}
\usepackage{color}
\usepackage{colortbl}
\begin{document}
\definecolor{textcol}{rgb}{.118, .565, 1.00}
\definecolor{rowcol}{rgb}{.218, .565, 1.00}
\textbf{Antioxidant/Longevity }
~\\
{\renewcommand{\arraystretch}{1.8}\begin{longtable} {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
\hline
\rowcolor{textcol}
\textbf{\textcolor{white}{Title1}} &\textbf{\textcolor{white}{Title2}} &\textbf{\textcolor{white}{Title3}} &\textbf{\textcolor{white}{Title4}}\\
\hline
\rowcolor{rowcol}
54880/RET2& -& 1&-\\
\hline
\rowcolor{rowcol}
rs5746136/SOD2& First Line~\\
~\\
Second Line & 1& some text\\
\hline
\end{longtable}}
\end{document}
但输出不是多行。我该如何解决这个问题?
答案1
此代码是否满足您的要求?我使用包;它允许我使用带星号的版本或makecell
来替换更改,后者是格式化(多行)单元格的命令。这些带星号的版本在单元格的顶部和底部对称地添加了一个小的垂直间距,因此行中单元格的内容垂直居中,而更改 \baselinestretch 则不会出现这种情况。\baselinestretch
\makecell
thead
我还使用了tabulary
tabularx 的一个变体,这样,给定表格的总宽度,列的宽度将基于该列中最宽的单元格。由于它与 \thead 配合使用效果不佳(第一列中无法正确显示 \thead),我不得不通过添加第一个空列来解决这个问题。
\documentclass[11pt,table,a5paper]{article}
\usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
\usepackage{collcell}
\usepackage{ltablex}
\usepackage{tabulary}
\usepackage{color}
\usepackage{colortbl}
\usepackage{makecell}
\renewcommand{\theadfont}{\color{white}\bfseries}%
\renewcommand{\theadalign}{lc}
\renewcommand{\cellalign}{lc}
\definecolor{textcol}{rgb}{.118, .565, 1.00}
\definecolor{rowcol}{rgb}{.218, .565, 1.00}
\begin{document}
\begin{tabulary}{\linewidth}{| l@{}L|L|C|L|}%{|*{4}{p{0.2\linewidth}|}}% {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
\multicolumn{5}{l}{\bfseries Antioxidant/Longevity}\\
\hline
\rowcolor{textcol}%
& \thead*{Title1} &\thead{{Title2}} &\thead{Title3} &\thead{Title4}\\
\hline
\rowcolor{rowcol}
& \makecell*{54880/RET2} & --& 1&--\\
\hline
\rowcolor{rowcol}
& rs5746136/SOD2& \makecell*{First Line\\Second Line}& 1& some text\\
\hline
\end{tabulary}
\end{document}
答案2
不要用于~\\
段落分隔符(在表格内或外面)
\documentclass[11pt,table,a5paper]{article}
\usepackage[top=2cm, bottom=2cm, outer=2.1cm, inner=1cm,twoside, headsep=26pt]{geometry}
\usepackage{collcell}
\usepackage{longtable}
\usepackage{color}
\usepackage{colortbl}
\begin{document}
\definecolor{textcol}{rgb}{.118, .565, 1.00}
\definecolor{rowcol}{rgb}{.218, .565, 1.00}
\textbf{Antioxidant/Longevity }
{\renewcommand{\arraystretch}{1.8}\begin{longtable} {|p{2.8cm}|p{4.7cm}|p{1cm}|p{2.1cm}|}
\hline
\rowcolor{textcol}
\textbf{\textcolor{white}{Title1}} &\textbf{\textcolor{white}{Title2}} &\textbf{\textcolor{white}{Title3}} &\textbf{\textcolor{white}{Title4}}\\
\hline
\rowcolor{rowcol}
54880/RET2& -& 1&-\\
\hline
\rowcolor{rowcol}
rs5746136/SOD2& First Line
Second Line & 1& some text\\
\hline
\end{longtable}}
\end{document}