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?

  • Have you tried changing the order (hline after linebreak)? – TeXnician Jul 5 '17 at 15:05
  • There is no \linebreak in the code. Only `\` and this ends previous lines of the table. – Toscho Jul 5 '17 at 15:07
  • Yeah, and in a table hline as to be after \\. – TeXnician Jul 5 '17 at 15:08
  • So I asked, how to determine, if the previous line has ended over here ( tex.stackexchange.com/questions/378349/… ) 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 5 '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 5 '17 at 18:14
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 \\.

| improve this answer | |
  • 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 5 '17 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.