0

I have a .csv-file, which I want to make into a table using the datatool-package. The .csv-file ist structured like

foo&bar&foobar

where several lines are grouped together and only the first in each group contains entries for foo and bar, the rest only for foobar. The entries may contain math.

I tried to check for the first line in a group with \ifdefempty from etoolbox-package and printing out \hline, if the entry for bar isn't empty, but this yields an error message.

MWP

\documentclass{article}
\usepackage{etoolbox}
\usepackage{tabu}
\usepackage{longtable}
\usepackage{datatool}

\DTLloaddb[keys={foo,bar,foobar}]{mydatabase}{testbase.csv}

\newcommand{\printcommand}[2][]{%
 \begin{longtabu}to\textwidth{@{}llX@{}}
 \firsthline
 Foo&Bar&Foobar\\
 \hline\endhead
 \DTLforeach*{#2}{\foo=foo,\bar=bar,\foobar=foobar}{%
%   \ifdefempty{\bar}{}{\hline}%
  \foo&\bar&\foobar\\
  }
 \end{longtabu}
}

\begin{document}
foobar

\printcommand{mydatabase}
\end{document}

The testbase.csv looks like this

myfoo,mybar,myfoobar
17,barbar,bar 1
,,bar 234
,,barbary
23,ba ba ba,\(a^2+b^2=c^2\)
,,\emph{barfoo}
,,foofoofoo

If I compile like this, I get no errors, but if I uncomment the commented line, I get misplaced \noalin and misplaced \omit errors.

If I replace longtabu by tabu (and remove the then undefined \endhead), I get even more errors.

How can I make this work the way I want?

Toscho
  • 4,713
  • Have you tried changing the order (hline after linebreak)? – TeXnician Jul 05 '17 at 15:05
  • There is no \linebreak in the code. Only \\ and this ends previous lines of the table. – Toscho Jul 05 '17 at 15:07
  • Yeah, and in a table hline as to be after \. – TeXnician Jul 05 '17 at 15:08
  • So I asked, how to determine, if the previous line has ended over here ( https://tex.stackexchange.com/questions/378349/in-a-tabular-how-to-make-sure-previous-line-has-ended/ ) and got the command \crcr as a result, that behaves like \cr except if there was \cr before, in which case, it does nothing. But this doesn't work here, as expected: If added before the \hline in the commented line, it adds an empty line. So the \ifdefempty seems to add something, so that \hline breaks, because it's not directly after newline and \crcr is like \cr, because it's not directly after \cr. – Toscho Jul 05 '17 at 18:04
  • @Toscho: Is this the output you're after - image? A horizontal line \hline to separate every \bar case - code. Note the use of \DTLiffirstrow. – Werner Jul 05 '17 at 18:14
  • Yes, that's the output, I'm looking for. – Toscho Jul 05 '17 at 18:16
  • @Werner \DTLiffirstrow only checks the first input line, but I would have to check for the first outputline of each page. – Toscho Jul 05 '17 at 18:42
  • @Toscho: Perhaps it will help if you write up a complete example that shows the expected output for a (say) 2-page table. There seems to be cases where the suggestions work, and other cases where it doesn't. Please spoon-feed us that kind of information. – Werner Jul 05 '17 at 18:46
  • Really recommend not using tabu or longtabu unless you want buggy code which will break completely at some unspecified future date. – cfr Jul 06 '17 at 00:17
  • @cfr Do you have sound arguments for this statement of yours or is this just whining about tabu? – Toscho Jul 12 '17 at 17:53
  • @Toscho Yes. This is based on dialogue between egreg and tabu's author. The author has promised backwards-incompatible changes and refuses to fix bugs in the meantime, even when provided with all the information required to do so. (That is, I'm not talking about refusing to spend hours trying to track down a bug in code you're about to replace anyway.) If you think that's 'just whining', fine. But don't look for any sympathy when you have to rewrite every tabu in your book or dissertation after inadvertently breaking it by updating your TeX installation. – cfr Jul 12 '17 at 23:06
  • @Toscho https://tex.stackexchange.com/questions/121841/is-the-tabu-package-obsolete/121847#121847 Perhaps you think that is 'just whining about tabu', too? Gosh! How unfair! – cfr Jul 12 '17 at 23:10

1 Answers1

1

Your error is pretty simple. You try to place a \hline without having the previous line ended. That won't work (with or without loop). Try

\DTLforeach*{#2}{\foo=foo,\bar=bar,\foobar=foobar}{%
  \foo&\bar&\foobar\\
  \ifdefempty{\bar}{}{\hline}%
}

and you'll see that it compiles to

longtabu

Probably you'll say that this is not what you want. I know, but your check does not work for outputting a hline before. You would have to ensure that the previous line ends with something like \\.

TeXnician
  • 33,589
  • But with my code, I'm sure, that there is a \\ before that line: The head ends with such a \\, and every line in the loop ends with such a \\. – Toscho Jul 05 '17 at 17:28