I am getting spurious blank spaces inserted upon loading a dtltool db.

The MWE below produces links to all the image files named with a prefix the same as the .tex file:

But if I comment out the \DTLloaddb line (as indicated by the comments in the code), this produces:

Note that the indentation increases as the number of files being linked to increases. Even with zero matches (as is the case for the A.tex file), there is still an indentation.

I suspect that this is related to an earlier problem of mine where I was seeing a Spurious blank space when adding a member to a database within \foreach. I tried to use added an \unskip whenever I invoke \AddMemberToDB, but that does not help in this case.

## Bonus Question:

• This is not really TeX related, but how do I change the invocation of ls such that the error messages such as the following are not displayed in the console.

ls: B-*.pdf: No such file or directory

I had this working with a discussion in the chat room, but started to break in my real use case.

## Code:

\documentclass{article}
\usepackage{datatool}
\usepackage{xparse}
\usepackage{xstring}
\usepackage{tikz}
\usepackage{showframe}
\usepackage{hyperref}

%% -------------------------------------- Create the necessary files
%% Four .tex files:
\begin{filecontents*}{A.tex}
\end{filecontents*}

\begin{filecontents*}{B.tex}
\end{filecontents*}

\begin{filecontents*}{C.tex}
\end{filecontents*}

\begin{filecontents*}{D.tex}
\end{filecontents*}

% No .png file associated with A

% One image file associated with B
\begin{filecontents*}{B-1.png}
\end{filecontents*}

% Two image files associated with C
\begin{filecontents*}{C-1.png}
\end{filecontents*}
\begin{filecontents*}{C-2.jpg}
\end{filecontents*}

% Three image files associated with D
\begin{filecontents*}{D-1.png}
\end{filecontents*}
\begin{filecontents*}{D-2.png}
\end{filecontents*}
\begin{filecontents*}{D-3.jpg}
\end{filecontents*}
%% --------------------------------------

