使用 Expl3、environ 和 tcolorbox,可以创建一个环境,将以白线结尾的每段文本放在一个框中。我想修改下面给出的代码,以便它还填充 tcolobox 框的“Title”可选字段和上下部分。例如,以循环方式按相同顺序填充“Tile”、“上部”部分和“下部”部分,因为它找到以空白行或 \end{mynewenviron} 结尾的行。“mynewenviron”是为此目的创建的环境的名称。
\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{xcolor}
\usepackage{varwidth}
\usepackage{environ}
\usepackage{xparse}
\newlength{\bubblesep}
\newlength{\bubblewidth}
\setlength{\bubblesep}{2pt}
\AtBeginDocument{\setlength{\bubblewidth}{.75\textwidth}}
\newcommand{\bubble}[1]{%
\tcbox[
on line,
arc=2.5mm,
rounded corners,
]{\begin{varwidth}{\bubblewidth}#1\end{varwidth}}%
}
\ExplSyntaxOn
\seq_new:N \l__ooker_bubbles_seq
\tl_new:N \l__ooker_bubbles_first_tl
\tl_new:N \l__ooker_bubbles_last_tl
\NewEnviron{rightbubbles}
{
\begin{flushright}
\sffamily
\seq_set_split:NnV \l__ooker_bubbles_seq { \par } \BODY
\int_compare:nTF { \seq_count:N \l__ooker_bubbles_seq < 2 }
{
\bubble{\BODY}\par
}
{
\seq_pop_left:NN \l__ooker_bubbles_seq \l__ooker_bubbles_first_tl
\seq_pop_right:NN \l__ooker_bubbles_seq \l__ooker_bubbles_last_tl
\bubble{\l__ooker_bubbles_first_tl}
\par\nointerlineskip
\addvspace{\bubblesep}
\seq_map_inline:Nn \l__ooker_bubbles_seq
{
\bubble{##1}
\par\nointerlineskip
\addvspace{\bubblesep}
}
\bubble{\l__ooker_bubbles_last_tl}
\par
}
\end{flushright}
}
\ExplSyntaxOff
\begin{document}
\begin{rightbubbles}
Right-aligned gray bubbles (103, 184,104) with white text
Bubbles only break after a paragraph (equivalent to an enter press
when chatting). Long message with multiple lines will be kept in one bubble.
Left and right edges are round.
Single
\end{rightbubbles}
\begin{rightbubbles}
Single
\end{rightbubbles}
\end{document}