关于这个项目我现在正尝试通过创建一个“对话伙伴数据库”为每个对话伙伴定义一个自己的 tcolorbox 环境,datatool
包裹然后使用\DTLforeach
。
我希望第一个框是蓝色的,标题是“John Doe”,但不知何故,两个框都是红色的,标题都是“Jane Stag”。我需要做哪些更改才能使其正常工作?
或者是否有更合适的方法来创建这种数据库(不使用外部文件)?
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\usepackage{datatool}
\begin{document}
\DTLnewdb{cp}% "cp" for "conversational partners".
\DTLnewrow{cp}
\DTLnewdbentry{cp}{name}{John Doe}
\DTLnewdbentry{cp}{shortcut}{JD}
\DTLnewdbentry{cp}{pfc}{blue}% "pfc" for "personal frame color"
\DTLnewrow{cp}
\DTLnewdbentry{cp}{name}{Jane Stag}
\DTLnewdbentry{cp}{shortcut}{JS}
\DTLnewdbentry{cp}{pfc}{red}
\DTLforeach{cp}{\NAME=name, \SHORTCUT=shortcut, \PFC=pfc}{%
\newtcolorbox{\SHORTCUT}{%
colframe={\PFC},
title={\NAME}
}
}
\begin{JD}%should be blue and have the title "John Doe", but is also red and has the title "Jane Stag".
\lipsum[1]
\end{JD}
\begin{JS}
\lipsum[1]
\end{JS}
\end{document}
答案1
问题发生的原因是占位符命令未在循环内展开,这意味着当使用颜色框时,占位符命令与最后一次迭代时一样。以下是解决方法:
\documentclass{article}
\usepackage{lipsum}
\usepackage{tcolorbox}
\usepackage{datatool}
\begin{document}
\DTLnewdb{cp}% "cp" for "conversational partners".
\DTLnewrow{cp}
\DTLnewdbentry{cp}{name}{John Doe}
\DTLnewdbentry{cp}{shortcut}{JD}
\DTLnewdbentry{cp}{pfc}{blue}% "pfc" for "personal frame color"
\DTLnewrow{cp}
\DTLnewdbentry{cp}{name}{Jane Stag}
\DTLnewdbentry{cp}{shortcut}{JS}
\DTLnewdbentry{cp}{pfc}{red}
\DTLforeach{cp}{\NAME=name, \SHORTCUT=shortcut, \PFC=pfc}{%
\edef\donewcolbox{\noexpand\newtcolorbox{\SHORTCUT}{%
colframe={\PFC},
title={\NAME}}%
}%
\donewcolbox
}
\begin{JD}%should be blue and have the title "John Doe", but is also red and has the title "Jane Stag".
\lipsum[1]
\end{JD}
\begin{JS}
\lipsum[1]
\end{JS}
\end{document}
得出的结果为: