Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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