2

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.

3 Answers 3

4

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

This is a tricky solution and I can't guarantee that it works in all circumstances. But for simple text it should probably be sufficient.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{ulem}

\begin{document}

\long\def\myuline#1\tcblower{\uline{#1}\tcblower}

\begin{tcolorbox}[before upper=\myuline,
title=My heading]
This is another \textbf{tcolorbox}.
The upper part of it should be underlined if everything goes right.

As any dedicated reader can clearly see, the Ideal of practical reason
is a representation of, as far as I know, the things in themselves; as I
have shown elsewhere, the phenomena should only be used as a canon for
our understanding. The paralogisms of practical reason are what first
give rise to the architectonic of practical reason. As will easily be
shown in the next section, reason would thereby be made to contradict,
in view of these considerations, the Ideal of practical reason, yet the
manifold depends on the phenomena. Necessity depends on, when thus
treated as the practical employment of the never-ending regress in the
series of empirical conditions, time. Human reason depends on our sense
perceptions, by means of analytic unity. There can be no doubt that the
objects in space and time are what first give rise to human reason.

\tcblower
This is the lower part of the box.
\end{tcolorbox}

\end{document}

enter image description here

1

Inspired by Jinwen's attempt:

\documentclass{article}

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

\ExplSyntaxOn
\NewDocumentEnvironment{tcolorboxunderlineupper}{O{}+b}
 {
  \niksirat_tcboxuu:nn { #1 } { #2 }
 }
 {}

\seq_new:N \l__niksirat_tcboxuu_full_seq
\seq_new:N \l__niksirat_tcboxuu_upper_seq
\seq_new:N \l__niksirat_tcboxuu_upper_uline_seq

\cs_new_protected:Nn \niksirat_tcboxuu:nn
 {
  % split the contents at \tcblower
  \seq_set_split:Nnn \l__niksirat_tcboxuu_full_seq { \tcblower } { #2 }
  % split the upper part at \par
  \seq_set_split:Nnx \l__niksirat_tcboxuu_upper_seq
   { \par }
   { \seq_item:Nn \l__niksirat_tcboxuu_full_seq { 1 } }
  \seq_set_map:NNn \l__niksirat_tcboxuu_upper_uline_seq \l__niksirat_tcboxuu_upper_seq
   { \uline{##1} }
  \begin{tcolorbox}[#1]
  % deliver the upper part
  \seq_use:Nn \l__niksirat_tcboxuu_upper_uline_seq { \par }
  % cope with the lower part
  \int_compare:nF { \seq_count:N \l__niksirat_tcboxuu_full_seq == 1 }
   {
    \tcblower \seq_item:Nn \l__niksirat_tcboxuu_full_seq { 2 }
   }
  \end{tcolorbox}
 }
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnx }
\ExplSyntaxOff

\begin{document}

\begin{tcolorboxunderlineupper}
Some text for test.
\end{tcolorboxunderlineupper}

\begin{tcolorboxunderlineupper}
Some text for test.
\tcblower
Here, you see the lower part of the box.
\end{tcolorboxunderlineupper}

\begin{tcolorboxunderlineupper}
Some text for test.

Another paragraph.
\tcblower
Here, you see the lower part of the box.
\end{tcolorboxunderlineupper}

\begin{tcolorboxunderlineupper}[colback=red!30!green]
Some text for test.

Another paragraph.
\tcblower
Here, you see the lower part of the box.
\end{tcolorboxunderlineupper}

\end{document}

enter image description here

The tcolorboxunderlineupper environment has an optional argument to specify options for the inner tcolorbox, as shown in the last example.

You must log in to answer this question.

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