使用此表格环境时,我需要将第二行的单元格内容放在每个单元格的中间。但是,我将它们全部对齐到单元格的顶部(尽管它们垂直居中)。有人可以指导我我犯了什么错误吗?
\documentclass[12pt,twoside,openright]{report}
\usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{float}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{array,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{longtable}
%\usepackage[dvips]{color} % comment it to run with png images and pdflatex
%\usepackage{epsfig} %Sharma commented
\usepackage{graphicx} % Sharma introduced
%\usepackage{mciteplus}
\usepackage[sectionbib]{chapterbib}
\usepackage[sort&compress,numbers]{natbib}
\usepackage[nottoc]{tocbibind}
\usepackage{enumerate}
\usepackage{bm}
\usepackage{braket}
\usepackage{microtype}
\usepackage[titletoc]{appendix}
\usepackage{color}
\usepackage{fixltx2e}
\usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}
\setlength{\textheight}{23cm}
\setlength{\textwidth}{15cm}
\setlength{\oddsidemargin}{0.4in}
\setlength{\evensidemargin}{0in}
\begin{document}
\begin{table}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|*{16}{>{\centering\arraybackslash}m{0.8cm}|}} \hline
\multicolumn{4}{|c|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{
BN-partial-ed-GQD} \\ \hline
\multicolumn{2}{|M{1.6cm}|}{Avg. edge} & \multicolumn{2}{C{1.6cm}|}{Avg. 2$^{nd}$ zigzag line} &
\multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} &
\multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} &
\multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\ \hline
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
spin-${\beta}$ &
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
spin-${\beta}$ &
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\ \hline
19.02 & 19.02 & 21.48 & 21.49 & 20.81 & 20.81 & 22.26 & 22.26 & 11.99 & 10.82 & 10.85 & 12.01 &
7.21 & 7.81 & 8.38 & 7.81 \\ \hline
\multicolumn{4}{|c|}{ Charge transfer} & - 0.67 & - 0.67 & + 0.78 & + 0.78 & + 0.58 & - 0.59 & -
0.56 &
+ 0.6 & - 0.61 & + 0.01 & + 0.57 & - 0.002 \\ \hline
\end{tabular}
}
\end{table}
\end{document}
请帮助我。 附言:有一些不必要的包(对于这个表来说),但它们是我的论文所必需的。所以我保留了它们(如果它们偶然会造成任何问题,请告诉我)。
答案1
您可以将p
的定义更改为。C
m
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
完整代码:
\documentclass[12pt,twoside,openright]{report}
\usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{float}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{array,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{longtable}
%\usepackage[dvips]{color} % comment it to run with png images and pdflatex
%\usepackage{epsfig} %Sharma commented
\usepackage{graphicx} % Sharma introduced
%\usepackage{mciteplus}
\usepackage[sectionbib]{chapterbib}
\usepackage[sort&compress,numbers]{natbib}
\usepackage[nottoc]{tocbibind}
\usepackage{enumerate}
\usepackage{bm}
\usepackage{braket}
\usepackage{microtype}
\usepackage[titletoc]{appendix}
\usepackage{color}
\usepackage{fixltx2e}
\usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}
\setlength{\textheight}{23cm}
\setlength{\textwidth}{15cm}
\setlength{\oddsidemargin}{0.4in}
\setlength{\evensidemargin}{0in}
\begin{document}
\begin{table}
\centering
\resizebox{\columnwidth}{!}{%
\begin{tabular}{|*{16}{>{\centering\arraybackslash}m{0.8cm}|}} \hline
\multicolumn{4}{|c|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{
BN-partial-ed-GQD} \\ \hline
\multicolumn{2}{|C{1.6cm}|}{Avg. edge} & \multicolumn{2}{C{1.6cm}|}{Avg. 2$^{nd}$ zigzag line} &
\multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} &
\multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} &
\multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\ \hline
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
spin-${\beta}$ &
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ &
spin-${\beta}$ &
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\ \hline
19.02 & 19.02 & 21.48 & 21.49 & 20.81 & 20.81 & 22.26 & 22.26 & 11.99 & 10.82 & 10.85 & 12.01 &
7.21 & 7.81 & 8.38 & 7.81 \\ \hline
\multicolumn{4}{|c|}{ Charge transfer} & - 0.67 & - 0.67 & + 0.78 & + 0.78 & + 0.58 & - 0.59 & -
0.56 &
+ 0.6 & - 0.61 & + 0.01 & + 0.57 & - 0.002 \\ \hline
\end{tabular}
}
\end{table}
\end{document}
答案2
我认为,这里最好使用tabularx
。此外,还可以使用 的值\tabcolsep
和字体大小。使用\scriptsize
,最后一行单元格只能使用一行。我添加了cellspace
包,在单元格的顶部和底部有一些垂直填充,并用 替换2$^{nd}$
(这无论如何都是错误的:它会产生斜体 nd,就像两个变量一样) 。最后,我用\multicolumn2\textsuperscript{nd}
替换了您的C
列类型(其宽度计算宽度没有考虑tabcolsep
和的值arrayrulewidth
) ,并在需要时使用手动换行符。不需要:c
makecell
\adjustwidth
\documentclass[a4paper]{article}
\usepackage[hmargin=2.5cm, showframe, nomarginpar]{geometry}
\usepackage{float}
\usepackage{multirow}
\usepackage{array,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}S{m{#1}}}
\usepackage{longtable}
\usepackage{adjustbox}
\usepackage{tabularx}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{makecell}
\usepackage{cellspace}
\setlength\cellspacetoplimit{3pt}
\setlength\cellspacebottomlimit{3pt}
\addparagraphcolumntypes{X, C}
\begin{document}
\begin{table}
\centering\setlength\tabcolsep{3pt}\footnotesize
\begin{tabularx}{\linewidth}{|*{16}{S{X}|}} \hline
\multicolumn{4}{|Sc|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{ BN-partial-ed-GQD} \\
\hline
\multicolumn{2}{|Sc|}{Avg. edge} & \multicolumn{2}{Sc|}{\makecell{Avg. 2\textsuperscript{nd} \\ zigzag line}}
& \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} & \multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\
\hline
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\
\hline
\end{tabularx}
\end{table}
\begin{table}[!ht]
\centering\setlength\tabcolsep{1.5pt}\scriptsize
\begin{tabularx}{\linewidth}{|*{16}{S{X}|}} \hline
\multicolumn{4}{|Sc|}{ GQD} & \multicolumn{4}{c|}{ BN-zigzag-ed-GQD} & \multicolumn{8}{c|}{ BN-partial-ed-GQD} \\
\hline
\multicolumn{2}{|c|}{Avg. edge} & \multicolumn{2}{Sc|}{\makecell{Avg. 2\textsuperscript{nd}\\ zigzag line}} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} & \multicolumn{2}{c|}{ B-edge} & \multicolumn{2}{c|}{ N-edge} & \multicolumn{2}{c|}{ B-border} & \multicolumn{2}{c|}{ N-border} \\ \hline
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\
\hline
\end{tabularx}
\end{table}
\end{document}
答案3
这就是你要找的吗?
编辑(2): 在编辑您的问题后,我采用了对您提供的新数据的回答。在此,我更改了您的序言:
- 将包移动
hypperref
到前言的末尾,这是它应该在的位置(因为它可以更改其他包中的某些设置) - 立即将
geometry
包裹移至原处documentclass
geometry
删除文本大小的设置...因为它们已经由包决定注释掉我笔记本电脑中未安装且不影响表格格式的所有软件包
在设计您的表格时,我使用了您对列类型的定义
M
。我在表格序言中以及multicolumns
表格的第二行中使用了 iz。我还删除了resizebox
,在我看来,在这种情况下应该避免使用它。更好的方法是确定较小的字体——在这种情况下我使用\footnotesize
——并减少列间空间。
代码:
\documentclass[12pt,twoside,openright]{report}
\usepackage[top=1.0in, bottom=1.0in, left=1.0in, right=1.0in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{float}
\usepackage{multicol}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{array,booktabs}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}
%\usepackage[dvips]{color} % comment it to run with png images and pdflatex
%\usepackage{epsfig} %Sharma commented
\usepackage{graphicx} % Sharma introduced
%\usepackage{mciteplus}
%\usepackage[sectionbib]{chapterbib}
%\usepackage[sort&compress,numbers]{natbib}
%\usepackage[nottoc]{tocbibind}
\usepackage{enumerate}
\usepackage{bm}
\usepackage{braket}
\usepackage{microtype}
%\usepackage[titletoc]{appendix}
\usepackage{color}
\usepackage[linktocpage,colorlinks=true,linkcolor=black,citecolor=black]{hyperref}
\usepackage{fixltx2e}
%surplus settings ... this determine geometry package!
%\setlength{\textheight}{23cm}
%\setlength{\textwidth}{15cm}
%\setlength{\oddsidemargin}{0.4in}
%\setlength{\evensidemargin}{0in}
\begin{document}
\medskip
\begin{table}[h]
\setlength{\tabcolsep}{3pt}
\centering
\footnotesize
\begin{tabular}{|*{16}{>{\centering\arraybackslash}M{0.8cm}|}} \hline
\multicolumn{4}{|c|}{ GQD}
& \multicolumn{4}{c|}{BN-zigzag-ed-GQD}
& \multicolumn{8}{c|}{BN-partial-ed-GQD} \\ \hline
\multicolumn{2}{|M{1.6cm}|}{Avg. edge}
& \multicolumn{2}{M{1.6cm}|}{Avg. 2$^{nd}$ zigzag line}
& \multicolumn{2}{M{1.6cm}|}{ B-border}
& \multicolumn{2}{M{1.6cm}|}{ N-border}
& \multicolumn{2}{M{1.6cm}|}{B-edge}
& \multicolumn{2}{M{1.6cm}|}{ N-edge}
& \multicolumn{2}{M{1.6cm}|}{ B-border}
& \multicolumn{2}{M{1.6cm}|}{ N-border} \\ \hline
spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ & spin-${\alpha}$ & spin-${\beta}$ \\ \hline
\end{tabular}
\end{table}
\end{document}
以上代码已经测试。
在我的回答中,我仅关注单元格内容的垂直和水平居中。如果您想改进表格布局,请考虑使用makecell
包及其宏\thead{...}
来设置表格列的头部以及扩大单元格内容上方和下方间隙的其他可能性(请参阅包文档)。