Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

enter image description here

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
}%
\newcommand*{\AddMemberToDB}[3]{%
    % #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} {%
    \AddMemberToDB{MyDB}{MyDBKey}{\x}%
}%
def

\bigskip\noindent
Database:\par
\DTLdisplaydb{MyDB}
\end{document}
share|improve this question
    
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
add comment

1 Answer

up vote 4 down vote accepted

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]{%
  \@sDTLifhaskey{#1}{#2}%
  {%
    \expandafter\dtlcolumnnum\expandafter
      =\dtlcolumnindex{#1}{#2}\relax
    \edef\@dtl@dogetprops{\noexpand\@dtl@getprops
      {\noexpand\@dtl@key}{\noexpand\@dtl@type}%
      {\noexpand\@dtl@colhead}{\noexpand\@dtl@before}%
      {\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.

enter image description here

\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
}%
\newcommand*{\AddMemberToDB}[3]{%
    % #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} {%
    \AddMemberToDB{MyDB}{MyDBKey}{\x}\unskip%
}%
def

\bigskip\noindent
Database:\par
\DTLdisplaydb{MyDB}
\end{document}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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