# Spurious blank space when adding a member to a database within \foreach

The MWE adds blank space proportional to the maximum value of the \foreach iteration:

Why is this and how do I eliminate this additional space?

## Note:

• This problem seems similar to an earlier issue I had earlier in Text being output when none should be, but I don't know how to resolve this issue.
• I am guessing this is some sort of expansion related issue as \dtlexpandnewvalue is required to get the correct output from the database.

## Code:

\documentclass{article}

\usepackage{pgffor}
\usepackage{datatool}
\usepackage{showframe}

\newcommand*{\InitalizeDB}[1]{%
\DTLifdbexists{#1}%
{\DTLcleardb{#1}}% DB exists, so just clear it
{\DTLnewdb{#1}}%   DB does not exist, so create it
}%
% #1 = name of DB
% #2 = db key to be used
% #3 = member to be added to DB
\dtlexpandnewvalue%
\DTLnewrow{#1}%
\DTLnewdbentry{#1}{#2}{#3}%
}%

\begin{document}
\InitalizeDB{MyDB}
\noindent
abc%
\foreach \x in {2,...,7} {%
}%
def

\bigskip\noindent
Database:\par
\DTLdisplaydb{MyDB}
\end{document}

-
Related Question: Spurious blank space when loading a datatool database. –  Peter Grill Dec 10 '12 at 0:43
I've now fixed the bug (in v2.11) causing this problem. –  Nicola Talbot Dec 10 '12 at 8:41

The macro \@dtl@updatekeys defines \@defl@dogetprops via an \edef and doesn't insert % at the end of the line, causing the unwanted skip:

\newcommand*{\@dtl@updatekeys}[3]{%
{%
\expandafter\dtlcolumnnum\expandafter
=\dtlcolumnindex{#1}{#2}\relax
\edef\@dtl@dogetprops{\noexpand\@dtl@getprops
{\noexpand\@dtl@key}{\noexpand\@dtl@type}%
{\noexpand\@dtl@after}{\the\csname dtlkeys@#1\endcsname}%
{\number\dtlcolumnnum}}% <--------- inserted '%'
\@dtl@dogetprops
%...
}


Since \@dtl@updatekeys is quite large, issuing \unskip after you've added a member (either part of \AddMemberToDB or within the \foreach) solves the problem.

\documentclass{article}

\usepackage{pgffor}% http://ctan.org/pkg/pgf
\usepackage{datatool}% http://ctan.org/pkg/datatool
\usepackage{showframe}% http://ctan.org/pkg/showframe

\newcommand*{\InitalizeDB}[1]{%
\DTLifdbexists{#1}%
{\DTLcleardb{#1}}% DB exists, so just clear it
{\DTLnewdb{#1}}%   DB does not exist, so create it
}%
% #1 = name of DB
% #2 = db key to be used
% #3 = member to be added to DB
\dtlexpandnewvalue%
\DTLnewrow{#1}%
\DTLnewdbentry{#1}{#2}{#3}%
}%

\begin{document}
\InitalizeDB{MyDB}
\noindent
abc%
\foreach \x in {2,...,7} {%