Breakable boxes with Tikz and right and leftmargin

I've found a method to obtain breakable boxes, using polyglossia for arabic, and the Tikz package and I have two problems...

1) the commands leftmargin and rightmargin of Tikz have no effect on the boxes... there's a package deciding for the width of the box... which one and how could I remain it ?

2) I want to add a title for some frames (different for every frame) in the box of the frametitle (here Example). How can I declare such environment ?

Thank you for help

Here's a simple example with my packages (don't worry for the text written right to left !!)

\documentclass[14pt,a4paper]{extbook}%{article}%
\usepackage{amsmath,amssymb}
\usepackage[framemethod=TikZ]{mdframed}

%\usepackage[dvips]{graphicx}
\usepackage{boxedminipage}
\usepackage{slashbox}

\usepackage{titlesec}
\usepackage{xcolor}
\usepackage[framemethod=TikZ]{mdframed}

\usepackage{enumitem}

\usepackage{minitoc}

\usepackage{rotating}

\usepackage{fmultico}
\setlength{\columnseprule}{0.1pt}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}

\newfontfamily\arabicfont[Script=Arabic, AutoFakeSlant=-0.02]{Amiri}
\setsansfont[Script=Arabic,Scale=1.5]{Amiri}

\tikzstyle{titleframe} =
[draw=black, thick, scale=.7, fill=white,%
line width=0.1pt, text=black, rectangle,
left, minimum height=.5cm]

\newcounter{example}
\renewcommand*\theexample{Example~\arabic{section}.\arabic{example}}

\makeatletter
\mdfdefinestyle{examplestyle}{%
outerlinewidth=1em,%
outerlinecolor=white,%
leftmargin=-1em,%
rightmargin=-1em,%
middlelinewidth=1.2pt,%
%    roundcorner=5pt,%
linecolor=gray!20,%
backgroundcolor=white,%
innertopmargin=1.2\baselineskip,
%    skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
skipabove={\dimexpr1\baselineskip\relax},
skipbelow={0em},
needspace=3\baselineskip,
frametitlefont=\sffamily\bfseries,
settings={\global\stepcounter{example}},
firstextra={%
\node[titleframe,xshift=-1cm] at (P-|P) %
{\RL{~{\mdf@frametitlefont{\theexample}}~}};},%
singleextra={%
%        \node[titleframe,xshift=1cm] at (P-|O) %
\node[titleframe,xshift=-1cm] at (P-|P) %
{~{\mdf@frametitlefont{\theexample}}~};}
}
\makeatother

\newenvironment{example}%
{\begin{mdframed}[style=examplestyle]}{\end{mdframed}}

\begin{document}

\begin{example}
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}

\end{example}

\end{document}


With tcolorbox. There is still room for improvement but this should serve as a starting point.

\documentclass[14pt,a4paper]{extbook}%{article}%
\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}

%\usepackage[dvips]{graphicx}
\usepackage{boxedminipage}
\usepackage{slashbox}

\usepackage{titlesec}

\usepackage{enumitem}

\usepackage{minitoc}

\usepackage{rotating}

\usepackage{fmultico}
\setlength{\columnseprule}{0.1pt}

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{polyglossia}
\setmainlanguage[numerals=maghrib]{arabic}
\setotherlanguage{english}

\newfontfamily\arabicfont[Script=Arabic, AutoFakeSlant=-0.02]{Amiri}
\setsansfont[Script=Arabic,Scale=1.5]{Amiri}

\newcounter{example}
\renewcommand*\theexample{Example~\arabic{section}.\arabic{example}}

\newtcolorbox[use counter=example]{example}[3][]{%
enhanced,
colback=blue!10!white,
colframe=orange,
top=6mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
label={#3},
overlay unbroken and first={%
\node[rectangle,rounded corners,draw=black,fill=blue!20!white,
inner sep=1mm,anchor=east,font=\small,text width=0.5\textwidth]
at ([xshift=-1cm]frame.north east)
{\strut\RL{\textbf{Example \thetcbcounter: #2}}};},
breakable,
width=0.9\textwidth,       %%% change the width here.
#1}%

\begin{document}
\noindent
\begin{example}{My title goes long and long and long}{ex:first}   %% last one is label
To calculate the horizontal position the kinematic differential
equations are needed:
\begin{align}
\dot{n} &= u\cos\psi -v\sin\psi \\
\dot{e} &= u\sin\psi + v\cos\psi
\end{align}
For small angles the following approximation can be used:
\begin{align}
\dot{n} &= u -v\delta_\psi \\
\dot{e} &= u\delta_\psi + v
\end{align}

\end{example}

\end{document}


• Hello. Thank you, it seems good. I think that there was a problem of compatibility between tcolorbox and polyglossia, and it seems that it isn't now. I see this in detail today and tell you about it. Many thanks – Faouzi Bellalouna Mar 14 '14 at 7:04
• Hello Harish. After updating the package options of tcolorbox (most), and copy-paste ...exactly... your doc, I got the title frame "Example 1..." written justified on the left not on the right... How can I paste the pdf file here ?? – Faouzi Bellalouna Mar 14 '14 at 9:41
• Hello I solved this last problem... It's the option "text width=0.5\textwidth".. when you bring it, it justifies the text on the right... What's the effect of this option ?.. I'll read the doc of the package... Thanks – Faouzi Bellalouna Mar 14 '14 at 10:25
• Hello Harish. Everything is going well this macro, else that when the box is too long, it breaks pages "without any logic"... it puts a blank page before the long box... sometimes inside the box... What's do you think the problem ? – Faouzi Bellalouna Mar 18 '14 at 14:14
• @FaouziBellalouna Difficult to say without code. May be you can ask a new question... – user11232 Mar 18 '14 at 14:58