2

I recently build this little tcolorbox for a template:

\documentclass[a4paper, 12pt]{book}
\usepackage{tcolorbox}
\tcbset{framedboxfilled/.style={
        sharp corners,
        fonttitle={\bfseries},
        coltitle=black,
        toprule=1pt, bottomrule=1pt, titlerule=1pt,
        leftrule=0pt, rightrule=0pt,
    }
}
\tcbset{framedboxoutline/.style={%  
        framedboxfilled, colback=white,colbacktitle=white,
    }
}
\def\myboxstyle{framedboxoutline}% To be set by a class option
\newtcolorbox[auto counter,number within=section]{framedbox}[2][]{%
    \myboxstyle, title={#2}, #1
}

\begin{document}
\begin{framedbox}{Title}
    test
\end{framedbox}
\end{document}

Now I want to include a list of strings, e.g.:

\newcommand{\framedboxValues}{
    Example/example, Homework/homework,
}

This list should contain all names for the resulting tcolorboxes. The first value is for the title, the second value for the LaTeX command. What I want to archive is, that there is some kind of for-loop that takes the values in the list and creates the boxes with the style above. Sadly I have no idea how to archive that. My work so far resulted in this code snippet, that does not really work:

\ExplSyntaxOn
\NewDocumentCommand \defframedthms { m }
{ \clist_map_inline:nn {#1} { \defframedthm {##1} } }
\NewDocumentCommand \defframedthm { > { \SplitArgument { 1 } { / } } m }
{
% Create the tcolorboxes

}
\ExplSyntaxOff
\expandafter\defframedthms\expandafter{\framedboxValues}

I tried several things to create those tcolorboxes, but none of them worked. Can someone help me do that?

EDIT:

I made some progress, but still don't have a good solution. So far I have this:

\newcommand\framedthm[3]{
    \newtcolorbox[auto counter,number within=section]{#2}[2][]{%
        \myboxstyle,
        title={#1 \thetcbcounter~##1},
    }
}

\ExplSyntaxOn
\NewDocumentCommand \defframedthms { m }
{ \clist_map_inline:nn {#1} { \defframedthm {##1} } }
\NewDocumentCommand \defframedthm { > { \SplitArgument { 1 } { / } } m }
{ \framedthm #1 { } }
\ExplSyntaxOff

\expandafter\defframedthms\expandafter{\framedboxValues}

The remaining problems: I can not add a title for the boxes. I can't parse individual changes to the box. So e.g. \begin{box}[colback = red]{Title}... includes all problems I know so far (There is no title and the color is still white). But \begin{box}[title]... works, but without individual options for the tcolorbox.

1
  • I'm not sure about the indirection with the command for the list: is there any need for it?
    – egreg
    Sep 29, 2021 at 17:22

1 Answer 1

1

Are you looking for something like this? Here, I parse the argument to \framedboxvalues and create environments for each item in the list.

\documentclass[a4paper, 12pt]{book}
\usepackage{tcolorbox}
\tcbset{framedboxfilled/.style={
        sharp corners,
        fonttitle={\bfseries},
        coltitle=black,
        toprule=1pt, bottomrule=1pt, titlerule=1pt,
        leftrule=0pt, rightrule=0pt,
    }
}
\tcbset{framedboxoutline/.style={%  
        framedboxfilled, colback=white,colbacktitle=white,
    }
}
\def\myboxstyle{framedboxoutline}% To be set by a class option
\newtcolorbox[auto counter,number within=section]{framedbox}[2][]{%
    \myboxstyle, title={#2}, #1
}
\usepackage{listofitems}
\setsepchar[*]{,*/}
\newcommand\makemyenv[2]{%
  \newenvironment{#2}{\begin{framedbox}{#1}}{\end{framedbox}}%
}
\newcommand\framedboxValues[1]{%
  \ignoreemptyitems
  \readlist*\fbvalues{#1}%
  \def\tmp{}%
  \def\zz{\noexpand\zz}%
  \foreachitem\z\in\fbvalues[]{%
    \edef\tmp{\tmp\noexpand\zz
      {\fbvalues[\zcnt,1]}{\fbvalues[\zcnt,2]}
    }%
  }%
  \let\zz\makemyenv
  \tmp
}

\begin{document}
\framedboxValues{
    Example/example, Homework/homework
}
\begin{example}
    test
\end{example}
\begin{homework}
    test
\end{homework}
\end{document}

enter image description here

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.