0

with Expl3, environ and tcolorbox one can make an environment that will put each piece of text ending with a white line in a box. I'd like to modify its code given below so that it also fills the "Title" optional field of a tcolobox box and the upper - lower parts. For example in a cyclic way filling in the same order "Tile", "upper" part, and "lower" part as it finds lines ending with a blank line or \end{mynewenviron}. "mynewenviron" being the name of the environ created for this purpose.

\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}
3
  • First, note that a blank line is equivalent to \par. So, messing around with blank lines could also be achieved by redefining \par, I think. Second, I personally don't think \par should be hijacked. For example, you could instead use the tcbitemize environment by the tcolorbox package, if you don't mind adding \tcbitem before each speaker. Maybe there is a way to define \par as \tcbitem in a custom environment but I couldn't figure it out. – jessexknight Nov 25 '20 at 14:50
  • the advantage in the code i inserted in my post is that it allows to do a simple copy and paste into the .tex file and build it. In the worst case the user might need to manually add blank lines where he wants new box creations to be done. Instead of a blank line \par is more complicated as it's a piece of code. The Expl3 syntax should allow a cyclic processing lines in \BODY as here already the environment treats differently the first and last lines. – user1850133 Nov 25 '20 at 16:53
  • Yes but if you redefine \par then blank lines will also invoke the new definition. – jessexknight Nov 25 '20 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.