How to use command in upper part of \tcolorbox environment, like \underline

listingstcolorboxunderline

How can I use \underline command for upper part of \tcolorbox, and underline all text in upper part?
I test

before upper=\uline{,
after upper=},

or

before upper=\underline{,
after upper=},

but not work.

Best Answer

Your code does not work because as soon as you insert {, the grouping is initiated, and you would get something like

enter image description here

To solve this, one can define an environment-version of \uline. Here I use the +b type argument to record the content of the environment and apply \uline on it.

\NewDocumentEnvironment{uline-env}{+b}{\uline{#1}}{}

Then you can do

before upper*=\begin{uline-env},
after upper*=\end{uline-env},

which gives you:

enter image description here

Unfortunately, this approach does not work with \tcblower (which pretty much makes the upper specifier useless). The grouping mechanism here is rather complicated. I'm looking forward if someone can resolve this issue.

ADD: If you can guarantee that there shall always be a \tcblower in your environment, please then refer to Pieter van Oostrum's nice answer.

\documentclass{article}

\usepackage[many]{tcolorbox}
\usepackage{ulem}

\NewDocumentEnvironment{uline-env}{+b}{\uline{#1}}{}

\begin{document}

\begin{tcolorbox}[
    before upper*=\begin{uline-env},
    after upper*=\end{uline-env},
  ]
    Some text for test.
    % \tcblower
    % Here, you see the lower part of the box.
\end{tcolorbox}

\end{document}
Related Question