TransWikia.com

Expl3 and environ to fill boxes title, upper part, and lower part from only user text

TeX - LaTeX Asked on March 17, 2021

The following will take a piece of text and put each piece ending with a blank line in a box using tcolorbox. Each input is preceded by the time stamps.
I’d like the code to fill the upper part of the box with the first piece of text ending with a blank line it finds, with small font and then the second line for the lower part, and so on. If for the last box it finds only one piece of text then it should fill a box without using tcblower.

I started to write a new command, which is "bubbledual" but the environment "rightbubbles" must be further developed.

documentclass{article}
usepackage[many]{tcolorbox}
usepackage{varwidth}
usepackage{environ}
usepackage{xparse}

newlength{bubblesep}
newlength{bubblewidth}
setlength{bubblesep}{2pt}
AtBeginDocument{setlength{bubblewidth}{.75textwidth}}

newcommand{bubble}[2][]{%
  tcbox[
    on line,
    arc=2.5mm,
    rounded corners,
    title=#1,halign title=right
  ]{begin{varwidth}{bubblewidth}#2end{varwidth}}%
}

newcommand{bubbledual}[3][]{%
  tcbox[
    on line,
    arc=2.5mm,
    rounded corners,
    title=#1,halign title=right
  ]{begin{varwidth}{bubblewidth}#2end{varwidth}}%
  tcblower
  {begin{varwidth}{bubblewidth}#3end{varwidth}}%
}

ExplSyntaxOn
seq_new:N l__itt_bubbles_seq
tl_new:N l__itt_bubbles_first_tl
tl_new:N l__itt_bubbles_last_tl

NewEnviron{rightbubbles}[1][]
 {
  begin{flushright}
  sffamily
  seq_set_split:NnV l__itt_bubbles_seq { par } BODY
  int_compare:nTF { seq_count:N l__itt_bubbles_seq < 2 }
   {
     bubble[#1]{BODY}par
   }
   {
    seq_map_inline:Nn l__itt_bubbles_seq
     {
      bubble[#1]{##1}
      parnointerlineskip
      addvspace{bubblesep}
     }
   }
  end{flushright}
 }
ExplSyntaxOff

begin{document}

begin{rightbubbles}[ member A ]
20:20:11" 26 nov. 2020

Right-aligned gray bubbles with black text

20:23:12" 26 nov. 2020

Bubbles only break after a paragraph (equivalent to an enter press 
when chatting). Long message with multiple lines will be kept in one bubble.

20:27:54" 26 nov. 2020

Left and right edges are round.

20:30:06" 26 nov. 2020

Single line ...
end{rightbubbles}

end{document}

One Answer

This does something of the sort. (I prefer the cleaner, traditional defs, but you can convert this to expl3 if you need.) This code comes with a variant of your bubble box that allows for a tcblower.

documentclass{article}
usepackage[many]{tcolorbox}
usepackage{varwidth}
usepackage{environ}
usepackage{xparse}

newlength{bubblesep}
newlength{bubblewidth}
setlength{bubblesep}{2pt}
AtBeginDocument{setlength{bubblewidth}{.75textwidth}}

newcommand{bubble}[2][]{%
  tcbox[
    on line,
    arc=2.5mm,
    rounded corners,
    title=#1,halign title=right
  ]{begin{varwidth}{bubblewidth}#2end{varwidth}}%
}

newcommand{bubbledual}[3][]{%
setbox0hbox{begin{varwidth}{bubblewidth}
small #2
end{varwidth}}%
setbox1hbox{begin{varwidth}{bubblewidth}
#3
end{varwidth}}%
ifdimwd0>wd1relax
edefmybubblewidth{thedimexprwd0+1.2cm}%
else
edefmybubblewidth{thedimexprwd1+1.2cm}%
fi
  begin{tcolorbox}[width=mybubblewidth,
    arc=2.5mm,
    rounded corners,
    title=#1,halign title=right
  ]
  begin{varwidth}{bubblewidth}
  {small #2}
  end{varwidth}%
  tcblower
  begin{varwidth}{bubblewidth}
  #3
  end{varwidth}%
end{tcolorbox}}

defbubblesplit#1newline#2newline{ifx#2relaxrelax
bubble[mytitle]{#1}%
else
bubbledual[mytitle]{#1}{#2}%
fi
}%



ExplSyntaxOn
seq_new:N l__itt_bubbles_seq
tl_new:N l__itt_bubbles_first_tl
tl_new:N l__itt_bubbles_last_tl

NewEnviron{rightbubbles}[1][]
 {
  begin{flushright}
  sffamily
  seq_set_split:NnV l__itt_bubbles_seq { par } BODY
  int_compare:nTF { seq_count:N l__itt_bubbles_seq < 2 }
   {
     %bubble[#1]{BODY}par
      defmytitle{#1}%
      letoldnewlinenewline
      letnewlinerelax
      expandafterbubblesplit##1newlinenewline
      letnewlineoldnewline
   }
   {
    seq_map_inline:Nn l__itt_bubbles_seq
     {
      %bubble[#1]{##1}
      defmytitle{#1}%
      letoldnewlinenewline
      letnewlinerelax
      expandafterbubblesplit##1newlinenewline
      letnewlineoldnewline
      parnointerlineskip
      addvspace{bubblesep}
     }
   }
  end{flushright}
 }
ExplSyntaxOff

begin{document}

begin{rightbubbles}[ member A ]
20:20:11" 26 nov. 2020newline
Right-aligned gray bubbles with black text

20:23:12" 26 nov. 2020newline
Bubbles only break after a paragraph (equivalent to an enter press 
when chatting). Long message with multiple lines will be kept in one bubble.

20:27:54" 26 nov. 2020newline
Left and right edges are round.

20:30:06" 26 nov. 2020newline
Single line dots

a line without time stamp
end{rightbubbles}

end{document}

enter image description here

Answered by user229669 on March 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP