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}