\end{tabular}\\ 的新命令?绕过语法和转义

newcommand for \end{tabular}\\? Getting around syntax and escape

我有一个 table 制作程序,可以为我制作 table。然而,一个问题是它写得不是很好。

例如,我希望它制作一个如下所示的 table:

\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}

您无法通过重新定义\tabular 来消除此错误,其中\begin{tabular}\end{tabular} 只是扩展。那是因为错误 \ 只是偶尔出现。

你也不应该尝试通过重新定义 \ 来解决这个问题,因为这样的定义必须深入 TeX 内部。

我认为你最好的选择是

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

您还可以 post 使用小脚本处理输出,该脚本删除表格后的任何尾随 \

您可以使用 LuaLaTeX 并且 (a) 设置一个函数,将文本中 \end{tabular}\ 的所有实例更改为 \end{tabular} "on the fly" 并且 (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}

这样您仍然可以处理源代码而无需更改代码。