Bug Tracker RSS feed

I’m currently working on a major new version of the datatool package. This may take a while. Please be patient. (Experimental version available for testing.)

ID 260🔗
Date: 2023-06-22 17:26:07
Last update: 2023-06-26 09:24:37
Status Open
Sign in if you want to bump this report.
Category datatool
Version 2.32
Summary \DTLifstringeq (and others) leads to a "Misplaced \noalign" error
Cross Ref 130

Sign in to subscribe to notifications about this report.

Description

Hi,
I'm trying to use \DTLifstringeq inside a \DTLforeach loop to conditionally apply a specific row color.
However, the use of \rowcolor in either the true or false branch of \DTLifstringeq leads to an error of type "Misplaced \noalign".
If other tests are used such as \DTLifoddrow, \DTLiffirstrow or \DTLiflastrow everything works fine.
I think this issue is related to the Bug Report 130.

Following what was done to fix the bug 130, I looked at the definition of \DTLifstringeq in datatool-base.sty:

\newcommand*{\@DTLifstringeq}[4]{%
  \protected@edef\@dtl@tmpcmp{%
    \noexpand\dtlcompare{\noexpand\@dtl@tmpcount}{#1}{#2}}%
  \@dtl@tmpcmp
  \ifnum\@dtl@tmpcount=0\relax
    #3%
  \else
    #4%
  \fi
}
I've changed this to:
\newcommand*{\@DTLifstringeq}[4]{%
  \protected@edef\@dtl@tmpcmp{%
    \noexpand\dtlcompare{\noexpand\@dtl@tmpcount}{#1}{#2}}%
  \@dtl@tmpcmp
  \ifnum\@dtl@tmpcount=0 % space intended
    #3%
  \else
    #4%
  \fi
}
But this did not fixed the problem.
Is there a way to have \DTLifstringeq to work as \DTLifoddrow and not throw an error when using \rowcolor?
Thanks.

MWE

Download (706B)

\RequirePackage{filecontents}
\begin{filecontents*}{data.csv}
name
first
dsdd
second
third
fourth
fifth
\end{filecontents*}

\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{datatool}

\DTLloaddb{sample}{data.csv}

\begin{document}

\begin{tabular}{c}
% \rowcolor{orange}%
  \textbf{name}
  \DTLforeach{sample}{\word=name}{%
    \\
    \DTLifstringeq{\word}{second}{\rowcolor{red}}{} % Does not work
%     \rowcolor{yellow}                               % Works
%     \DTLifoddrow{\rowcolor{green}}{}                % Works
%     \DTLiffirstrow{\rowcolor{blue}}{}               % Works
%     \DTLiflastrow{\rowcolor{orange}}{}              % Works
    \word
  }
\end{tabular}

\end{document}

Evaluation

Unfortunately loops and conditionals can be very problematic inside tabular-like environments. The same problem occurs using etoolbox's \ifdefstring instead of \DTLifstringeq:

\begin{tabular}{c}
% \rowcolor{orange}%
  \textbf{name}
  \DTLforeach{sample}{\word=name}{%
    \\
    \ifdefstring{\word}{second}{\rowcolor{red}}{}% Does not work either
%    \DTLifstringeq{\word}{second}{\rowcolor{red}}{} % Does not work
%     \rowcolor{yellow}                               % Works
%     \DTLifoddrow{\rowcolor{green}}{}                % Works
%     \DTLiffirstrow{\rowcolor{blue}}{}               % Works
%     \DTLiflastrow{\rowcolor{orange}}{}              % Works
    \word
  }
\end{tabular}
One possible solution uses LaTeX3 code:
\ExplSyntaxOn
\newcommand{\ifstrtest}[4]{\str_if_eq:eeTF {#1} {#2} {#3} {#4} }
\ExplSyntaxOff

\begin{tabular}{c}
  \textbf{name}
  \DTLforeach{sample}{\word=name}{%
    \\
    \ifstrtest{\word}{second}{\rowcolor{red}}{}
    \word
  }
\end{tabular}
An alternative approach would be to construct the content of the tabular environment as follows:
\newcommand{\tabcontent}{\begin{tabular}{c}\textbf{name}}

\DTLforeach{sample}{\word=name}{%
 \appto\tabcontent{\\}%
 \DTLifstringeq{\word}{second}{\appto\tabcontent{\rowcolor{red}}}{}%
 \eappto\tabcontent{\expandonce{\word}}%
}
\appto\tabcontent{\end{tabular}}

\tabcontent
This moves all the problematic code outside of the tabular environment. (This is essential what glossaries-extra's \printunsrtglossary does to avoid similar problems.)

I'm in the process of rewriting datatool to use new LaTeX3 commands, but it will take some time before it's finished.

Comments

0 comments.


Add Comment

Name (optional):

Are you human? Please confirm the bug report ID (which can be found at the top of this page) or login if you have an account.

Comment:


You can use the following markup:

Block:

[pre]Displayed verbatim[/pre]
[quote]block quote[/quote]

In line:

[tt]code[/tt]
[file]file/package/class name[/file]
[em]emphasized text[/em]
[b]bold text[/b]
[url]web address[/url] [sup]superscript[/sup]
[sub]subscript[/sub]

Ordered list:
[ol]
[li]first item[/li]
[li]second item[/li]
[/ol]

Unordered list:
[ul]
[li]first item[/li]
[li]second item[/li]
[/ul]

You can use the Preview button to review your message formatting before submitting.

Page permalink: https://www.dickimaw-books.com/bugtracker.php?key=260