0

Sorry that the question description isn't great, but what I mean is how can you update the settings to support having either a title that's just default from the tcbcounter like Example 1.1 or something more descriptive like Example 1.1: Making a tcolorbox - something to help distinguish examples apart and give descriptions so I can easily come back to them later.

I've currently updated this tcolorbox to support both listings and text/comments; however, I would also like to make the title work like what I described above. Here is the code for the box (with examples working here):

\newtcblisting[auto counter, number within=section, list inside=examplelist]{tcbexample}[2][]{%
        colback=gray!5, colbacktitle=gray!40, coltitle=black,
        frame hidden, arc=2pt, titlerule=0pt, toptitle=2pt, bottomtitle=2pt,
        fonttitle=\bfseries, breakable, enhanced, parbox=false,
        segmentation style={solid, draw=gray!20, line width=1pt}, % personal update
        comment and listing,
        title=Example~\thetcbcounter,
        comment={#2},#1
}

enter image description here

Is there some way the settings for the tcolorbox could be altered to change the title so that it can reflect something more descriptive like Example 1.1: Making a tcolorbox (but when such input is left empty, it defaults to Example 1.1? Thanks!

4
  • Does this answer your question? Remove colon from title bar in tcolorbox theorem without a title
    – user202729
    Commented Nov 7, 2021 at 2:50
  • ... actually on closer look there's a slight difference (theorem versus listing)...
    – user202729
    Commented Nov 7, 2021 at 2:52
  • Good call, I'll add that now. Sorry for the confusion!
    – Revise
    Commented Nov 7, 2021 at 2:57
  • Thanks for the input - I'm terrible at coming up with title names so sometimes it might not be descriptive enough. I'll try to improve my questions in the future.
    – Revise
    Commented Nov 8, 2021 at 1:52

1 Answer 1

1

You can make use of the after title key:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable, listings, skins}

\newtcblisting[auto counter, number within=section, list inside=examplelist]{tcbexample}[2][]{%
        colback=gray!5, colbacktitle=gray!40, coltitle=black,
        frame hidden, arc=2pt, titlerule=0pt, toptitle=2pt, bottomtitle=2pt,
        fonttitle=\bfseries, breakable, enhanced, parbox=false,
        segmentation style={solid, draw=gray!20, line width=1pt}, % personal update
        comment and listing,
        title=Example~\thetcbcounter,
        comment={#2},#1
}

\pgfkeysifdefined{/tcb/name}{\fail}{
  \tcbset{
    name/.style={after title={: #1}}
  }
}

\begin{document}
\section{title}
\begin{tcbexample}[name=title]{comment}
code
\end{tcbexample}

\begin{tcbexample}{no title}
code
\end{tcbexample}
\end{document}

enter image description here

tcolorbox has a built-in logic for this kind of appending an "optional name" to title (also check the key separator sign), but unfortunately it's hard-coded in the internal of \tcbnewtheorem (more specifically, \tcb@theo@title), hence will only apply for tcolorbox-defined theorem environments.

2
  • Thanks! Would adding the name attribute would be a global change, i.e. it would work for all boxes?
    – Revise
    Commented Nov 7, 2021 at 6:00
  • 1
    @Revise Yes, every tcolorbox-defined box can use name option. Commented Nov 8, 2021 at 5:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .