2

I have an environment created with tcolorbox where the upper part is in bold text, and the lower part is not. Sometimes I use \tcbsubtitle inside this environment, and I would like that to correspond to the separation of the upper and lower part. If I do not use \tcbsubtitle, then \tcblower adds the correct amount of space, but if I use \tcbsubtitle in addition, the space becomes too much.

I realize that the purpose of \tcbsubtitle is not to start the lower part of the box, but since the only difference between the upper and the lower part is the font weight, I thought I could get \tcbsubtitle to change the font following it. I did try the following without success:

\tcbset{
    subtitle style = {after  = {\normalfont}}
}

Here is a minimal working example:

\documentclass{article}

\usepackage[most]{tcolorbox}

\newtcbtheorem{example}{Example}{
    middle           = 1em,
    lower separated  = false,
    fontupper        = \bfseries\boldmath\raggedright,
    fontlower        = \raggedright,
    fonttitle        = \bfseries\boldmath\raggedright,
}{ex}

\begin{document}

\begin{example}{}{}
    Bold text
    \tcblower % Appropriate amount of vertical space
    Normal text
\end{example}

\begin{example}{}{}
    Bold text
    \tcblower % Too much space
    \tcbsubtitle{Subtitle}
    Normal text
\end{example}

\begin{example}{}{}
    Bold text
    \tcbsubtitle{Subtitle} % Appropriate amount of vertical space
    Bold text              % Still bold
\end{example}

\end{document}

1 Answer 1

1

The code below has a conditional \ifLower, toggled true immediately after entering the lower part. The \tcbsubtitle is redefined to use this conditional to detect if it is the first subtitle in the lower part, and if it is, \vspace{-2em} is applied. The result looks like below:

enter image description here

\documentclass{article}

\usepackage[most]{tcolorbox}

\newif\ifLower\Lowerfalse

\newtcbtheorem{example}{Example}{
    before lower     = {\Lowertrue},
    middle           = 1em,
    lower separated  = false,
    subtitle style   = {height = 1.5em},
    fontupper        = \bfseries\boldmath,
    fonttitle        = \bfseries\boldmath,
    fontlower        = \normalfont,
}{ex}

\let\tcbsubtitleOriginal=\tcbsubtitle
\renewcommand{\tcbsubtitle}{%
    \ifLower\vspace{-2em}\fi%
    \Lowerfalse%
    \tcbsubtitleOriginal%
}

\begin{document}

\begin{example}{}{}
    Bold text
    \tcblower
    Normal text
\end{example}

\begin{example}{}{}
    Bold text
    \tcblower
    \tcbsubtitle{Subtitle}
    Normal text
\end{example}

\begin{example}{}{}
    Bold text
    \tcblower
    \tcbsubtitle{Subtitle}
    Normal text
    \tcbsubtitle{Subtitle}
    Normal text 2
\end{example}

\end{document}
2
  • This suggestion does not work if the same box contains multiple \tcbsubtitle.
    – user44413
    Mar 8, 2021 at 13:06
  • @user44413 Check my new edit, this should work.
    – Jinwen
    Mar 8, 2021 at 13:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .