我在加载dtltool
数据库时插入了虚假的空格。
下面的 MWE 生成所有以与 .tex 文件相同的前缀命名的图像文件的链接:
但是如果我注释掉该\DTLloaddb
行(如代码中的注释所示),则会产生:
请注意,缩进会随着链接文件数量的增加而增加。即使没有匹配项(如文件的情况A.tex
),仍然会缩进。
我怀疑这与我之前遇到的一个问题有关,当时我看到在 \foreach 中向数据库添加成员时出现虚假空白。我尝试\unskip
在每次调用时添加一个\AddMemberToDB
,但在这种情况下没有帮助。
附加问题:
这实际上与 TeX 无关,但是我该如何改变调用,
ls
以便控制台中不显示如下错误消息。ls:B-*.pdf:没有此文件或目录
我在聊天室中讨论过这个问题,但在实际使用中却出现问题。
代码:
\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}{%
\dtlforeachkey(\@dtl@key,\@dtl@col,\@dtl@type,\@dtl@head)\in{#1}\do
{%
\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
\newcommand{\AddLinkToFile}[3][black]{%
%#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}}%
% If comment out the following \DTLloaddb, the no additional indentation
\DTLloaddb[noheader,keys={FileName}]{ImageDB}{\jobname.ImageDB}%
%------------------------------------------------------------
\makebox[4.0cm][l]{%
\AddLinkToFile[red]{\x.tex}{\x}%
\hspace*{1.0ex}%
\DTLifdbexists{ImageDB}{%
\DTLifdbempty{ImageDB}{%
% no associated image files found.
}{%
\DTLforeach{ImageDB}{\ImageFile=FileName}{%
\hspace*{1.0ex}%
\AddLinkToFile[gray]{\ImageFile}{\ImageFile}%
}%
}%
}{}%
}%
% Add a new line if we are at the appropriate multiple
\stepcounter{NumRowElements}%
\IfEq{\arabic{NumRowElements}}{\NumberOfElementsBeforeLinebreak}{%
\setcounter{NumRowElements}{0}%
\par\noindent%
}{}%
}%
\end{document}
答案1
已在 2.30 版本(2018-04-16)中修复。
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
早期版本的补丁(不要将其与 v2.29 一起使用,因为它改变了一些内部结构,包括\@dtl@readline
,以修复另一个问题):
\makeatletter
\renewcommand*{\@dtl@readline}[2]{%
\read#1 to #2%
\ifdefempty{#2}%
{%
}%
{%
\dtl@trim#2%
}% <- added comment character
}
\makeatother
(你可以使用以下方式跟踪 bug 的进展我的错误追踪器。