Latest news 2020-07-03: SmashWords sale (ends 31st July 2020): 50% off crime/SF novel The Private Enemy and 100% off (free!) I’ve Heard the Mermaid Sing.

# Bug Tracker

ID: 81 🔗 Closed (Not a Bug) datatool 2.22 Ronan Keryell 2015-04-03 22:42:25 Spurious line in tabular with

## Report

Hello!
Thank you for the amazing datatool!

I have an issue with a spurious line appearing in a tabular environment.

I spent hours on datatool.sty with \tracingall and \tracingmacro=1 tracking some spurious active space or cr but did not find anything...

So I wrote this bug report with a simple example to exhibit the issue. Actually I found no sample in the documentation with a \hline at the end that would have shown this bug...

Thank you!

\documentclass{report}
\usepackage{datatool}
\begin{document}

\DTLnewdb{mydata}
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{John}%
\DTLnewdbentry{mydata}{Surname}{Smith, Jr}%
\DTLnewdbentry{mydata}{Score}{68}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Jane}%
\DTLnewdbentry{mydata}{Surname}{Brown}%
\DTLnewdbentry{mydata}{Score}{75}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Andy}%
\DTLnewdbentry{mydata}{Surname}{Brown}%
\DTLnewdbentry{mydata}{Score}{42}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Z\"oe}%
\DTLnewdbentry{mydata}{Score}{52}%

\DTLdisplaydb{mydata}

There is a spurious tabular line at the end:\\
\begin{tabular}{|l|l|r|}%
\hline%
\DTLforeach{mydata}{%
\firstname=FirstName,\surname=Surname,\score=Score}{%
\firstname & \surname & \score\\\hline}%
\end{tabular}

In a manual tabular, no spurious line at the end:\\
\begin{tabular}{|l|l|r|}
\hline
A & B & C\\\hline
C & D & E\\\hline
\end{tabular}

\end{document}



### MWE

\documentclass{report}
\usepackage{datatool}
\begin{document}

\DTLnewdb{mydata}
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{John}%
\DTLnewdbentry{mydata}{Surname}{Smith, Jr}%
\DTLnewdbentry{mydata}{Score}{68}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Jane}%
\DTLnewdbentry{mydata}{Surname}{Brown}%
\DTLnewdbentry{mydata}{Score}{75}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Andy}%
\DTLnewdbentry{mydata}{Surname}{Brown}%
\DTLnewdbentry{mydata}{Score}{42}%
\DTLnewrow{mydata}%
\DTLnewdbentry{mydata}{FirstName}{Z\"oe}%
\DTLnewdbentry{mydata}{Score}{52}%

\DTLdisplaydb{mydata}

There is a spurious tabular line at the end:\\
\begin{tabular}{|l|l|r|}%
\hline%
\DTLforeach{mydata}{%
\firstname=FirstName,\surname=Surname,\score=Score}{%
\firstname & \surname & \score\\\hline}%
\end{tabular}

In a manual tabular, no spurious line at the end:\\
\begin{tabular}{|l|l|r|}
\hline
A & B & C\\\hline
C & D & E\\\hline
\end{tabular}

\end{document}



## Evaluation

This is unfortunately a feature rather than a bug and is why all the examples in the manual place \\ at the start of the final argument of \DTLforeach. This is due to additional stuff that has to be performed at the end of each iteration (such as incrementing the row counter) and this causes LaTeX to think a new row has started in the tabular.

This can be illustrated with a slight modification to your manual tabular:

\begin{tabular}{|l|l|r|}
\hline
A & B & C\\\hline
C & D & E\\\hline\def\tmp{stuff}%
\end{tabular}

This also adds an unwanted row.

There's no way to fix this as the commands at the end of each iteration have to come after the supplied code has been performed. The only way around this is to move the \\\hline to the start of the body. For example:

\begin{tabular}{|l|l|r|}%
\hline%
\DTLforeach{mydata}{%
\firstname=FirstName,\surname=Surname,\score=Score}{%
\DTLiffirstrow{}{\\\hline}\firstname & \surname & \score}%
\\\hline
\end{tabular}

I will have to close this as not a bug, but I will emphasis this feature in the manual.

## Watch This Report

If you supply your name, it will be used in the email greeting, which provides a more personal message, otherwise you'll just get a generic greeting. If you have previously supplied your name when signing up for notifications, you don't need to resupply it unless you want to change it.

If you have previously subscribed to notifications for this report, you can unsubscribe by clicking on the "Stop Notification" button.

The "Confirm Bug ID" field helps to protect against spambots. Please enter the bug ID (which you can find at the top of this page).

Name: (Optional.)