我正在尝试制作一个包含我所有课程详细信息的表格。我编写了以下代码。它存在一些问题:
- 为什么编译时给出的是空白的第一页(第二页有表格)?
- 表格似乎在页面左边距留出了很大的空间,可以用来容纳更宽的第四列,但我似乎无法使用该空间。
- 表格超出了第二页底部的页码,并且在第二页显示的内容之后不显示任何内容。我还有其他几门课程,并且希望此表格跨越大约 3 页。我该怎么做?
注意:我把第二列设为 p 列,因为一些讲师的真实姓名需要两行。
以下是代码:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{booktabs}
\begin{document}
\begin{tabular}{p{2.5cm}p{2cm}lp{6cm}}
\toprule[2pt]
Name of Course & Instructor & Book(s) Referred & Brief List of Topics Covered \\
\hline
\\[\defaultaddspace]
Algebra I & ABC & Herstein; M. Artin &
\parbox{6cm}{Basic Set Theory; Group Theory\\(upto Sylow theorems);\\ Introduction to Ring Theory} \\
\\[\defaultaddspace]
Algebra II & DEF & Hoffman and Kunze &
\parbox{6cm}{Linear Algebra \\(upto Spectral theorems)} \\
\\[\defaultaddspace]
Algebra III & GHI & S. Lang &
\parbox{6cm}{Ring and Module Theory\\(covering Hilbert basis theorem and structure theorem for finitely generated abelian groups)}\\
\\[\defaultaddspace]
Algebra IV & JKL & S. Lang &
\parbox{6cm}{Introduction to Field \\ and Galois Theory}\\
\\[\defaultaddspace]
Analysis I & MNO & T. Apostol &
\parbox{6cm}{Real numbers; Sequences and series;\\ Calculus of a single variable \\ (upto Taylor's Theorem)
} \\
\\[\defaultaddspace]
Analysis II & PQR & T. Apostol &
\parbox{6cm}{Riemann integral; Introduction to \\ Calculus of several variables; \\ Metric spaces and elementary \\ topological notions on it} \\
\\[\defaultaddspace]
Analysis III & \parbox{2cm}{STU} & S. Dineen &
\parbox{6cm}{\textbf{Vector Calculus}: multiple integrals, \\ Jacobian formula, line and surface \\ integrals, Green's, Stokes' and Gauss' (Divergence) theorems; \\ Sequences of functions; Weierstrass \\ approximation theorem} \\
\\[\defaultaddspace]
Analysis IV & VWX & Rudin &
\parbox{6cm}{\textbf{Introduction to Function Spaces}:\\ compact metric spaces,\\ C([a,b]) as a complete metric space, \\ Banach's contraction principle, \\ Stone-Weierstrass and Arzela-Ascoli theorems, Fourier series } \\
\\[\defaultaddspace]
Probability Theory I & YZ & Chung; Feller &
\parbox{6cm}{Combinatorial probability and urn models; Conditional probability; \\ Random variables; Expectation; \\ Moment generating functions;\\ Standard distributions(Binomial etc.); \\ Joint and conditional distributions; \\ Weak law of large numbers} \\
\\[\defaultaddspace]
Probability Theory II & YZ & Feller &
\parbox{6cm}{Joint distributions; \\Transformation of variables\\(assuming Jacobian formula);\\ Distributions for sums, maxima/minima, order statistics, etc.; Standard multivariate and sampling distributions; Conditional expectation; Convergence of random variables} \\
\\[\defaultaddspace]
\bottomrule[2pt]
\end{tabular}
\end{document}
答案1
真正的问题是页面中没有足够的空间容纳表格。
我的建议是这样的:
如果您希望表格拆分到多页,请使用
longtable
同一个包,而不是。tabular
在这种情况下,表格可以保留在一页中。您可以使用该
geometry
包并仅使用以下命令减少表格所在页面的页边距\newgeometry{margin=2cm}
稍后通过发出以下命令恢复正常布局
\restoregeometry
将长度缩短
\defaultaddspace
至1pt
\setlength{\defaultaddspace}{1pt}
无需使用
\parbox
es 来使单元格内容居中。您可以加载array
包并使用m
代替p
。您还可以声明新的列类型M
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}}
您还可以使用
\newline
它在单元格内发出换行符。
完整示例:
\documentclass[a4paper,10pt]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm,array}
\usepackage{booktabs}
\usepackage{array,longtable}
\usepackage{lipsum} %only for the example
\newcolumntype{M}[1]{>{\raggedright\arraybackslash}m{#1}}
\setlength{\defaultaddspace}{1pt}
\begin{document}
\lipsum[1-5]
\newpage
\newgeometry{margin=2cm}
\begin{longtable}{p{2.5cm}p{2cm}lM{6cm}}
\toprule[2pt]
Name of Course & Instructor & Book(s) Referred & Brief List of Topics Covered \\
\hline
\\[\defaultaddspace]
Algebra I & ABC & Herstein; M. Artin &
Basic Set Theory; Group Theory\newline(upto Sylow theorems);\newline Introduction to Ring Theory \\
\\[\defaultaddspace]
Algebra II & DEF & Hoffman and Kunze &
Linear Algebra \newline(upto Spectral theorems) \\
\\[\defaultaddspace]
Algebra III & GHI & S. Lang &
Ring and Module Theory\newline(covering Hilbert basis theorem and structure theorem for finitely generated abelian groups) \\
\\[\defaultaddspace]
Algebra IV & JKL & S. Lang &
Introduction to Field \newline and Galois Theory\\
\\[\defaultaddspace]
Analysis I & MNO & T. Apostol &
Real numbers; Sequences and series;\newline Calculus of a single variable \newline (upto Taylor's Theorem) \\
\\[\defaultaddspace]
Analysis II & PQR & T. Apostol &
Riemann integral; Introduction to \newline Calculus of several variables; \newline Metric spaces and elementary \newline topological notions on it \\
\\[\defaultaddspace]
Analysis III & \parbox{2cm}{STU} & S. Dineen &
\textbf{Vector Calculus}: multiple integrals, \newline Jacobian formula, line and surface \newline integrals, Green's, Stokes' and Gauss' (Divergence) theorems; \newline Sequences of functions; Weierstrass \newline approximation theorem \\
\\[\defaultaddspace]
Analysis IV & VWX & Rudin &
\textbf{Introduction to Function Spaces}:\newline compact metric spaces,\newline C([a,b]) as a complete metric space, \newline Banach's contraction principle, \newline Stone-Weierstrass and Arzela-Ascoli theorems, Fourier series \\
\\[\defaultaddspace]
Probability Theory I & YZ & Chung; Feller &
Combinatorial probability and urn models; Conditional probability; \newline Random variables; Expectation; \newline Moment generating functions;\newline Standard distributions(Binomial etc.); \newline Joint and conditional distributions; \newline Weak law of large numbers \\
\\[\defaultaddspace]
Probability Theory II & YZ & Feller &
Joint distributions; \newline Transformation of variables\newline(assuming Jacobian formula);\newline Distributions for sums, maxima/minima, order statistics, etc.; Standard multivariate and sampling distributions; Conditional expectation; Convergence of random variables \\
\\[\defaultaddspace]
\bottomrule[2pt]
\end{longtable}
\newpage
\restoregeometry
\lipsum[1-5]
\end{document}
输出:
编辑
p
正如 egreg 在他的评论中注意到的那样,如果您使用普通列而不需要额外的\\
前面的列,表格可能会看起来更好\\[\defaultaddspace]
。
这是另一种可能性(选择您最喜欢的一种):
\documentclass[a4paper,10pt]{article}
\usepackage{geometry}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsthm,array}
\usepackage{booktabs}
\usepackage{array,longtable}
\usepackage{lipsum} %only for the example
\begin{document}
\lipsum[1-5]
\newpage
\newgeometry{margin=3cm}
\begin{longtable}{p{2.5cm}p{2cm}lp{6cm}}
\toprule[2pt]
Name of Course & Instructor & Book(s) Referred & Brief List of Topics Covered \\[\defaultaddspace]
\hline
\\
Algebra I & ABC & Herstein; M. Artin &
Basic Set Theory; Group Theory\newline(upto Sylow theorems);\newline Introduction to Ring Theory \\[\defaultaddspace]
Algebra II & DEF & Hoffman and Kunze &
Linear Algebra \newline(upto Spectral theorems) \\[\defaultaddspace]
Algebra III & GHI & S. Lang &
Ring and Module Theory\newline(covering Hilbert basis theorem and structure theorem for finitely generated abelian groups) \\[\defaultaddspace]
Algebra IV & JKL & S. Lang &
Introduction to Field \newline and Galois Theory \\[\defaultaddspace]
Analysis I & MNO & T. Apostol &
Real numbers; Sequences and series;\newline Calculus of a single variable \newline (upto Taylor's Theorem) \\[\defaultaddspace]
Analysis II & PQR & T. Apostol &
Riemann integral; Introduction to \newline Calculus of several variables; \newline Metric spaces and elementary \newline topological notions on it \\[\defaultaddspace]
Analysis III & \parbox{2cm}{STU} & S. Dineen &
\textbf{Vector Calculus}: multiple integrals, \newline Jacobian formula, line and surface \newline integrals, Green's, Stokes' and Gauss' (Divergence) theorems; \newline Sequences of functions; Weierstrass \newline approximation theorem \\[\defaultaddspace]
Analysis IV & VWX & Rudin &
\textbf{Introduction to Function Spaces}:\newline compact metric spaces,\newline C([a,b]) as a complete metric space, \newline Banach's contraction principle, \newline Stone-Weierstrass and Arzela-Ascoli theorems, Fourier series \\[\defaultaddspace]
Probability Theory I & YZ & Chung; Feller &
Combinatorial probability and urn models; Conditional probability; \newline Random variables; Expectation; \newline Moment generating functions;\newline Standard distributions(Binomial etc.); \newline Joint and conditional distributions; \newline Weak law of large numbers \\[\defaultaddspace]
Probability Theory II & YZ & Feller &
Joint distributions; \newline Transformation of variables\newline(assuming Jacobian formula);\newline Distributions for sums, maxima/minima, order statistics, etc.; Standard multivariate and sampling distributions; Conditional expectation; Convergence of random variables \\[\defaultaddspace]
\bottomrule[2pt]
\end{longtable}
\newpage
\restoregeometry
\lipsum[1-5]
\end{document}
答案2
我建议您对表格执行以下操作,假设它适合一页:
使用
tabularx
环境而不是tabular
环境,将其宽度设置为\textwidth
,并使用X
列类型作为最后一列。这会将最后一列的宽度设置为(\textwidth
减去其他三列的宽度)。在最后一列中使用 raggedright 排版而不是完全对齐;加载
ragged2e
包以允许以 raggedright 模式排版的单词连字。摆脱各种
\parbox
包装器并使用\newline
语句在第 4 列中引入换行符。(感谢@egreg!)将 many
\\ \\[\defaultaddspace]
指令替换为\\[\defaultaddspace]
。如果您确实需要比 提供的更多的垂直空白\defaultaddspace
,只需适当增加该参数的值即可。在下面的示例中,我使用 来2.5\defaultaddspace
在代数和分析课程组后提供更多的垂直空间。
假设1in
四周都有边距,你会得到以下结果:
\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,booktabs}
\usepackage{tabularx,ragged2e}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{@{} l p{1.75cm} l Y @{}}
\toprule[2pt]
Name of Course & Instructor & Book(s) Referred & Brief List of Topics Covered \\
\midrule
Algebra I & ABC & Herstein; M. Artin &
Basic Set Theory; Group Theory\newline
(upto Sylow theorems);\newline
Introduction to Ring Theory \\[\defaultaddspace]
Algebra II & DEF & Hoffman and Kunze &
Linear Algebra \newline
(upto Spectral theorems) \\[\defaultaddspace]
Algebra III & GHI & S. Lang &
Ring and Module Theory \newline
(covering Hilbert basis theorem and structure theorem for finitely generated
abelian groups)\\[\defaultaddspace]
Algebra IV & JKL & S. Lang &
Introduction to Field and Galois Theory \\[2.5\defaultaddspace]
Analysis I & MNO & T. Apostol &
Real numbers; Sequences and series;\newline
Calculus of a single variable \newline
(upto Taylor's Theorem) \\[\defaultaddspace]
Analysis II & PQR & T. Apostol &
Riemann integral; Introduction to Calculus of several variables; \newline
Metric spaces and elementary topological notions on it \\[\defaultaddspace]
Analysis III & STU & S. Dineen &
\textbf{Vector Calculus}: multiple integrals, Jacobian formula, line and
surface integrals, Green's, Stokes' and Gauss' (Divergence) theorems; \newline
Sequences of functions; \newline
Weierstrass approximation theorem \\[\defaultaddspace]
Analysis IV & VWX & Rudin &
\textbf{Introduction to Function Spaces}:\newline
compact metric spaces, $C([a,b])$ as a complete metric space, Banach's contraction principle,
Stone-Weierstrass and Arzela-Ascoli theorems, Fourier series \\[2.5\defaultaddspace]
Prob.\ Theory I & YZ & Chung; Feller &
Combinatorial probability and urn models; Conditional probability; \newline
Random variables; Expectation; \newline
Moment generating functions;\newline
Standard distributions (Binomial etc.); \newline
Joint and conditional distributions; \newline
Weak law of large numbers \\[\defaultaddspace]
Prob.\ Theory II & YZ & Feller &
Joint distributions; \newline
Transformation of variables (assuming Jacobian formula);\newline
Distributions for sums, maxima\slash minima, order statistics, etc.;\newline
Standard multivariate and sampling distributions;\newline
Conditional expectation; \newline
Convergence of random variables \\
\bottomrule[2pt]
\end{tabularx}
\end{document}
附录:扩展此设置非常简单,以便表格可以跨越多个页面。需要(i)加载长桌和文本表(除了tabularx
包)在序言中;(ii)对表的页眉/页脚结构进行一些更改,以使其符合语法longtable
:
首先将当前表的全部内容(从
\begin{tabularx}{\textwidth}{...}
到\end{tabularx}
)放入单独文件。将此文件命名为mybiglongtable.tex
。(我相信您可以想出一个更合适的名字……)在“驱动程序文件中”,插入指令
\LTXtable{\textwidth}{mybiglongtable.tex} % note explicit width indication
就在您希望表格开始的位置。(A
longtable
不是 LaTeX 意义上的“浮动对象”。)因此,前面的单页表格示例中的驱动程序文件将如下所示:\documentclass[a4paper,10pt]{article} \usepackage[utf8]{inputenc} \usepackage[margin=1in]{geometry} \usepackage{amsmath,amsthm,booktabs} \usepackage{tabularx,longtable,ltxtable,ragged2e} \newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} \begin{document} \LTXtable{\textwidth}{mybiglongtable.tex} \end{document}
在文件中
mybiglongtable.tex
,进行以下更改:改变
\begin{tabularx}{\textwidth}{@{} l p{1.75cm} l Y @{}}
到
\begin{longtable}{@{} l p{1.75cm} l Y @{}} % no explicit width indication
并将
\end{tabularx}
(在文件末尾)更改为\end{longtable}
。使用
longtable
语法,在之后立即提供所需的页眉和页脚信息\begin{longtable}{...}
,如下所示:\toprule[2pt] Name of Course & Instructor & Book(s) Referred & Brief List of Topics Covered \\ \midrule \endfirsthead \multicolumn{4}{@{}l}{\emph{Continued from previous page}}\\ \midrule[\heavyrulewidth] \endhead \midrule[\heavyrulewidth] \multicolumn{4}{r@{}}{\emph{Continued on following page}} \endfoot \bottomrule[2pt] \endlastfoot
请注意,表格顶部和底部的所有规则绘制命令都分配给命令
\endfirsthead
、\endhead
、\endfoot
和之一\endlastfoot
。文件的其余部分(直到
\end{longtable}
)应包含表格的“主体”,即与课程相关的信息。您可以像在早期的 MWE 中一样使用\\[\defaultaddspace]
和\\[2.5\defaultaddspace]
来提供内容的一些视觉分组。