PROBLEM
So I'm finishing my tcolorbox
styles for my writings, but I can't manage to center the text correctly if it is shorter than the \linewidth
. One solution would be to add \centering
every time, but it's not automatic, and I'm sure there's a better solution.
I also want that if the text is shorter than the \linewidth
(as an example 3) center the box, and adapt de width of the box depending on the width of the text. Otherwise keep the max width
the \linewidth
and jump to a new line (with \centering
).
In example 1, the centering is not build up correctly, instead of using halgin=center
there should be like a text=centered
(like \centering
). And also, as it's shorter than the \linewidth
, the width of the box should be adapted.
In example 3 the box should be centered, for this example I used hbox
, which doesn't work if the text is larger than the \linewidth
.
So is there any way to combine the example 3 when the text is short (with the box centered), and the example 1 when the text is larger (with correct center)?
MWE
The overlay=
is just in case the box is way to big, and needs to jump to another page the borders are not rounded (to know that the box hasn't finished).
\documentclass[12pt]{report}
\usepackage{amsmath, amssymb}
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\newtcolorbox{box1}[1]{title={\centering\large#1}, fonttitle=\bfseries, toptitle=1.5mm, bottomtitle=1.5mm, enhanced, breakable, colback=gray, colframe=black, boxrule=1pt, arc=2mm, rounded corners, coltitle=white, coltext=white, halign=center,
overlay first={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);},
overlay middle={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);
\draw[line width=1pt, black] (frame.north west)--(frame.north east);},
overlay last={
\draw[line width=1pt, black] (frame.north west)--(frame.north east);;}
}
\newtcolorbox{box2}[1]{title={\centering\large#1}, fonttitle=\bfseries, toptitle=1.5mm, bottomtitle=1.5mm, enhanced, breakable, colback=gray, colframe=black, boxrule=1pt, arc=2mm, rounded corners, coltitle=white, coltext=white,
overlay first={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);},
overlay middle={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);
\draw[line width=1pt, black] (frame.north west)--(frame.north east);},
overlay last={
\draw[line width=1pt, black] (frame.north west)--(frame.north east);;}
}
\newtcolorbox{box3}[1]{title={\centering\large#1}, fonttitle=\bfseries, toptitle=1.5mm, bottomtitle=1.5mm, enhanced, breakable, colback=gray, colframe=black, boxrule=1pt, arc=2mm, rounded corners, coltitle=white, coltext=white, hbox,
overlay first={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);},
overlay middle={
\draw[line width=1pt, black] (frame.south west)--(frame.south east);
\draw[line width=1pt, black] (frame.north west)--(frame.north east);},
overlay last={
\draw[line width=1pt, black] (frame.north west)--(frame.north east);;}
}
\begin{document}
\begin{box1}{Formula of $\sin(\theta)$}
$\forall\theta\in\mathbb{R}, \forall k\in\mathbb{Z},\sin(\theta+2\pi k)=\sin(\theta)$
\end{box1}
\begin{box2}{Formula of $\sin(\theta)$}
$\forall\theta\in\mathbb{R}, \forall k\in\mathbb{Z},\sin(\theta+2\pi k)=\sin(\theta)$
\end{box2}
\begin{box3}{Formula of $\sin(\theta)$}
$\forall\theta\in\mathbb{R}, \forall k\in\mathbb{Z},\sin(\theta+2\pi k)=\sin(\theta)$
\end{box3}
\end{document}
Best Answer
The example below is based on OP's
box3
environment, with some changes:breakable
is commented out since it's not supported whenhbox
is used.varwidth upper
sets a max width forhbox
box. It needsvarwidth
package.halign title=flush center
centers the title,before upper app=\centering
(needs tcolorbox libraryhooks
) centers the (upper part) text, andcenter
centers the box.halign=flush center
has no effect because the alignment is reset by thevarwidth
environment added byvarwidth upper
option.halign
andhalign title
,flush center
andcenter
are different. The first uses LaTeX\centering
which sets\rightskip0pt plus 1fil\leftskip0pt plus 1fil
(and more), while the latter (center
alignment) sets\centering
plus\leftskip0pt plus2em\rightskip0pt plus2em\spaceskip.3333em \xspaceskip.5em \hbadness=10000\relax
. The differences will be noticeable when title text typesets in multiline.Because
halign=center
sets paragraph shape and interword spacing dimensions in relative unitsem
, the effect of centering is influenced by the current font size, which makestitle={\large#1}, fonttitle=\large\bfseries,
andtitle={#1}, fonttitle=\bfseries
slightly different.See related source lines [1] and [2] in
tcolorbox.sty
at v6.0.4.