3

Concerning this project, I'm now trying to define an own tcolorbox environment for every conversational partner by creating a "conversational partner database" with the datatool package and then using \DTLforeach.

I wanted the first box to be blue and have the title "John Doe", but for some reason, the two boxes are both red and have the same title "Jane Stag". What do I have to change to make it work?

Or is there a more suitable way for creating this kind of database (without using external files)?

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

Output

3

The problem occurs because the placeholder commands aren't getting expanded inside the loop, which means when the colorbox is used, the placeholder commands are as at the final iteration. Here's a workaround:

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

This produces:

Red and blue boxes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.