genauguy genauguy - 6 months ago 57
LaTeX Question

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

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}

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download