2

I have a table-making program that creates tables for me. One problem, however, is that it isn't written very well.

For example, I would like it to make a table that looks like this:

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

Sometimes, however, it adds to backslashes to the end of the end{tabular} command, which reads end{tabular}\\. This throws an error in some environments, like threeparttable and center.

I have edited the source code for this program on my own machine, and emailed an unresponsive maintainer. I have an upcoming project where I will need to share this code with multiple coworkers across multiple computers, and i can't have everyone find the exact command to change in the code of the package. This isn't even beginning to consider errors with reproducability in general.

I realized that good fix for this would be to just have Latex read the command \end{tabular}\\ as end{tabular}. But I can't get the syntax to work when I try to define my own command. Can someone help me create this definition? I do not understand why \newcommand{\end{tabular}\\}{\end{tabular}} doesn't work.

EDIT:

I have added a MWE. The following code will not compile using ShareLatex. An error pops up in the blank line after end{tabular}\\ saying "There is no line to end here". The second chunk of code, which ends with end{tabular} (no backslashes) compiles fine.

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

This is the second block of code, the one that runs.

\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}
genauguy
  • 195
  • 1
  • 11

2 Answers2

2

You cannot get rid of this error by redefining \tabular, of which the \begin{tabular} and \end{tabular} is just an extension. That's because the errative \\ just occurs occasionally.

You also shouldn't try to solve this by redefining \\, because such a definition would have to poke a lot into TeX internals.

I think your best options are

  • find someone to fix this bug in the table generator and get it officially released,
  • create an inofficial patch for the latest release and distribute that to your team, or
  • try to find a workaround for that bug, such as optimizing the source file that contains the table prior to compilation

You could also post-process the output with a small script that removes any trailing \\ after tabular.

yacc
  • 2,915
  • 4
  • 19
  • 33
  • Thank you. I will look into my program's generic read/write functions to do a string replace immediately after saving the file. – genauguy Dec 19 '17 at 19:15
0

You could use LuaLaTeX and (a) set up a function that changes all instances of \end{tabular}\\ in the text to \end{tabular} "on the fly" and (b) assign this function to the so-called process_input_buffer callback, which does its work at a very early stage of processing, before (La)TeX does any of its usual work.

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}

This way you're still processing the source without having to change the code.

Werner
  • 14,324
  • 7
  • 55
  • 77