0

Basically, I am defining a tcolorbox and I want the title of the box to be of the form "Example X.XX Name of the example", with "Example X.XX" having a colored background and "Name of the example" having a white background.

I tried two alternatives:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}

\begin{document}

\begin{tcolorbox}[
coltitle = white, colframe = blue, colbacktitle = blue,
arc = 0mm, outer arc = 0mm, 
title = Example X.XX Name of the example]
\lipsum[1]
\end{tcolorbox}

\begin{tcolorbox}[coltitle = blue, colframe = blue, colbacktitle = white,
arc = 0mm, outer arc = 0mm, 
title = \colorbox{blue}{\color{white}{Example X.XX}} Name of the example]
\lipsum[1]
\end{tcolorbox}

\end{document}

In the first case, the whole title is colored, and in the second one there is an ugly gap between the colored box and the frames of the box i am defining. How can I achieve this.

2
  • Welcome to TeX.SX! Would you mind to provide a minimal but complete example, from \documentclass to \end{document}. This way it's easier to help you.
    – Ignasi
    Commented Jun 18, 2022 at 8:10
  • @Ignasi I edited the question to a working example Commented Jun 18, 2022 at 20:37

3 Answers 3

3

You need load the skin library, use enhanced option and create your own title frame box. Fill the whole title region with white color first. Then fill the left side until the words 'Example X.XX' to another color (e.g. blue). I add some spaces between 'Example X.XX' and 'Name of the example' to make the words in different filled region. And I apply different text color as well. See if this is what you want.

\documentclass{article}
\usepackage[skins]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}

\begin{document}
\begin{tcolorbox}[
coltitle = white, colframe = blue, colbacktitle = blue,
arc = 0mm, outer arc = 0mm, 
title = Example X.XX Name of the example]
\lipsum[1]
\end{tcolorbox}

\begin{tcolorbox}[
colframe = blue, 
coltitle=red,
sharp corners,
enhanced,
boxed title style={
    boxrule=1.4pt,
    sharp corners,
    interior code={
    \fill [white] (interior.north west) rectangle (interior.south east);
    \filldraw [blue] (interior.north west) rectangle ([xshift=3.5cm]interior.south west);
    },
},
attach boxed title to top,
title = \textcolor{white}{Example X.XX}\qquad Name of the example]
\lipsum[1]
\end{tcolorbox}
\end{document}

enter image description here

If you want a auto counter tcolorbox, I recommend you define your own box using xparse library. In follow example the counter will change within section.

\documentclass{article}
\usepackage[skins,xparse]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\DeclareTColorBox[auto counter,number within=section]{mybox}{ O{} m }{
colframe = blue, 
coltitle=red,
sharp corners,
enhanced,
boxed title style={
    boxrule=1.4pt,
    sharp corners,
    interior code={
    \fill [white] (interior.north west) rectangle (interior.south east);
    \filldraw [blue] (interior.north west) rectangle ([xshift=3cm]interior.south west);
    },
},
attach boxed title to top,
title = \textcolor{white}{Example \thetcbcounter}\qquad #2,
#1
}
\begin{document}
\section{One}
\begin{mybox}{First example name}
\lipsum[1]
\end{mybox}

\section{Two}
\begin{mybox}[coltitle=black]{Second Name of the example}
\lipsum[2]
\end{mybox}
\end{document}

enter image description here

according to your comment. You could do this. I create new counter myexample just to show the effect. You can use auto counter as the previous example.

\documentclass{article}
\usepackage[skins,xparse]{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}
\newlength{\lefttitle}
\newcounter{myexample}
\DeclareTColorBox[use counter=myexample,number within=section]{mybox}{ O{} m }{
colframe = blue, 
coltitle=red,
sharp corners,
enhanced,
boxed title style={
    boxrule=1.4pt,
    sharp corners,
    interior code={
    \fill [white] (interior.north west) rectangle (interior.south east);
    \node [white,fill=blue,anchor=west,outer sep=0pt,inner sep=3pt] at ([xshift=-0.05pt]interior.west) {Example~\thetcbcounter} ;
    },
},
attach boxed title to top,
title =\settowidth{\lefttitle}{Example~\thetcbcounter}\hspace{\lefttitle}#2,
#1
}
\begin{document}
\section{One}
\begin{mybox}{First example name}
\lipsum[1]
\end{mybox}

