tcolorbox
add tag
With the help of frougon, I almost get what I like.
The last thing I’m missing is how to set the flush right option when the tcolorbox has the varwidth option. Please look at the third green boxes of the two examples below:
xxxxxxxxxx
\documentclass[final]{ltugboat}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\usepackage{mwe}
\usepackage{showframe}
\newlength{\mytitlelen}
\makeatletter
\tcbset{
commonoptions/.style={
enhanced, show bounding box,
fonttitle=\bfseries\scriptsize,
titlerule=0pt, boxrule=0pt, boxsep=0pt,
toptitle=6pt, bottomtitle=4pt,
opacityframe=0,
},
myenlargeleft/.style={
enlarge left by=\linewidth - (\kvtcb@width)},
% width/.store in=\kvtcb@width, https://github.com/T-F-S/tcolorbox/blob/9858cec2254aab25a099d919e158a05132abab31/tex/latex/tcolorbox/tcolorbox.sty#L202
mywidth/.code={%
\settowidth{\mytitlelen}{\bfseries\scriptsize #1}%
\tcbset{width=\mytitlelen+\kvtcb@lefttitle+\kvtcb@righttitle}%
},
}
\makeatother
\NewTColorBox{receivedmsg}{s o m}{
commonoptions,
coltitle=red, colbacktitle=yellow!30,
colback=yellow!30,
title={#3},
IfBooleanTF={#1}{%
mywidth={#3},
}{%
hbox, varwidth upper=.8\linewidth,
},
halign=flush left,
IfValueT={#2}{#2}
}
\NewTColorBox{sentmsg}{s o m}{
commonoptions,
halign title=flush right,
halign=flush right,
coltitle=blue, colbacktitle=green!30,
colback=green!30,
title={#3},
IfBooleanTF={#1}{%
mywidth={#3},
}{%
hbox, varwidth upper=.8\linewidth,
},
IfValueT={#2}{#2},
myenlargeleft,
}
\begin{document}
\blindtext
\begin{receivedmsg}{Tostin:}
You know, I spent a lot of years disliking women. But I don't dislike you.
\end{receivedmsg}
\begin{sentmsg}*{Edna:}
Oh?
\end{sentmsg}
\begin{receivedmsg}{Tostin:}
You're not a woman. You're more than a woman. You're a \emph{mechanic}.
\end{receivedmsg}
\begin{sentmsg}{Edna:}
Half line length
\end{sentmsg}
\begin{sentmsg}{Edna:}
I would like this to be flush right.
I would like this to be flush right.
\end{sentmsg}
\begin{receivedmsg}*{Tostin:}
Y
\end{receivedmsg}
\begin{receivedmsg}{Tostin:}
Half line length
\end{receivedmsg}
\blindtext
\begin{receivedmsg}{Tostin:}
You know, I spent a lot of years disliking women. But I don't dislike you.
\end{receivedmsg}
\begin{sentmsg}*{Edna:}
Oh?
\end{sentmsg}
\begin{receivedmsg}{Tostin:}
You're not a woman. You're more than a woman. You're a \emph{mechanic}.
\end{receivedmsg}
\begin{sentmsg}{Edna:}
Half line length
\end{sentmsg}
\begin{sentmsg}{Edna:}
\flushright How to have this effect without explicitly using \cs{flusright} in the box text?
\end{sentmsg}
\begin{receivedmsg}*{Tostin:}
Y
\end{receivedmsg}
\begin{receivedmsg}{Tostin:}
Half line length
\end{receivedmsg}
\blindtext
\end{document}
show all 103 lines