我正在尝试使用 datatool 和 从 csv 构建词汇表\longnewglossaryentry
。但我无法让它工作。
让我们从一些事情开始做编译:
\documentclass[10pt, a4paper]{article}
\usepackage[verbose]{datatool}
\usepackage{filecontents}
\usepackage{hyperref}
\usepackage[nopostdot]{glossaries}
\begin{filecontents*}[overwrite]{test.csv}
Name,Term,Description
"html","Hypertext Markup Language (HTML)","is a markup language"
"css","Cascading Style Sheets (CSS)","is a style sheet language"
\end{filecontents*}
\makeglossaries
\glssetexpandfield{name}
\glssetexpandfield{desc}
\DTLloaddb{data}{test.csv}
\DTLforeach*{data}
{%
\Name=Name,%
\Term=Term,%
\Desc=Description%
}
{%
\newglossaryentry{\Name}{
name={\Term},
description={\Desc}
}%
}
\begin{document}
\glsaddall
\printglossaries
\end{document}
呈现如下效果:
现在到了最有趣的部分。我有一些包含枚举的描述。如果我将以下行添加到 csv 中:
"enum","","Enumeration","is a numbered list like \begin{enumerate} \item first \item second \end{enumerate}"
并重新编译,出现错误:
[...]
! Incomplete \iffalse; all text was ignored after line 31.
<inserted text>
\fi
[...]
这是有道理的,因为我正在使用\newglossaryentry
不支持列表的[请参阅更新 2,这不是主要问题]。\longnewglossaryentry
做支持这一点,但是当我在脚本中更改为时,我收到来自数据工具的错误\newglossaryentry
:\longnewglossaryentry
! Package datatool Error: Can't break out of anything.
这就是我陷入困境的地方。
更新 1
我也看到过这种格式的使用\longnewglossaryentry
,其中描述作为第二个参数给出:
\longnewglossaryentry{\Name}{
name={\Term}
}{\Desc}%
在这种情况下,列表确实有效,但所有描述都将变得等于最后一组描述。
更新 2
奇怪的是,当我向 datatool () 添加选项“详细”时\usepackage[verbose]datatool
,错误包含更多信息,但也不同并且实际上等于以下错误\newglossaryentry
:
New row added to database `data'
Added Name -> enum to database `data'
Added Term -> Enumeration to database `data'
! Incomplete \iffalse; all text was ignored after line 19.
<inserted text>
\fi
答案1
数据库加载时它就已经中断了。您可以保护 csv 中的条目:
\documentclass[10pt, a4paper]{article}
\usepackage[verbose]{datatool}
\usepackage{filecontents}
\usepackage{hyperref}
\usepackage[nopostdot]{glossaries}
\begin{filecontents*}[overwrite]{test.csv}
Name,Term,Description
"html","Hypertext Markup Language (HTML)","is a markup language"
"css","Cascading Style Sheets (CSS)","is a style sheet language"
"enum","Enumeration","is a numbered list like \unexpanded{\begin{enumerate} \item first \item second \end{enumerate}}"
\end{filecontents*}
\makeglossaries
\glssetexpandfield{name}
\glssetexpandfield{desc}
\DTLloaddb{data}{test.csv}
\DTLforeach*{data}
{%
\Name=Name,%
\Term=Term,%
\Desc=Description%
}
{%
\newglossaryentry{\Name}{
name={\Term},
description={\Desc}
}%
}
\begin{document}
\glsaddall
\printglossaries
\end{document}
编辑
实际上,由于该verbose
选项,它在加载数据库时会失败。如果删除它,仍然存在\glssetexpandfield{desc}
\newglossaryentry
尝试完全展开描述内容的问题,这会再次中断。可以通过在那里添加 \unexpanded 来避免这种情况:
\documentclass[10pt, a4paper]{article}
\usepackage{datatool}
\usepackage{hyperref}
\usepackage[nopostdot]{glossaries}
\begin{filecontents*}[overwrite]{test.csv}
Name,Term,Description
"html","Hypertext Markup Language (HTML)","is a markup language"
"css","Cascading Style Sheets (CSS)","is a style sheet language"
"enum","Enumeration","is a numbered list like \begin{enumerate} \item first \item second \end{enumerate}"
\end{filecontents*}
\makeglossaries
\glssetexpandfield{name}
\glssetexpandfield{desc}
\DTLloaddb{data}{test.csv}
\DTLforeach*{data}
{%
\Name=Name,%
\Term=Term,%
\Desc=Description%
}
{%
\newglossaryentry{\Name}{
name={\Term},
description={\unexpanded\expandafter{\Desc}}
}%
}
\begin{document}
\glsaddall
\printglossaries
\end{document}