\section{Two}
\setcounter{myexample}{100}
\setcounter{section}{1000}
\begin{mybox}[coltitle=black]{Second Name of the example}
\lipsum[2]
\end{mybox}
\end{document}

enter image description here

5
  • thank you for your answer, in this case I understand that the blue box has a fixed size, is there a way to have de \filldraw command to be the length of the text? So that if I have example 1.1 or example 200.200, it would look consistent and not one more squeezed than the other? I don't think this might be an issue for me, as I will be working with smal numbers and the same text in the box; but I am curious if it's possible/viable, or if I have to change the lenght depending on the environment I am using (i.e theorem, example, lemma, etc.) Commented Jun 19, 2022 at 2:33
  • @Daniel I add a method in the answer.
    – Tom
    Commented Jun 19, 2022 at 3:13
  • @Daniel For other case (e.g. theorem,lemma), you can define the a another tcolorbox, and set the width using \newlength{\Tlefttitle} and \settowidth{\Tlefttitle}{Theorem~\thetcbcounter} accordingly.
    – Tom
    Commented Jun 19, 2022 at 3:18
  • thank you, I am actually using an example, theorem, etc. counter to each one of them, so this is precisely what I needed, thank you. One last question, can I use `` \node [white,fill=blue,anchor=west,outer sep=0pt,inner sep=3pt] at ([xshift=-0.05pt]interior.west) {#1}```` if I wanted the argument to be boxed? or would that throw an error? Your solution is already what I wanted, I'm just curious. Commented Jun 20, 2022 at 5:18
  • @Daniel #1 is the optional arg without defalut value, #2 will be title name right now, if you need a other one, you set it in \DeclareTColorBox[use counter=myexample,number within=section]{mybox}{ O{} m m}, The last m here indicate another mandatory arg, using #3 in the definition. So you environment will have following syntax begin{mybox}[#1]{#2}{#3} some contents... \end{mybox} .
    – Tom
    Commented Jun 20, 2022 at 5:33
1

Tikz library tikzmark should be the ultimate solution but I didn't succeed.

Here's a lengthy way:

  • a costumed \xcolorbox[<options>]{<color>}{<text>} similar to \colorbox but through <options> the box separations (and bleed margins) on each side can be configured, and
  • a new user command \tcbtitlename{<color>}{<text>} based on \xcolorbox, with knowledge of tcolorbox internals that hold the value of separations.

Usage: \begin{tcolorbox}[title=\tcbtitlename{blue}{text} text, ...]

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{lipsum}

\makeatletter
\pgfqkeys{/colorbox}{
  left/.initial=\fboxsep,
  right/.initial=\fboxsep,
  top/.initial=\fboxsep,
  bottom/.initial=\fboxsep,
  grow to left by/.initial=0pt,
  grow to right by/.initial=0pt,
  grow to top by/.initial=0pt,
  grow to bottom by/.initial=0pt,
}

% \xcolorbox{<color>}{<text>}
% \xcolorbox[left=<dim>, ...]{<color>}{<text>}
%
\protected\def\xcolorbox{\@ifnextchar[\xcolor@box{\xcolor@box[]}}
\protected\def\xcolor@box[#1]#2#{\xcolor@@box{#1}{#2}}
\def\xcolor@@box#1#2#3{\xcolor@b@x{#1}\relax{\color#2{#3}}}

\long\def\xcolor@b@x#1#2#3#4%
 {\leavevmode
  \begingroup\pgfqkeys{/colorbox}{#1}%
  \setbox\z@\hbox{%
    \kern\dimexpr\pgfkeysvalueof{/colorbox/left}+\pgfkeysvalueof{/colorbox/grow to left by}\relax%
    {\set@color#4}%
    \kern\dimexpr\pgfkeysvalueof{/colorbox/right}+\pgfkeysvalueof{/colorbox/grow to right by}\relax}%
  \dimen@\ht\z@
  \advance\dimen@\dimexpr\pgfkeysvalueof{/colorbox/top}+\pgfkeysvalueof{/colorbox/grow to top by}\relax
  \ht\z@\dimen@
  \dimen@\dp\z@
  \advance\dimen@\dimexpr\pgfkeysvalueof{/colorbox/bottom}+\pgfkeysvalueof{/colorbox/grow to bottom by}\relax
  \dp\z@\dimen@
  \hskip-\pgfkeysvalueof{/colorbox/grow to left by}%
  \setbox\z@\hbox{%
    {#2{#3\color@block{\wd\z@}{\ht\z@}{\dp\z@}\box\z@}}}%
  \ht\z@\dimexpr\ht\z@-\pgfkeysvalueof{/colorbox/grow to top by}\relax
  \dp\z@\dimexpr\dp\z@-\pgfkeysvalueof{/colorbox/grow to bottom by}\relax
  \box\z@
  \hskip-\pgfkeysvalueof{/colorbox/grow to right by}%
  \endgroup}

% \tcbtitlename{<color>}{<title name>}
\newcommand\tcbtitlename[2]{%
  \xcolorbox[%
    left= 0pt,
    top=0pt,
    bottom=0pt,
    grow to left by=\dimexpr\kvtcb@lefttitle+\kvtcb@boxsep\relax,
    grow to top by=\dimexpr\kvtcb@toptitle+\kvtcb@boxsep\relax,
    grow to bottom by=\dimexpr\kvtcb@bottomtitle+\kvtcb@boxsep\relax,
  ]{#1}{#2}%
}
\makeatother

\begin{document}

\begin{tcolorbox}[
  coltitle = white, colframe = blue, colbacktitle = blue,
  arc = 0mm, outer arc = 0mm, 
  title = Example X.XX Name of the example
]
  \lipsum[2]
\end{tcolorbox}

\begin{tcolorbox}[
  coltitle = blue, colframe = blue, colbacktitle = white,
  arc = 0mm, outer arc = 0mm, 
  title = \tcbtitlename{blue}{\color{white}Example X.XX} Name of the example,
]
  \lipsum[2]
\end{tcolorbox}
\end{document}

enter image description here

1
  • thank you for your answer, I don't think I have the knowledge to make good use of it, but this seems very efficient if I am defining a lot of similar looking boxes, thank you Commented Jun 20, 2022 at 5:26
1

You can use only TeX primitive commands \hbox, \vbox, \hrule, \vrule, \setbox, \kern etc. You need not use the tcolorbox package in this case. Advantage: all is controlled by you.

\documentclass{article}
\usepackage{lipsum}
\usepackage{xcolor}
\newdimen\rulew \rulew=1.4pt
\def\hruleb{\hrule height\rulew} 
\def\vruleb{\vrule width\rulew}

\long\def\mybox#1#2#3{\par
   \setbox0=\vbox{{\leftskip=\parindent\rightskip=\parindent \parindent=0pt
      \color{black}\medskip #3\par\medskip
   }}
   \setbox1=\hbox{{\hskip\parindent\color{white}#1\quad}}
   \vbox{
      \color{blue}\hruleb
      \hbox to\hsize{%
         \vruleb\hss
         \vbox{
            \hbox{\rlap{\vrule height1.1em depth.5em width\wd1}\box1 \quad\color{black}#2}
            \hruleb
            \hbox{\rlap{\kern\rulew
                 \color{lightgray!20}\vrule height\ht0 width\dimexpr\hsize-\rulew}\box0}%
         }\hss\vruleb
      }
      \hruleb
}}
\begin{document}
\mybox{Example X.XX}{Name of the example}{
   \lipsum[1]
}
\end{document}
3
  • Hi @wipet, really like your answer. Would you mind I edit it to include a document class and relative packages. Or you prefer make it as Optex syntax. People can compile it and learn more from it.
    – Tom
    Commented Jun 19, 2022 at 7:43
  • @Tom you can add document class etc. It is LaTeX syntax: colors are selected by syntax from xcolor LaTeX package, so document class etc. can be here.
    – wipet
    Commented Jun 19, 2022 at 17:37
  • thank you for you answer, I like how customizable it is. I will try to learn from this, but as I said to a previous commenter, I might not be abe to use all its advantages yet Commented Jun 20, 2022 at 5:31

You must log in to answer this question.

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