% https://tex.stackexchange.com/questions/51854/dtldeletedb-from-datatool-package-has-no-effect-within-foreach
%
%  Usage: Use \DTLgdeletedb instead of \DTLdeletedb
\makeatletter
\newcommand*{\DTLgdeletedb}[1]{%
\DTLifdbexists{#1}{%
{%
\global\expandafter\let\csname dtl@ci@#1@\@dtl@key\endcsname\undefined
}%
\global\expandafter\let\csname dtldb@#1\endcsname\undefined
\global\expandafter\let\csname dtlkeys@#1\endcsname\undefined
\global\expandafter\let\csname dtlrows@#1\endcsname\undefined
\global\expandafter\let\csname dtlcols@#1\endcsname\undefined
}{%
\PackageError{***** Error: Can't delete database #1':
database doesn't exist}{}{}%
}%
}
\makeatother

%#1 = text color
%#2 = file to open upon clicking
%#3 = text to display
%
\href{run:#2}{\protect\textcolor{#1}{#3}}%
}%

\newcommand{\ListAllAssociatedImageFilesOfTypeInCSV}[3][\jobname.ImageDB]{%
% #1 = temp file name
% #2 = dir
% #3 = file extension
\immediate\write18{%
ls -1 #2#3 %
| grep -v "No such file or directory" %
| sort -f%
%       >&2 % List ALL files
> ./#1% Store in given file name
}%
}%

\newcounter{NumRowElements}
\newcommand*{\NumberOfElementsBeforeLinebreak}{2}
\newcommand*{\ListOfImageFileTypes}{jpg,png,pdf}

\begin{document}
\setcounter{NumRowElements}{0}%
\par\noindent%
\foreach \x in {A,...,D}{%
\DTLifdbexists{ImageDB}{\DTLgdeletedb{ImageDB}}{}%
\ListAllAssociatedImageFilesOfTypeInCSV[\jobname.ImageDB]
{\x}{-*.{\ListOfImageFileTypes}}%
%------------------------------------------------------------
\makebox[4.0cm][l]{%
\hspace*{1.0ex}%
\DTLifdbexists{ImageDB}{%
\DTLifdbempty{ImageDB}{%
% no associated image files found.
}{%
\DTLforeach{ImageDB}{\ImageFile=FileName}{%
\hspace*{1.0ex}%
}%
}%
}{}%
}%
% Add a new line if we are at the appropriate multiple
\stepcounter{NumRowElements}%
\IfEq{\arabic{NumRowElements}}{\NumberOfElementsBeforeLinebreak}{%
\setcounter{NumRowElements}{0}%
\par\noindent%
}{}%
}%
\end{document}

• I forgot to comment out an EOL character in \@dtl@readline. I'll get it fixed in the next version of datatool. – Nicola Talbot Dec 9 '12 at 14:17
• This problem has been fixed in 2013/01/15 v2.13 version of datatool. – Peter Grill Jan 20 '13 at 5:43

Fixed in version 2.30 (2018-04-16).

Patch for v2.29:

\makeatletter
\edef\@dtl@construct@lopoff#1#2{%
\noexpand\long
\noexpand\def\noexpand\@dtl@lopoff#1##1##2\noexpand\to##3##4{%
\noexpand\ifx#2##1\noexpand\relax
\noexpand\ifstrempty{##1}%
{\noexpand\@dtl@qlopoff#1{}##2\noexpand\to##3##4\relax}%
{%
\noexpand\dtl@ifsingle{##1}%
{\noexpand\@dtl@qlopoff#1##1##2\noexpand\to##3##4\relax}%
{\noexpand\@dtl@qlopoff#1{##1}##2\noexpand\to##3##4\relax}%
}%
\noexpand\else
\noexpand\ifstrempty{##1}%
{\noexpand\@dtl@lop@ff#1{}##2\noexpand\to##3##4\relax}%
{%
\noexpand\dtl@ifsingle{##1}%
{\noexpand\@dtl@lop@ff#1##1##2\noexpand\to##3##4\relax}% <---
{\noexpand\@dtl@lop@ff#1{##1}##2\noexpand\to##3##4\relax}% <---
}%
\noexpand\fi
}%
}%
\DTLsetseparator{,}%
\makeatother


Patch for earlier versions (don't use this with v2.29 as that changed some of the internals, including \@dtl@readline, in order to fix another problem):

\makeatletter
\ifdefempty{#2}%
{%
}%
{%
\dtl@trim#2%

• So, this is different than the problem discovered in Spurious blank space when adding a member to a database within \foreach. It seems that \let\Old@dtl@updatekeys\@dtl@updatekeys \renewcommand*{\@dtl@updatekeys}[3]{\Old@dtl@updatekeys{#1}{#2}{#3}\unskip} also works so thought that perhaps that was related. – Peter Grill Dec 10 '12 at 0:42
• @PeterGrill It's a different problem but the same basic cause (a missing percent character at the end of a line). The problem with \@dtl@updatekeys was fixed in v2.11, so Werner's patch in your link is no longer required. (I tried neatening up the code in datatool.sty to make it easier to read, but I think I must've missed commenting some EOL characters. Hopefully that's the last one!) – Nicola Talbot Dec 10 '12 at 8:40
• Nicola: It seems as if this bug is back. I can reproduce this exact issue with the above MWE (after commenting out the \newcommand*{\DTLgdeletedb}). The \DTLloaddb is inserting the spurious space (as commeting out that line does not result in any space). I am working on a simpler test case for a new question but until I make one I thought I'd let you know in case this is a known issue. Using listfiles I have the version I am using is datatool.sty 2017/11/12 v2.29 (NLCT), datatool-base.sty 2017/11/12 v2.29 (NLCT) and datatool-fp.sty 2017/11/12 v2.29 (NLCT)`. – Peter Grill Apr 16 '18 at 6:31