我正在尝试用乳胶复制手绘表格。
到目前为止我有以下代码
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools}
\usepackage{cancel}
\usepackage{pgfplots}
\usepackage{amsmath}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{amssymb}
\usetikzlibrary{patterns}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{color,xcolor}
\usepackage{booktabs,array}
\usepackage{hyperref}
\usepackage{graphicx}
\usetikzlibrary{arrows}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{blkarray}
\usepackage{caption}
\usepackage{adjustbox}
\usepackage{multirow}
\usepgfplotslibrary{fillbetween}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}
\begin{center}{
\setlength\arraycolsep{6pt} % default is "5pt"
$\begin{array}{lccc}
\midrule
\multirow{2}{*}{SECTION A} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of}\\\text{questions}\end{array}} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of questions}\\\text{to be answered}\end{array}} & \multicolumn{1}{l}{\begin{array}{@{}c@{}}\text{Number of}\\\text{marks}\end{array}}\\[0.224cm]
\hline
& \multicolumn{1}{c}{7} & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{36} \\[0.224cm]
\hline
\text{SECTION B} & \begin{array}{@{}c@{}}\text{Number of}\\\text{questions}\end{array} & \begin{array}{@{}c@{}}\text{Number of questions}\\\text{to be answered}\end{array} & \begin{array}{@{}c@{}}\text{Number of}\\\text{marks}\end{array}\\[0.124cm]
\hline
& 4 & 2 & 24\\
\hline
& & & \text{Total }60\\
\hline
\end{array}$}
\end{center}
\end{document}
它似乎生成了有点类似的表格(如下所示),但是 A 部分和 B 部分的第一列中并没有出现多行。
我该如何实现这一点?我还想让表格中的文本水平和垂直居中。
谢谢!
答案1
尝试
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{caption}
\usepackage{booktabs, makecell, multirow}
\begin{document}
\begin{center}
\begin{tabular}{lccc}
\toprule
\multirow{3}{*}{SECTION A}
& \makecell{Number of\\ questions}
& \makecell{Number of questions\\ to be answered}
& \makecell{Number of\\ marks} \\
\cmidrule{2-4}
& 7 & 7 & 36 \\
\midrule
\multirow{3}{*}{SECTION B}
& \makecell{Number of\\ questions}
& \makecell{Number of questions\\ to be answered}
& \makecell{Number of\\ marks} \\
\cmidrule{2-4}
& 4 & 2 & 24 \\
\midrule
& & & Total 60 \\
\bottomrule
\end{tabular}
\end{center}
\end{document}
这就是你要找的吗?
如您所见,在上面的 mwe(最小工作示例)中,我从代码中删除了所有与表无关的内容。同时,我还删除了所有不需要的代码复杂化,因为用于\multicolumn
一列单元格。请注意,而是hline
使用来自的规则booktabls
:\toprule
和。对于两行单元格\midrule
,\bottomrule
使用\makecell
来自\makecell
包的内容。
答案2
使用非常强大和多功能包的替代方案tabu
:
显然该tabu
包被认为是过时的(感谢@Zarko 的提醒),但使用该tabulary
包可以实现相同的效果。
\documentclass{article}
\usepackage{tabulary}
\usepackage{booktabs}
\usepackage{multirow}
\begin{document}
\begin{tabulary}{0.8\linewidth}{l*{3}{C}}
\toprule
\multirow{2}{*}{SECTION A} & Number of questions & Number of questions to be answered & Number of marks \\
\cmidrule{2-4}
& 7 & 7 & 36 \\\midrule
\multirow{2}{*}{SECTION B} & Number of questions & Number of questions to be answered & Number of marks \\\cmidrule{2-4}
& 4 & 2 & 24 \\\midrule
& & & Total 60\\ \bottomrule
\end{tabulary}
\end{document}
但我相信如果您使用这样的表格,您实际上并不需要多行:
和
\begin{tabulary}{0.8\linewidth}{l*{3}{C}}
\toprule
& Number of questions & Number of questions to be answered & Number of marks \\
\midrule
Section A & 7 & 7 & 36 \\
Section B & 4 & 2 & 24 \\
\midrule
Total & & & 60\\
\bottomrule
\end{tabulary}
答案3
这是一个基于的解决方案,代码更简单makecell
。我认为您不需要array
为这个表创建一个环境,tabular
在这里就很好了,并且系统地使用规则表单booktabs
将节省您输入额外空格的时间,[0.224mm]
因为这些规则包含了一些垂直填充。
另外,不要多次加载一个包。请注意,xcolor
已加载color
、mathtools
加载amsmath
、blkarray
加载array
。最后,但并非最不重要的是,hyperref
应作为最后一个包加载,极少数例外。
\documentclass[]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{pgfplots}
\renewcommand{\thesection}{\arabic{section}}
\usepackage{mathtools, amssymb}
\usepackage{cancel}
\newtheorem{theorem}{THEOREM}
\newtheorem{proof}{PROOF}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\usetikzlibrary{arrows}
\usepackage{fancyhdr}
\usepackage{bigints}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{booktabs, caption}
\usepackage{blkarray, multirow, makecell}
\usepackage{graphicx}
\usepackage{adjustbox}
\usepackage{polynom}
\usepackage{flexisym}
\usepackage{wallpaper}
\usepackage{hyperref}
\newenvironment{tightcenter}{
\setlength\topsep{0pt}
\setlength\parskip{0pt}
\begin{center}}{\end{center}}
\begin{document}
\begin{center}
\begin{tabular}{lccc}
\toprule
\multirowcell{3.3}{SECTION A} & \makecell{Number of \\ questions} & \makecell{Number of questions \\ to be answered} & \makecell{Number of \\ marks}\\
\cmidrule{2-4}
& \multicolumn{1}{c}{7} & \multicolumn{1}{c}{7} & \multicolumn{1}{c}{36} \\
\midrule
\multirowcell{3.4}{SECTION B} & \makecell{Number of \\ questions} & \makecell{Number of questions \\ to be answered} & \makecell{Number of \\ marks}\\
\cmidrule{2-4}
& 4 & 2 & 24\\
\midrule
& & & \text{Total }60\\
\bottomrule
\end{tabular}
\end{center}
\end{document}