在 \foreach 中向数据库添加成员时出现虚假空白

在 \foreach 中向数据库添加成员时出现虚假空白

MWE 添加与迭代的最大值成比例的空白\foreach

在此处输入图片描述

为什么会这样?我该如何消除这些额外的空间?

笔记:

  • 这个问题和我之前遇到的问题类似输出不应显示的文本,但我不知道如何解决这个问题。
  • 我猜测这是一种与扩展相关的问题,因为\dtlexpandnewvalue这是从数据库获取正确输出所必需的。

代码:

\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}

答案1

\@dtl@updatekeys通过\@defl@dogetprops\edef并且不会插入%到行尾,从而导致不必要的跳过:

\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
    %...
}

由于\@dtl@updatekeys相当大,因此在\unskip添加成员(的一部分\AddMemberToDB或在内\foreach)后发出即可解决问题。

在此处输入图片描述

\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}

相关内容