当我编写真值表时,我喜欢在单元格中留出更多一些空间,因此我为arraystretch
和分配新值tabcolsep
,如下所示......
\documentclass[12pt]{article}
\begin{document}
\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm}
\begin{tabular}{| c | c | c | c | c |}
\hline
$Q$ & $R$ & $Q \lor R$ \\ \hline \hline
T & T & T \\
T & F & T \\
F & T & T \\
F & F & F \\
\hline
\end{tabular}
\end{document}
...一切都很好。
最终我为真值表创建了一个新的环境:
\newenvironment{truthtabular}[1]{%
\renewcommand{\arraystretch}{1.5}
\renewcommand{\tabcolsep}{0.2cm}
\begin{tabular}{#1}{%
}
}{\end{tabular}}
环境运行正常,除非我想要顶部的 hline,在这种情况下我会收到以下错误:
Misplaced \noalign. \hline
You can't use `\hrule' here except with leaders. \hline
Missing number, treated as zero. \hline
Illegal unit of measure (pt inserted). \hline
为了诊断问题,我从新环境的定义中删除了重新分配,因此:
\newenvironment{truthtabular}[1]{%
%\renewcommand{\arraystretch}{1.5}
%\renewcommand{\tabcolsep}{0.2cm}
\begin{tabular}{#1}{%
}
}{\end{tabular}}
当以 hline 开头时,编译仍然会失败,并出现相同的错误。失败示例如下:
\documentclass[12pt]{article}
\begin{document}
\newenvironment{truthtabular}[1]{%
\begin{tabular}{#1}{%
}
}{\end{tabular}}
\begin{truthtabular}{| c | c | c | c | c |}
\hline
$Q$ & $R$ & $Q \lor R$ \\ \hline \hline
T & T & T \\
T & F & T \\
F & T & T \\
F & F & F \\
\hline
\end{truthtabular}
\end{document}
为什么引导线会在定义的环境中引发问题?可以采取哪些措施来避免这些问题?
答案1
您的定义中有一对伪括号。
我稍微改进了你的表格,使用\\hhline
双线,并建议另一种布局:
\documentclass[12pt]{article}
\usepackage{array, hhline, booktabs}
\usepackage[x11names, table]{xcolor}
\newenvironment{truthtabular}[1]{%
\renewcommand{\arraystretch}{1.5}
\setlength{\tabcolsep}{0.2cm} \tabular{#1}
}{\endtabular}
\begin{document}
\begin{truthtabular}{| c | c | c | }
\hline
$Q$ & $R$ & $Q \lor R$ \\ %
\hhline{:=:=:=:}
T & T & T \\
T & F & T \\
F & T & T \\
F & F & F \\
\hline
\end{truthtabular}
{ % begin box
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\arrayrulecolor{LightSteelBlue3}
\begin{truthtabular}{cc!{\color{LightSteelBlue3}\vrule width1.5pt}c }
$Q$ & $R$ & $Q \lor R$ \\ %
\midrule
T & T & T \\
T & F & T \\
F & T & T \\
F & F & F
\end{truthtabular}
} % end box
\end{document}
答案2
必须是行中的第一个元素。在您的示例中,由于您使用空组结束了定义\hrule
,因此您将一个空组放在了环境的顶部。以下最小示例复制了您的问题:truthtable
\begin{truthtable}
\documentclass{article}
\begin{document}
\begin{tabular}{c}
{} \hline % <---- not good
table
\end{tabular}
\end{document}
删除此空组(以及虚假空间)一切和平将恢复:
\documentclass{article}
\usepackage{booktabs,eqparbox}
\newenvironment{truthtabular}[1]
{\renewcommand{\arraystretch}{1.5}% https://tex.stackexchange.com/a/31704/5764
%\setlength{\tabcolsep}{0.2cm}% ...if you still need it
\begin{tabular}{#1}}
{\end{tabular}}
\begin{document}
\begin{truthtabular}{ *{3}{c} }
\toprule
\eqmakebox[ttt]{$Q$} & \eqmakebox[ttt]{$R$} & \eqmakebox[ttt]{$Q \lor R$} \\
\midrule
T & T & T \\
T & F & T \\
F & T & T \\
F & F & F \\
\bottomrule
\end{truthtabular}
\end{document}