如何使用 \tcolorbox 环境上部的命令,例如 \underline

如何使用 \tcolorbox 环境上部的命令,例如 \underline

我怎样才能对 \tcolorbox 的上部使用 \underline 命令,并为上部的所有文本添加下划线?我测试

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

或者

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

但不起作用。

答案1

您的代码不起作用,因为只要您插入{,就会启动分组,您会得到类似的结果

在此处输入图片描述

为了解决这个问题,可以定义一个环境版本\uline。这里我使用+b类型参数来记录环境的内容并应用\uline它。

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

然后你可以做

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

这将为您提供:

在此处输入图片描述

不幸的是,这种方法不适用\tcblower(这几乎使upper说明符变得毫无用处)。这里的分组机制相当复杂。我希望有人能解决这个问题。

添加:如果您能保证您的环境中始终存在\tcblower,请参考 Pieter van Oostrum 的精彩回答。

\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

这是一个棘手的解决方案,我不能保证它在所有情况下都有效。但对于简单的文本来说,它可能就足够了。

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

在此处输入图片描述

答案3

受到Jinwen的尝试的启发:

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

在此处输入图片描述

环境tcolorboxunderlineupper有一个可选参数来指定内部的选项tcolorbox,如最后一个例子所示。

相关内容