T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
213 stars 15 forks source link

`toprule=0pt` changes the layout of the left-bottom and right-bottom corners #277

Closed dbitouze closed 2 months ago

dbitouze commented 2 months ago

As shown by the following MCE, toprule=0pt changes the layout of the left-bottom and right-bottom corners. Is it a bug or a feature?

\documentclass{article}
\usepackage{tcolorbox}
\usepackage[paperwidth=12cm,paperheight=2cm,margin=.25cm]{geometry}
\pagestyle{empty}
\begin{document}
\tcbset{sharp corners=north}
\begin{tcolorbox}
  Foo
\end{tcolorbox}
\newpage
\begin{tcolorbox}[toprule=0pt]
  Foo
\end{tcolorbox}
\end{document}
T-F-S commented 2 months ago

Actually, it is a feature. With rounded corners, a tcolorbox has four identical inner arcs (interior) and outer arcs (frame). The radius of an outer arc is the radius of the inner arc plus the rule width.

Things get complicated, if there is no identical rule width. Here is valid:

Your toprule=0pt therefore makes the outer arc radius smaller.

Fortunately, you can overrule this algorithm by setting a manual outer arc radius. With default values for rule 0.5mm plus inner arc radius 1.0mm you may use outer arc=1.5mm.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage[paperwidth=12cm,paperheight=2cm,margin=.25cm]{geometry}
\pagestyle{empty}
\begin{document}

\begin{tcolorbox}
  Foo
\end{tcolorbox}
\newpage
\begin{tcolorbox}[toprule=0pt]
  Foo
\end{tcolorbox}
\newpage
\begin{tcolorbox}[toprule=0pt,outer arc=1.5mm]
  Foo
\end{tcolorbox}

\newpage
\tcbset{sharp corners=north}
\begin{tcolorbox}
  Foo
\end{tcolorbox}
\newpage
\begin{tcolorbox}[toprule=0pt]
  Foo
\end{tcolorbox}
\newpage
\begin{tcolorbox}[toprule=0pt,outer arc=1.5mm]
  Foo
\end{tcolorbox}
\end{document}

grafik

Box 2 is the intended behaviour for rounded corners, Box 3 has the manual settings for demonstration.

Box 6 is the box you possibly want to use.

dbitouze commented 2 months ago

OK, thanks. Too bad the feature isn't to autocorrect the outer arc radius :wink:

muzimuzhi commented 2 months ago

skin=enhancedlast gives a similar result, but the box is higher than the one with toprule=0pt by width of toprule.

muzimuzhi commented 2 months ago

Things get complicated, if there is no identical rule width. Here is valid:

  • outer arc radius = inner arc radius + minimum of the rules for the four sides

Maybe this can be added to the doc of /tcb/auto outer arc. Its current doc only says the auto-computed outer arc is dependent on /tcb/inner arc. https://github.com/T-F-S/tcolorbox/blob/067f384e2ba85ba3fd91958eb6adb8a1bcea6c29/doc/latex/tcolorbox/tcolorbox.doc.coreoptions.tex#L1169-L1172


If there's a way to expandably retrieve current values of aspects of tcolorboxes set by options, then the magic number 1.5mm in outer arc=1.5mm can be replaced with an expression, like outer arc/.expanded=\dimeval{\tcbgetvalue{outer arc}-\tcbgetvalue{bottomrule}}.

Example below shows various workarounds, including use of a new option fix outer arc which immediately computes radius of outer arc in the way auto outer arc will do, but in a latter place.

Boxes a.3.x and b.2.x all give the expected result, but in different ways.

Example

```tex \documentclass{article} \usepackage[skins]{tcolorbox} \usepackage[hmargin=.5in, twocolumn]{geometry} \pagestyle{empty} \makeatletter \tcbset{ fix outer arc/.code={% \tcb@comp@arc@auto % compute \tcb@outer@arc right now \let\tcb@comp@arc=\@empty % no further computation is needed } } \makeatother \NewDocumentCommand{\testTcb}{ O{} m }{ \begin{tcolorbox}[#1] \vphantom{Foo}\smash{#2} \end{tcolorbox} } \begin{document} \tcbset{every box/.style={sharp corners=north}} \testTcb[] {a.1 \texttt{sharp corners=north}} \testTcb[toprule=0pt] {a.2 \texttt{+ toprule=0pt}, smaller outer arc} \testTcb[toprule=0pt, outer arc=1.5mm] {a.3.1 \texttt{+ outer arc=1.5mm}} \makeatletter \testTcb[ outer arc=1.5mm, % evaluates to 1mm + .5mm = 1.5mm outer arc/.expanded=\dimeval{\kvtcb@arc+(\kvtcb@top@rule@stand)}, toprule=0pt ]{a.3.2 \texttt{+ outer arc = arc - original toprule}} \makeatother \testTcb[fix outer arc, toprule=0pt] {a.3.3 \texttt{+ fix outer arc} (new option)} \newpage \tcbset{every box/.style={skin=enhancedlast}} \testTcb[] {b.1 \texttt{skin=enhancedlast}, box higher by toprule} \testTcb[top=1.5mm] {b.2.1 \texttt{+ top=1.5mm}} \makeatletter \testTcb[ top/.expanded=\dimeval{\kvtcb@top-(\kvtcb@top@rule@stand)} ]{b.2.2 \texttt{+ top = top - toprule}} \makeatother \end{document} ```

image