Center and adapt width in tcolorbox

centerhorizontal alignmenttcolorboxwidth

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}

enter image description here

Best Answer

The example below is based on OP's box3 environment, with some changes:

  • Option breakable is commented out since it's not supported when hbox is used.
  • Option varwidth upper sets a max width for hbox box. It needs varwidth package.
  • Option halign title=flush center centers the title, before upper app=\centering (needs tcolorbox library hooks) centers the (upper part) text, and center centers the box.
    • Here halign=flush center has no effect because the alignment is reset by the varwidth environment added by varwidth upper option.
    • Feasible values for halign and halign title, flush center and center 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 units em, the effect of centering is influenced by the current font size, which makes title={\large#1}, fonttitle=\large\bfseries, and title={#1}, fonttitle=\bfseries slightly different.
      See related source lines [1] and [2] in tcolorbox.sty at v6.0.4.
\documentclass{article}

\usepackage{amsmath, amssymb}
\usepackage{tcolorbox}
\usepackage{varwidth}
\tcbuselibrary{breakable, hooks, skins}

\usepackage{lipsum}

\newtcolorbox{box3}[1]{
  % hbox mode boxes are not breakable
  enhanced, % breakable,
  hbox, 
%  tcbox width=auto limited,
  center,
  varwidth upper,
  before upper app=\centering,
  halign title=flush center,
  title={#1}, fonttitle=\large\bfseries,
  toptitle=1.5mm, bottomtitle=1.5mm,
  boxrule=1pt,
  arc=2mm, rounded corners,
  colback=gray, colframe=black,
  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);}
}

\begin{document}

\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}

\begin{box3}{Formula of $\sin(\theta)$}
  \lipsum[1][1-3]
\end{box3}

\begin{box3}{\lipsum[2][1-2]}
  $\forall\theta\in\mathbb{R}, \forall k\in\mathbb{Z},\sin(\theta+2\pi k)=\sin(\theta)$
\end{box3}

\begin{box3}{\lipsum[2][1-2]}
  \lipsum[1][1-3]
\end{box3}

\newpage
\subsection*{\textsf{halign=center} vs \textsf{halign=flush center}}

\begin{tcolorbox}[
  center, halign=center, fontupper=\Huge,
  title={\texttt{halign=center, fontupper=\string\Huge}}
]
  \leavevmode\llap{\smash{\rule{.4pt}{1em}}}Text Text
\end{tcolorbox}

\begin{tcolorbox}[
  center, halign=center, before upper=\Huge,
  title={\texttt{halign=center, before upper=\string\Huge}}
]
  \leavevmode\llap{\smash{\rule{.4pt}{4em}}}Text Text
\end{tcolorbox}

\begin{tcolorbox}[center, halign=center, title={\texttt{halign=center}}]
  \lipsum[1][1-3]
\end{tcolorbox}

\begin{tcolorbox}[center, halign=flush center, title={\texttt{halign=flush center}}]
  \lipsum[1][1-3]
\end{tcolorbox}

\end{document}

main output "halign=center" vs "halign=flush center"