5

in my book I have a lot of tcolorboxes to use. They are slightly different so I added a macro that decides depending on the arguments, which tcolorbox to draw. Obviously that produces a LOT of source code (one command per tcolorbox).

So I tried to reduce it but it doesn't work properly.

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240} % Farbe für + -

\newlength{\bs}\setlength{\bs}{30pt} % Basislänge   
\newlength{\ns}

\newcommand{\iround}{
                interior titled code={
                    \fill[mp,rounded corners=15pt]
                        ([xshift=-1pt,yshift=-10pt]interior.west)
                        rectangle
                        ([xshift=\ns,yshift=3pt]title.north west); 
                    \draw[rounded corners=15pt,color=mp, line width=1pt,fill=white]
                        (frame.south west) -- 
                        (title.south west) -- 
                        (title.south east) --
                        (frame.south east) -- cycle;}
}

\newcommand{\ibs}[3]{\scantokens{%
        \setlength{\ns}{\widthof{#1}+\bs}
            \begin{tcolorbox}[   
                arc=15pt,
                outer arc=15pt,
                title=#1,
                coltitle=black,
                enhanced,
                freelance,
                frame code={},
                \iround
            ]
            \raggedright #2
            \end{tcolorbox}}}


\begin{document}

\ibs{title}{A}{B}

\end{document}
  • It works if you say \newcommand\iround{ \fill[mp,rounded corners=15pt] ([xshift=-1pt,yshift=-10pt]interior.west) rectangle ([xshift=\ns,yshift=3pt]title.north west); \draw[rounded corners=15pt,color=mp, line width=1pt,fill=white] (frame.south west) -- (title.south west) -- (title.south east) -- (frame.south east) -- cycle; } and add interior titled code=\iround as an option for the tcolorbox. – Gonzalo Medina Apr 13 '14 at 18:05
  • could you explain why I have to omit the "interior titled code="? – Moldevort Apr 13 '14 at 18:16
3

Your code will work if you just leave the \fill and \draw parts in the definition of \iround and move the interior titled code to the argument of tcolorbox:

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240} % Farbe für + -

\newlength{\bs}\setlength{\bs}{30pt} % Basislänge   
\newlength{\ns}

\newcommand{\iround}{
                    \fill[mp,rounded corners=15pt]
                        ([xshift=-1pt,yshift=-10pt]interior.west)
                        rectangle
                        ([xshift=\ns,yshift=3pt]title.north west); 
                    \draw[rounded corners=15pt,color=mp, line width=1pt,fill=white]
                        (frame.south west) -- 
                        (title.south west) -- 
                        (title.south east) --
                        (frame.south east) -- cycle;
}

\newcommand{\ibs}[3]{\scantokens{% I am not sure why \cantokens was used here
        \setlength{\ns}{\widthof{#1}+\bs}
            \begin{tcolorbox}[   
                arc=15pt,
                outer arc=15pt,
                title=#1,
                coltitle=black,
                enhanced,
                freelance,
                frame code={},
                interior titled code=\iround
            ]
            \raggedright #2
            \end{tcolorbox}}}


\begin{document}

\ibs{title}{A}{B}

\end{document}

The reason is an expansion problem, similar to the one present in Using strings to list parameters for tikz-cd arrows; Steven B. Segletes showed in his answer there a possibility using \expandafters that might also work here (I didn't tested it), but I think (as he himself mentioned in his answer) that other approaches (like the ones I propose here) might be safer.

enter image description here

Another option is to use a style, as in:

\tcbset{
iround/.style={
  interior titled code={
                    \fill[mp,rounded corners=15pt]
                        ([xshift=-1pt,yshift=-10pt]interior.west)
                        rectangle
                        ([xshift=\ns,yshift=3pt]title.north west); 
                    \draw[rounded corners=15pt,color=mp, line width=1pt,fill=white]
                        (frame.south west) -- 
                        (title.south west) -- 
                        (title.south east) --
                        (frame.south east) -- cycle;}
  }                        
}

and then use this style for the tcolorbox:

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{calc}
\usetikzlibrary{calc}

\definecolor{mp}{RGB}{240,240,240} % Farbe für + -

\newlength{\bs}\setlength{\bs}{30pt} % Basislänge   
\newlength{\ns}

\tcbset{
iround/.style={
  interior titled code={
                    \fill[mp,rounded corners=15pt]
                        ([xshift=-1pt,yshift=-10pt]interior.west)
                        rectangle
                        ([xshift=\ns,yshift=3pt]title.north west); 
                    \draw[rounded corners=15pt,color=mp, line width=1pt,fill=white]
                        (frame.south west) -- 
                        (title.south west) -- 
                        (title.south east) --
                        (frame.south east) -- cycle;}
  }                        
}

\newcommand{\ibs}[3]{\scantokens{% I am not sure why \cantokens was used here
        \setlength{\ns}{\widthof{#1}+\bs}
            \begin{tcolorbox}[   
                arc=15pt,
                outer arc=15pt,
                title=#1,
                coltitle=black,
                enhanced,
                freelance,
                frame code={},
                iround
            ]
            \raggedright #2
            \end{tcolorbox}}}


\begin{document}

\ibs{title}{A}{B}

\end{document}

Which approach to use will depend on the actual intent; unfortunately, the question doesn't contain enough information of the actual use case to decide which one would be the best alternative here.

Another thing that calls my attention is the use of \scantokens; once again the problem is that the MWE posted doesn't give enough information as to decide whether this is really necessary.

  • The first one works best. As I have many different styles, I'm using a macro that changes the style depending on the arguments. – Moldevort Apr 13 '14 at 20:35

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.