# Deciding the width of a centered box withTcolorbox

I'm writing a book and I want to use boxes, using the package tcolorbox. I've found on some old discussion here the commands of \newtcolorbox{blankbox} with the command width=0.9\textwidth, it produces a centered box but of 0.9 width. When I put the width to another value, it's not centered.. My question is how can I obtain, a box always centered as the page, and with the width what I want?

I have another problem too. Sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the right, sometimes on the left (like in the second box here, written in Arabic). What's the reason for this?

I give a minimal example here, with the packages of my book:

    \documentclass[14pt,a4paper]{extbook}%{article}%

\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{etoolbox}
\usepackage{multicol}

\usepackage{amsmath,amssymb}
\usepackage[most]{tcolorbox}

\usepackage{boxedminipage}
\usepackage{slashbox}

\usepackage{titlesec}

\usepackage{enumitem}

\usepackage{minitoc}

\usepackage{rotating}

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

\usepackage{fancyhdr}
\pagestyle{fancy}

\usepackage{pdflscape}

\usepackage{array}

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

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

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

\newtcolorbox{blankbox}[2][]{%
enhanced,
%oversize,
colback=white,
boxrule=.5pt,
colframe=black,
top=6mm,
bottom=6mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
overlay unbroken and first={%
\node[titlewhite]
at ([xshift=-1cm]frame.north east)
{\strut\RL{\textbf{#2}}};},
breakable,
width=0.9\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
%enlarge left by=-.06\textwidth,
extrude right by=-5pt,
extrude left by=-5pt,
#1}%

\newtcolorbox{blankbox1}[2][]{%
enhanced,
%oversize,
colback=white,
boxrule=.5pt,
colframe=black,
top=6mm,
bottom=6mm,
enlarge top by=\baselineskip/2+1mm,
enlarge top at break by=0mm,pad at break=2mm,
fontupper=\normalsize,
overlay unbroken and first={%
\node[titlewhite]
at ([xshift=-1cm]frame.north east)
{\strut\RL{\textbf{#2}}};},
breakable,
width=\textwidth,       %%% change the width here.
arc=0pt,outer arc=0pt,
%enlarge left by=-.06\textwidth,
extrude right by=-5pt,
extrude left by=-5pt,
#1}%

\begin{document}

\begin{blankbox}{title}
I want to obtain a box having the same width of the text, and centered as the text... More precisely, in which point do tcolorbox puts the box              exactly??... And how can I obtain, a box always centered as the page, and with the width what I want ??

I have another problem too... sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the             right, sometimes on the left... (like in the second box here, written in arabic)...what's the reason for this ?
\end{blankbox}

\begin{blankbox1}{title}
I want to obtain a box having the same width of the text, and centered as the text... More precisely, in which point do tcolorbox puts the box              exactly??... And how can I obtain, a box always centered as the page, and with the width what I want ??

I have another problem too... sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the             right, sometimes on the left... (like in the second box here, written in arabic)...what's the reason for this ?
\end{blankbox1}

\end{document}

-
Could you please format source code(s) as source code(s) by adding four spaces in front of each line? I am using Gedit to format a large block of code: selecting my code, pressing Tab twice and it is done. –  Malipivo Apr 24 '14 at 15:56
I have deleted the arabic expressions on the second box for clearness... Thank you –  Faouzi Bellalouna Apr 24 '14 at 15:56
Hello Malipivo. Ok I'll try.. Thanks –  Faouzi Bellalouna Apr 24 '14 at 15:57
I hope that it seems good now ! –  Faouzi Bellalouna Apr 24 '14 at 16:16

You can define a centered box by using the \centering macro, like this:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{tcolorbox}
\newtcolorbox{cbox}[1][]{
before=\par\smallskip\centering,
#1
}
\begin{document}
some text before
\begin{cbox}[width=.9\textwidth]
test
\end{cbox}
some text after
\begin{cbox}[width=.8\textwidth]
test
\end{cbox}
\begin{cbox}[width=.7\textwidth]
test
\end{cbox}
\begin{cbox}[width=.6\textwidth]
test
\end{cbox}
\begin{cbox}[width=.5\textwidth]
test
\end{cbox}
\end{document}


After the centered box template is set you can specify the width of each box inside the document and they'll all be centered.

-
Thank you for the help...simple !.. what about my other question ?... "I have another problem too. Sometimes the rules of the boxes doesn't appear... sometimes the upper sometimes the lower rule, sometimes on the right, sometimes on the left. What's the reason for this? –  Faouzi Bellalouna Apr 25 '14 at 14:19
@FaouziBellalouna The other question is not realated to the placement of the tcolorbox, you should ask a separate question and provide an example of disappearing rule (I could not compile your mwe in this question). It might be an issue with your pdf viewer (so not in TeX) but we can't say until you provide an appropriate mwe. –  dcmst Apr 25 '14 at 15:55
Ok Thanks... I'll see it later.. –  Faouzi Bellalouna Apr 25 '14 at 19:14