\ end {tabular} \\的新命令?绕过语法和逃避

时间:2017-12-13 20:14:17

标签: latex tex renewcommand

我有一个制表程序,可以为我创建表格。然而,一个问题是它编写得不是很好。

例如,我希望它能创建一个如下所示的表:

\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}\\。这会在某些环境中引发错误,例如threeparttablecenter

我已经在自己的机器上编辑了这个程序的源代码,并通过电子邮件发送了一个没有响应的维护者。我有一个即将到来的项目,我将需要与多台计算机上的多个同事共享此代码,我不能让每个人都找到更改包的代码的确切命令。这通常不会开始考虑具有可再现性的错误。

我意识到对此的好处是让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}

2 个答案:

答案 0 :(得分:2)

您无法通过重新定义\tabular来解决此错误,其中\begin{tabular}\end{tabular}只是一个扩展名。那是因为偶尔发生错误的\\

你也不应该试图通过重新定义\\来解决这个问题,因为这样的定义必须在TeX内部进行大量的研究。

我认为你最好的选择是

  • 找到某人在表格生成器中修复此错误并正式发布,
  • 为最新版本创建一个非官方补丁并将其分发给您的团队,或者
  • 尝试找到该错误的解决方法,例如在编译之前优化包含该表的源文件

您还可以使用一个小脚本对输出进行后处理,该脚本会删除表格后的任何尾随\\

答案 1 :(得分:0)

您可以使用LuaLaTeX和(a)设置一个功能,将文本中\end{tabular}\\的所有实例更改为\end{tabular}"即时#34; (b)将此函数分配给所谓的process_input_buffer回调函数,该函数在处理的早期阶段完成其工作,之后(La)TeX执行任何常规工作。

enter image description here

% !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}

这样您仍然可以在不更改代码的情况下处理来源。