我有一个制表程序为我创建表格。然而,有一个问题是它写得不太好。
例如,我希望它能制作一个如下所示的表:
\begin{tabular}{|l|lll|l|}
\cline{1-1} \cline{5-5}
id & x & y & y & sum \\ \cline{1-1} \cline{5-5}
a & 1 & 2 & 3 & 6 \\ \cline{1-1} \cline{5-5}
b & 1 & 2 & . & 3 \\ \hline
c & \multicolumn{1}{l|}{.} & \multicolumn{1}{l|}{.} & . & . \\ \hline
\end{tabular}
但是,有时它会将反斜杠添加到end{tabular}
命令的末尾,该命令读取end{tabular}\\
。这会在一些环境中引发错误,比如threeparttable
和center
。
我在我自己的机器上编辑了这个程序的源代码,并给一个反应迟钝的维护者发了电子邮件。我有一个即将到来的项目,在这个项目中,我需要在多台计算机上与多个同事共享这段代码,而且我不能让每个人都找到要更改包代码的确切命令。一般情况下,这甚至还没有开始考虑具有可复制性的错误。
我意识到,解决这个问题的好方法是让Latex将命令\end{tabular}\\
读入end{tabular}
。但是,当我试图定义自己的命令时,语法无法工作。有人能帮我创建这个定义吗?我不明白为什么\newcommand{\end{tabular}\\}{\end{tabular}}
不能工作。
编辑:
我加了一辆MWE。以下代码将不使用ShareLatex进行编译。在end{tabular}\\
说"There is no line to end here
“之后,空行中会弹出一个错误。第二个代码块以end{tabular}
(没有反斜杠)结尾,编译很好。
\documentclass[12pt]{article}
\usepackage[a4paper, margin = .5in]{geometry}
\usepackage{pdflscape}
\usepackage{threeparttable}
\begin{document}
\begin{table}
\caption{My test table}
\begin{center}
\begin{threeparttable}
\small
\begin{tabular}{lll}
a & b & c \\
2 & 3 & 4 \\
this & that & here
\end{tabular}\\
\begin{tablenotes}
\item
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{table}
\end{document}
这是第二个代码块,运行的代码块。
\documentclass[12pt]{article}
\usepackage[a4paper, margin = .5in]{geometry}
\usepackage{pdflscape}
\usepackage{threeparttable}
\begin{document}
\begin{table}
\caption{My test table}
\begin{center}
\begin{threeparttable}
\small
\begin{tabular}{lll}
a & b & c \\
2 & 3 & 4 \\
this & that & here
\end{tabular}
\begin{tablenotes}
\item
\end{tablenotes}
\end{threeparttable}
\end{center}
\end{table}
\end{document}
发布于 2017-12-16 05:56:09
您不能通过重新定义\tabular
来消除此错误,\begin{tabular}
和\end{tabular}
只是其中的一个扩展。这是因为错误的\\
只是偶尔发生。
您也不应该试图通过重新定义\\
来解决这个问题,因为这样的定义必须深入TeX内部。
我认为你最好的选择是
您还可以使用一个小脚本对输出进行后处理,该脚本可以在表格之后删除任何尾随的\\
。
发布于 2018-01-02 22:06:23
您可以使用LuaLaTeX和(a)设置一个函数,将文本中的所有\end{tabular}\\
实例更改为“动态”;(b)将该函数分配给所谓的process_input_buffer
回调,该回调在(La)TeX完成其任何常规工作之前,在处理的早期阶段完成其工作。
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{threeparttable}
\usepackage{luacode}
\begin{luacode}
function removetabularbs ( line )
return string.gsub ( line, "\\end{tabular}\\\\", "\\end{tabular}" )
end
luatexbase.add_to_callback ( "process_input_buffer", removetabularbs, "removetabularbs" )
\end{luacode}
\begin{document}
\begin{threeparttable}
\begin{tabular}{ l l l }
a & b & c \\
2 & 3 & 4 \\
this & that & here
\end{tabular}\\
\begin{tablenotes}
\item Something
\end{tablenotes}
\end{threeparttable}
\end{document}
这样,您仍然可以处理源代码,而不必更改代码。
https://stackoverflow.com/questions/47801416
复制相似问题