我有一张分布在多个页面上的长表,我使用 packagelongtable
包对其进行了编码。我的代码示例如下:
\documentclass[a4paper]{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{cccccccp{5cm}} \hline
Row & GO term & P-value & OR & Exp Count & Count & Size
\\ \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\
8 & GO:0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\
9 & GO:0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\
10 & GO:0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\
\hline
\end{longtable}
\end{document}
但是,打印出来的表格向右移动了,这让我很烦恼。我该如何解决这个问题?我尝试添加\begin{center}
,centering
但没有成功。
答案1
表格在 MWE 中并没有“向右移动”。相反,MWE 的表格比 \textwidth(即文本块的宽度)更宽。这使得其内容突出到文本块的右侧。
假设文档的左侧和右侧边距实际上各为 1 英寸(2.54 厘米)宽,则可以使环境longtable
(刚好!)适合文本块如果你还可以删除第一列左侧和最后一列右侧的空格,即,如果你指定
\usepackage[margin=1in]{geometry}
在序言和
\begin{longtable}{@{} *{7}{c} p{5cm} @{}}
对于长桌本身。
但是,使用此设置,列中的文本材料p
将完全对齐,由于列宽只有 5 厘米,因此偶尔会出现很大的单词间隙。对于如此窄的列,最好将材料排版为右对齐,而不是完全对齐。因此,我建议您在序言中提供以下命令:
\usepackage{longtable}
\usepackage{array} % for "\newcolumntype" macro
\usepackage{ragged2e} % for "\RaggedRight" macro
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\usepackage[margin=1in]{geometry}
然后将longtable
环境设置为:
\begin{longtable}{@{} *{7}{c} P{5cm} @{}}
使用P
列类型,您将成功使该列的材料排版不正确,同时仍允许使用连字符。简单的\raggedright
宏会禁用连字符;这就是我建议在列类型\RaggedRight
的定义中使用的原因P
。
以下是实现这些建议的代码的输出。(左右两侧的黑色框线是我设置包showframe
的选项后画出来的geometry
。)
\documentclass[a4paper]{article}
\usepackage[showframe,margin=1in]{geometry}
\usepackage{longtable}
\usepackage{array}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\begin{document}
\begin{longtable}{@{} *{7}{c} P{5cm} @{}}
\hline
Row & GO term & P-value & OR & Exp Count & Count & Size \\
\hline
\endhead
\hline
\endfoot
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\
8 & GO:0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\
9 & GO:0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\
10 & GO:0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\
\end{longtable}
\end{document}
答案2
基于 的解决方案tabularx
。我还使用array
包,因为它的*{n}{}
和>{…}
构造可以节省输入,并booktabs
改善表格的外观。该siunitx
包用于对齐数字列。最后,我实现了行编号和GO:
标签的自动化。
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{array, booktabs, siunitx}
\sisetup{table-number-alignment = center, parse-numbers = false}
\usepackage{ltablex}
\usepackage{ragged2e}
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight\small\arraybackslash}m{#1}}
\newcounter{rowcnt}
\begin{document}
\mbox{}\vskip 2cm
\setlength\tabcolsep{3pt}\renewcommand\arraystretch{1.25}\setcounter{rowcnt}{0}
\begin{tabularx}{\linewidth}{>{\refstepcounter{rowcnt}}c >{GO:}cccS[table-format=4.2]S[table-format=4.0]S[table-format=5.0]X}%
\toprule
\multicolumn{1}{c}{Row} & \multicolumn{1}{c}{GO term} & P-value & OR & {Exp Count} & {Count} & {Size} \\
\cmidrule[\lightrulewidth](l{3pt}r){1-8}\addlinespace
1 & 0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\
2 & 0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\
3 & 0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\
4 & 0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\
5 & 0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\
6 & 0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\
7 & 2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\
8 & 0031323 & 0.00 & 1.63 & 378.24 & 531 & 2457 & regulation of cellular metabolic process \\
9 & 0051171 & 0.00 & 1.66 & 332.52 & 476 & 2160 & regulation of nitrogen compound metabolic process \\
10 & 0019219 & 0.00 & 1.66 & 328.52 & 469 & 2134 & regulation of nucleobase-containing compound metabolic process \\
\bottomrule
\end{tabularx}
\end{document}
答案3
问题是表格比文本宽度要宽。使用时\usepackage[showframe]{geometry}
,您会看到边框:
\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage[showframe]{geometry}
\begin{document}
\begin{longtable}{cccccccp{5cm}} \hline
Row & GO term & P-value & OR & Exp Count & Count & Size
\\ \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process \\
2 & GO:0050794 & 0.00 & 1.57 & 648.88 & 857 & 4215 & regulation of cellular process \\
3 & GO:0080090 & 0.00 & 1.67 & 376.24 & 536 & 2444 & regulation of primary metabolic process \\
4 & GO:0009889 & 0.00 & 1.69 & 344.84 & 497 & 2240 & regulation of biosynthetic process \\
5 & GO:0051252 & 0.00 & 1.72 & 308.20 & 453 & 2002 & regulation of RNA metabolic process \\
6 & GO:0006355 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of transcription, DNA-dependent \\
7 & GO:2001141 & 0.00 & 1.71 & 306.35 & 449 & 1990 & regulation of RNA biosynthetic process \\
\hline
\end{longtable}
\end{document}
所以你应该
- 桌子较小或
- 更改页面布局(边距) - 查看所提到的
goemetry
包。
答案4
正如 Manuel Kuehner 所说,您的桌子比 更宽\textwidth
。您可以
\textwidth
只需通过\usepackage[cm]{fullpage}
或(如上所述)使用即可放大geometry
。- 在表格左侧手动添加负空间,如下:
\documentclass[a4paper]{article}
\usepackage{longtable}
\begin{document}
\begin{longtable}{cccccccp{5cm}} \hline
Row & GO term & P-value & OR & Exp Count & Count & Size \\ \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process
\end{longtable}
\hspace*{-3cm}\begin{minipage}{5cm}\begin{longtable}{cccccccp{5cm}} \hline
Row & GO term & P-value & OR & Exp Count & Count & Size \\ \hline
1 & GO:0009987 & 0.00 & 1.65 & 1811.17 & 2078 & 11765 & cellular process
\end{longtable}\end{minipage}
\end{document}