我对 LaTeX 还很陌生。我制作这个表格时,大部分都是照搬网上的表格,没有任何理解。为什么列中的内容不居中?不要介意我可能添加了一些不相关的包
代码如下:
\documentclass{article}
\usepackage{graphicx,fancyhdr,amsmath,amssymb,amsthm,subfig,url,hyperref}
\usepackage[margin=1in]{geometry}
\newtheorem{theorem}{Theorem}
\newcommand{\myname}{xxxxxxxx}
\newcommand{\myid}{xxxxxxxx}
\newcommand{\hwNo}{xxxxxxxxxx}
%%% END
\fancypagestyle{plain}{}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RO,LE]{\sffamily\bfseries\large LUMS}
\fancyhead[LO,RE]{\sffamily\bfseries\large CS-210 Discrete Mathematics}
\fancyfoot[LO,RE]{\sffamily\bfseries\large \myname: \myid @lums.edu.pk}
\fancyfoot[RO,LE]{\sffamily\bfseries\thepage}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\arraystretch}{2}
%--------------------- This is the title of the document. DO NOT CHANGE IT ------------------------
\title{CS-210 \hwNo}
\author{\myname \qquad Student ID: \myid}
%--------------------------------- AFTER Entering the Student and Homework Information, write your answers below ----------------------------------
\begin{document}
\maketitle
\section{Problem 1}
\begin{table}[h!]
\centering
\caption{A}
\begin{tabular}{|l|l|l|l|l|l|l|l|l|l|l|l|l|}
\hline
A & B & C & $\overline{A}$ & $\overline{B}$ & $\overline{C}$ & A$\cap$B & $\overline{A} \cap$C & A$ \cap \overline{B}$ & $\overline{A} \cap \overline{C}$ & A$\cap$B $ \cup $ $\overline{A} \cap$C & $ (\ ( \ A \cap B )\ \cup (\ \overline{A} \cap C )\ )\ \complement $ & $ (\ A \cap \overline{B} )\ \cup (\ \overline{A} \cap \overline{C} )\ $ \\
\hline\hline
1& 1& 1 & 0 & 0 & 0 &1 &0 &0 &0 &1 &0 & 0 \\
1& 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0\\
1& 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1& 1\\
1& 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1& 1\\
0& 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0& 1 & 0& 0\\
0& 1& 0& 1 & 0 & 1 & 0 & 0 & 0 & 1& 0 & 1& 1\\
0& 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 & 0& 1 & 0 & 0\\
0& 0 & 0& 1 & 1 &1 & 0 & 0 & 0 & 1& 0 & 1 & 1\\
\hline
\end{tabular}
\end{table}
\end{document}
答案1
适合文本宽度的表格变体:
\documentclass{article}
\usepackage{amssymb}
\usepackage[margin=1in]{geometry}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{caption}
\begin{document}
\begin{table}[h!]
\centering
\caption{A}
\begin{tabular}{*{13}{c}}
\toprule
$A$ & $B$ & $C$ & $\overline{A}$ & $\overline{B}$ & $\overline{C}$ & $A\cap B$
& $\overline{A}\cap C$ & $A\cap\overline{B}$
& $\overline{A}\cap\overline{C}$
& \makecell{$A\cap B$\\${}\cup\overline{A}\cap C$}
& \makecell
{$\big((A\cap B)$\\${}\cup(\overline{A}\cap C)\big)\complement$}
& \makecell
{$(A\cap\overline{B})$\\${}\cup(\overline{A}\cap\overline{C})$} \\
\midrule
1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 \\
1 & 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 \\
1 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1 \\
1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 1 \\
0 & 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 \\
0 & 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1 \\
\bottomrule
\end{tabular}
\end{table}
\end{document}
答案2
我建议您(a)去掉所有垂直线(每条线都会占用一点空间,而 14 条线会占用相当多的空间!),(b)稍微简化单元格标题(大多数情况下,尝试始终使用数学模式),(c)使用c
而不是l
作为所有 13 列的列类型,以及(d)使用环境tabular*
而不是环境tabular
。
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{fancyhdr}
\fancypagestyle{plain}{}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RO,LE]{\sffamily\bfseries\large LUMS}
\fancyhead[LO,RE]{\sffamily\bfseries\large CS-210 Discrete Mathematics}
\fancyfoot[LO,RE]{\sffamily\bfseries\large \myname: \myid @lums.edu.pk}
\fancyfoot[RO,LE]{\sffamily\bfseries\thepage}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\usepackage{booktabs} % <-- new
\usepackage{graphicx,amsmath,amssymb,amsthm,subfig}
\newtheorem{theorem}{Theorem}
\newcommand{\myname}{xxxxxxxx}
\newcommand{\myid}{xxxxxxxx}
\newcommand{\hwNo}{xxxxxxxxxx}
\usepackage[hyphens,spaces]{url}
\usepackage[colorlinks,allcolors=blue]{hyperref}
%%% END
%---- This is the title of the document.
%---- DO NOT CHANGE IT --------------
\title{CS-210 \hwNo}
\author{\myname \qquad Student ID: \myid}
%----- AFTER Entering the Student and Homework
%----- Information, write your answers below
\begin{document}
\maketitle
\section{Problem 1}
\begin{table}[h!]
\setlength\tabcolsep{0pt} % let LaTeX calculate intercol. whitespace
\renewcommand{\arraystretch}{1.5} % "2" is excessive
\caption{A}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}*{13}{c}}
\toprule
$A$
& $B$
& $C$
& $\overline{A}$
& $\overline{B}$
& $\overline{C}$
& $A\cap B$
& $\overline{A} \cap C$
& $A \cap \overline{B}$
& $\overline{A} \cap \overline{C}$
& $A\cap B \cup \overline{A} \cap C$
& $ \overline{(A\cap B) \cup(\overline{A}\cap C)}$
& $ (A\cap\overline{B}) \cup (\overline{A}\cap\overline{C} )$ \\
\midrule % **not** \hline\hline
1& 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0\\
1& 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0\\
1& 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 1\\
1& 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 1\\
0& 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0\\
0& 1 & 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1\\
0& 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1 & 0 & 0\\
0& 0 & 0 & 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 1 & 1\\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
答案3
该表是数学表,因此应该是array
。您可以节省大量空间,删除括号后添加的空格并减少列之间的填充。此外,可以(并且应该)删除垂直规则。
\documentclass{article}
\usepackage{graphicx,fancyhdr,amsmath,amssymb,amsthm,subfig,url,hyperref}
\usepackage[margin=1in,headheight=14pt]{geometry}
\usepackage{booktabs}
\newtheorem{theorem}{Theorem}
\newcommand{\myname}{xxxxxxxx}
\newcommand{\myid}{xxxxxxxx}
\newcommand{\hwNo}{xxxxxxxxxx}
%%% END
\fancypagestyle{plain}{}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RO,LE]{\sffamily\bfseries\large LUMS}
\fancyhead[LO,RE]{\sffamily\bfseries\large CS-210 Discrete Mathematics}
\fancyfoot[LO,RE]{\sffamily\bfseries\large \myname: \myid @lums.edu.pk}
\fancyfoot[RO,LE]{\sffamily\bfseries\thepage}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\renewcommand{\arraystretch}{2}
%--------------------- This is the title of the document. DO NOT CHANGE IT ------------------------
\title{CS-210 \hwNo}
\author{\myname \qquad Student ID: \myid}
%--------------------------------- AFTER Entering the Student and Homework Information, write your answers below ----------------------------------
\begin{document}
\maketitle
\section{Problem 1}
\begin{table}[!htp]
\centering
\caption{A}
\[
\setlength{\arraycolsep}{3.3pt}
\begin{array}{@{}*{13}{c}@{}}
\toprule
A & B & C & \overline{A} & \overline{B} &
\overline{C} & A\cap B & \overline{A} \cap C &
A \cap \overline{B} & \overline{A} \cap \overline{C} &
A\cap B \cup \overline{A} \cap C &
((A \cap B)\cup(\overline{A}\cap C))^\complement &
(A \cap \overline{B} )\cup (\overline{A} \cap \overline{C} ) \\
\midrule
1& 1& 1 & 0 & 0 & 0 &1 &0 &0 &0 &1 &0 & 0 \\
1& 1 & 0 & 0 & 0 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0\\
1& 0 & 1 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 1& 1\\
1& 0 & 0 & 0 & 1 & 1 & 0 & 0 & 1 & 0 & 0 & 1& 1\\
0& 1 & 1 & 1 & 0 & 0 & 0 & 1 & 0 & 0& 1 & 0& 0\\
0& 1& 0& 1 & 0 & 1 & 0 & 0 & 0 & 1& 0 & 1& 1\\
0& 0 & 1 & 1 & 1 & 0 & 0 & 1 & 0 & 0& 1 & 0 & 0\\
0& 0 & 0& 1 & 1 &1 & 0 & 0 & 0 & 1& 0 & 1 & 1\\
\bottomrule
\end{array}
\]
\end{table}
\end{document}
注意的设置headheight
和使用,booktabs
以提高表格的质量。