4

I am trying to create a vocabulary list from a csv file containing the vocabulary using tabularx and datatool.

This is what I have tried:

\documentclass{article}
\usepackage{datatool,tabularx}
\usepackage{hyperref}

\begin{filecontents*}{test.csv}
english,german
voasc1,translation1
vofc2,trans2
\end{filecontents*}

\begin{document}

\DTLloaddb{vocabDB}{test.csv}%Load vocab file

\noindent
\begin{tabularx}{\columnwidth}{X|X}
  \textbf{English} & \textbf{German} \\
  \hline
  \DTLforeach*{vocabDB}{\English=english,\German=german}{
    \English & \German \\
  }
\end{tabularx}

\end{document}

But then I get a blank line in the first column. At least there is no TeX error while compiling.

enter image description here

I tried to use this tabularx syntax without datatool and then it is successful, i.e. each row has just one line and both columns are in the same height.

EDIT

It seems to actually be hyperref causing this strange behaviour. (I have added the line to code above). Why does it do that?

Edit 2 Solved the issue by placing the table into a NoHyper environment.

  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. – Thomas F. Sturm Jan 14 '15 at 17:34
  • I don't receive the output you show. Your MWE has an output that resembles this. – Werner Jan 14 '15 at 18:46
  • It seems to be hyperref causing this issue. I have added the respective line to the code in my question. Any ideas why hyperref causes this behaviour? – PDev Jan 14 '15 at 19:04
  • Ok so now that I have found the problem I just placed the table into a NoHyper environment so that I do not get this behaviour. Thanks for your help. – PDev Jan 14 '15 at 19:09
4

datatool keeps track of the rows that it parses when using \DTLforeach[*][<condition>]{<db name>}{<assign list>}{<text>} (from the user guide):

The macro \DTLforeach may be nested up to three times. Each level uses the corresponding counters: DTLrowi, DTLrowii and DTLrowiii which keep track of the current row.

Note that these counters are only incremented when <condition> is satisfied, therefore they will not have the correct value in <condition>. These counters are incremented using \refstepcounter before the start of <text>, so they may be referenced using \label, however remember that \label references the last counter to be incremented using \refstepcounter in the current scope. The \label should therefore be the first command in <text> to ensure that it references the current row counter.

The important bit here is that the row counter can be referenced using a \label. As such, it's incremented using \refstepcounter - something that hyperref redefines in order to manage its hyperlinking anchors and such.

In order to avoid this, remove hyperref functionality for the table (via a NoHyper environment), or don't use a referenceable stepping mechanism in the counters. That is, \let\refstepcounter\stepcounter:

\documentclass{article}
\usepackage{datatool,tabularx}
\usepackage{hyperref}

\begin{filecontents*}{test.csv}
english,german
english1,german1
english2,german2
\end{filecontents*}

\begin{document}

\DTLloaddb{vocabDB}{test.csv}%Load vocab file

\noindent
{\let\refstepcounter\stepcounter% Or \begin{NoHyper}
\begin{tabularx}{\columnwidth}{X|X}
  \textbf{English} & \textbf{German} \\
  \hline
  \DTLforeach*{vocabDB}{\English=english,\German=german}{
    \English & \German \\
  }
\end{tabularx}%
}% \end{NoHyper}

\end{document}

Note that this behaviour is also specific to the X-column type of tabularx and other fixed-width column types (like p or m). For example, using an l-column doesn't show this problem.

  • This seems like a bug, but I'm not sure whether it should be fixed in datatool or hyperref. I doubt whether tabularx is at fault here, and @DavidCarlisle would agree. ;) – Werner Jan 14 '15 at 19:54

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.