# Frame only around the boxtitle in Tcolorbox. Making “half-background”

Writing a book in arabic, using polyglossia, I use Tcolorbox to make examples, exercises, theorems and so.. I have two kinds of titled examples: one with Arabic title, in RighToLeft mode, obtained by the option

  title=\RL{\textbf{#2}}


and another with two titles, one in Arabic and one in english, obtained by the option

  title=\RL{\textbf{#2}}}\hfill{\small\LR{\textbf{#3}}


I use the command ''colbacktitle=gray!20'' for the background of the title, and I want two things:

1/ First, I want to obtain, for the case of the only arabic title a "half-box" of the title, so a boxtitle wich has a width equal the half-width of the box of the examples.

2/ Another thing what I want is to make a frame for the tcolorbox but only around the title of the example

Thanks for any help

I edit the question to put a MWE:

\documentclass[14pt,twoside]{book}

\usepackage{extsizes}

\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage{array}
\usepackage[total={165mm,235mm},centering,twoside]{geometry}%

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}
\newfontfamily\arabicfont[Script=Arabic,Scale=1.2,AutoFakeSlant=-0.02]{Simplified Arabic}
\setmonofont{DejaVu Sans Mono}
\let\arabicfonttt\ttfamily
\setsansfont[Script=Arabic,Scale=1.5]{Simplified Arabic}

\tikzstyle{whitebox} =
[draw=white, thick, scale=.707, fill=white,%
line width=0.3pt, text=black, rectangle,
font=\Large,
left, minimum height=.5cm, minimum width=1.9cm]

\newcounter{example}

\newtcolorbox[auto counter,
number within=section,
number freestyle={\noexpand\arabic{\tcbcounter}}]{example}[2][]{%
enhanced,
left=-5pt,
right=-5pt,
colback=white,
colframe=white,
top=5pt,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#2},
overlay unbroken and first={%
\node[whitebox]
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
#1}%

\newtcolorbox[use counter from=example]{texample}[3][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\mbox{\hspace*{5pt}\RL{\textbf{#2}}},
colback=white,
colframe=red,
top=0mm,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
enlarge top  by=-2mm,
fontupper=\normalsize,
label={#3},
overlay unbroken and first={%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[whitebox] (B)
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
enlarge top at break by=0mm,pad at break=2mm,
#1%
}%

\newtcolorbox[use counter from=example]{teaexample}[4][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\hspace*{5pt}\RL{\textbf{#2}}\hfill\begin{english}\LR{\textbf{#3}}\end{english},
colback=white,
colframe=red,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#4},
overlay unbroken and first={%
\node[whitebox] (B)
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
enlarge top at break by=0mm,pad at break=2mm,
#1%
}%

\begin{document}

\begin{example}{l1}
\LR{The text of the example (labeled here Ex) is usually in arabic, in RightToLeft mode}
\end{example}

\begin{texample}{\LR{Arabic title1}}{l2}
\LR{Text of another example}
\end{texample}
\begin{teaexample}{\LR{Arabic title2}}{English title}{l3}
\LR{Another text of another example}
\end{teaexample}
\end{document}


I give the compilation of this file (which must be done with the xelatex compiler)

For the second example, I want a box title of width half of the total width (so 82.5 mm)

Other thing. Is it possible to have the frame (red here) around the box of the title only (here in gray) ?

• Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Mar 7 '15 at 17:38
• Excuse me but I need some time to make my code understandable. I'll do this tomorrow with your permission. Thanks – Faouzi Bellalouna Mar 7 '15 at 18:23
• Permission granted ;-) But perhaps you should omit the arabic font/language parts since not everybody has the fonts/knowledge of it ;-) – user31729 Mar 7 '15 at 18:23
• @SoundsOfSilence See that I have edited the question and put a MWE, in Arabic environment without any Arabic letter...! So you don't need any font ! – Faouzi Bellalouna Mar 7 '15 at 21:44

Set the boxrule to 0pt (so no frame is drawn for the complete box) and use one of the options for attaching the title, for example:

boxrule=0pt,
minipage boxed title=\textwidth,
attach boxed title to top right,
boxed title style={
enhanced,
arc=0pt,
outer arc=0pt,
colbacktitle=gray!40,
colframe=red,
}


Using the minipage boxed title=<width> you can control the desired width for the boxed title and then you can use boxed title style to further customize the box used for the title.

\documentclass[14pt,twoside]{book}

\usepackage{extsizes}

\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}
\usepackage{array}
\usepackage[total={165mm,235mm},centering,twoside]{geometry}%

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}
\setmonofont{DejaVu Sans Mono}
\let\arabicfonttt\ttfamily

\tikzstyle{whitebox} =
[draw=white, thick, scale=.707, fill=white,%
line width=0.3pt, text=black, rectangle,
font=\Large,
left, minimum height=.5cm, minimum width=1.9cm]

\newcounter{example}

\newtcolorbox[auto counter,
number within=section,
number freestyle={\noexpand\arabic{\tcbcounter}}]{example}[2][]{%
enhanced,
left=-5pt,
right=-5pt,
colback=white,
colframe=white,
top=5pt,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#2},
overlay unbroken and first={%
\node[whitebox]
at ([xshift=1.5cm,yshift=-1cm]frame.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
#1}%

\newtcolorbox[use counter from=example]{texample}[3][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\mbox{\hspace*{5pt}\RL{\strut\textbf{#2}\strut}},
colback=white,
boxrule=0pt,
minipage boxed title=0.5\textwidth,
attach boxed title to top right,
boxed title style={
enhanced,
arc=0pt,
outer arc=0pt,
colbacktitle=gray!40,
colframe=red,
},
top=0mm,
bottom=5pt,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
enlarge top  by=-2mm,
fontupper=\normalsize,
label={#3},
overlay unbroken and first={%
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\node[whitebox] (B)
at ([xshift=1.5cm,yshift=-1cm]frame.north east|-title.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
enlarge top at break by=0mm,pad at break=2mm,
#1%
}%

\newtcolorbox[use counter from=example]{teaexample}[4][]{%
enhanced,
left=-5pt,
right=-5pt,
coltitle=black,
colbacktitle=gray!40,
title=\hspace*{5pt}\RL{\strut\textbf{#2}}\hfill\begin{english}\LR{\textbf{#3}\strut}\end{english},
colback=white,
boxrule=0pt,
minipage boxed title=\textwidth,
attach boxed title to top right,
boxed title style={
enhanced,
arc=0pt,
outer arc=0pt,
colbacktitle=gray!40,
colframe=red,
},
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#4},
overlay unbroken and first={%
\node[whitebox] (B)
at ([xshift=1.5cm,yshift=-1cm]frame.north east|-title.north east)
{\strut\RL{\setlength{\extrarowheight}{-30.5pt}\textbf{
$\begin{array}{c} \hline \rule{0pt}{2ex} \mbox{Ex} \\ \mbox{\large\thetcbcounter} \rule{0pt}{1.5ex} \end{array}$}}};},
breakable,
before=\par\smallskip\centering,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
extrude right by=5pt,
extrude left by=5pt,
enlarge top at break by=0mm,pad at break=2mm,
#1%
}%

\begin{document}

\begin{example}{l1}
\LR{The text of the example (labeled here Ex) is usually in arabic, in RightToLeft mode}
\end{example}

\begin{texample}{\LR{Arabic title1}}{l2}
\LR{Text of another example}
\end{texample}
\begin{teaexample}{\LR{Arabic title2}}{English title}{l3}
\LR{Another text of another example}
\end{teaexample}
\end{document}


## Remarks

• I added \struts to the titles so the boxes will have the same height with or without descenders/ascenders.

• The position of the "Ex #" label is now made with respect to both the frame and title "boxes" so it is consistent with the use of the new definitions.

• I changed the Arabic font to Sheherazade since I don't have the one you were using but this is irrelevant for the solution.
• @FaouziBellalouna You're welcome. In some hours I'll add some improvements to my answer. – Gonzalo Medina Mar 8 '15 at 12:11
• ok for another answer – Faouzi Bellalouna Mar 8 '15 at 13:33
• @FaouziBellalouna Please see my updated answer. I explain the improvements in the "Remarks" section. – Gonzalo Medina Mar 9 '15 at 0:02
• Thank you. In the case of half-width frame I think that a good improvement is to put the frame not of fixed length (half of the width) but to make this length variable, depending of the length of the title, something like "length-title+10pt". How can we do this ? – Faouzi Bellalouna Mar 9 '15 at 4:52
• Excuse me but I haven't found in your answer the improvement that you talk about – Faouzi Bellalouna Mar 9 '15 at 4:58