tcolorbox add tag
CarLaTeX
How can I enlarge to the right and flush left a tcolorbox simultaneously?

See the green box in the example, I also showed the bounding box for debugging.

```
\documentclass[final]{ltugboat}
\usepackage{mwe}
\usepackage{showframe}
\usepackage[most]{tcolorbox}
\usetikzlibrary{tikzlings}
\usepackage{calc}
\usepackage{varwidth}
\tcbset{
  commonoptions/.style={
  enhanced, show bounding box,
    fonttitle=\bfseries\scriptsize,
    titlerule=0pt, boxrule=0pt, boxsep=0pt,
    toptitle=6pt,bottomtitle=4pt,
		opacityframe=0,
    varwidth upper=.9\linewidth}}
\newtcolorbox{receivedmsg}[2][]{
  commonoptions,    
		halign title=flush left,
    halign=flush left,
    enlarge left by=9mm,
    overlay={
      \pic[scale=.3] at ([yshift=-6mm,xshift=-6mm]frame.north west) {bear};
      \path[fill=yellow!30] (frame.north) --([xshift=-3mm]frame.north west) -- ([yshift=-3mm]frame.north west) -- cycle;
      },
  coltitle=red,colbacktitle=yellow!30, 
	colback=yellow!30, hbox,
  title={#2},#1}
\newtcolorbox{sentmsg}[2][]{
  commonoptions,  
  hbox,	
    enlarge right by=9mm,
    flush right,
		halign title=flush right,
    halign=flush right,
    overlay={
      \pic[scale=.3] at ([yshift=-6mm,xshift=6mm]frame.north east) {elephant};
      \path[fill=green!30] (frame.north) --([xshift=3mm]frame.north east) -- ([yshift=-3mm]frame.north east) -- cycle;
      },
  coltitle=blue,colbacktitle=green!30,	
  colback=green!30,title={#2},#1}    
\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:}
  \makebox[\widthof{\bfseries\scriptsize Edna:}][l]{Oh?}
\end{sentmsg}
\begin{receivedmsg}{Tostin:}
  You're not a woman. You're more than a woman. You're a \emph{mechanic}.
\end{receivedmsg}
\blindtext
\end{document}
```

![image.png](/image?hash=459eacc9d7cf81dc82dfe14e379791bed68ddc651cc7ebd88b3d6f21acce60a9)

Desired result:

![image.png](/image?hash=3a5964dd1c39186796dc1f9bf83d209d77b919381112d2028fe96d2fac9687b1)
Top Answer
frougon
I first removed distracting stuff like the `hbox` and `varwidth upper` options. If you really want the `0.9\linewidth` thing, you can readd these options (see below). Regarding the green box that is of interest here, I used:

```
\tcbset{flush right/.show code}
```

This led me to define the following style:

```
my flush right/.style={
  enlarge left by=\linewidth - (\kvtcb@width + #1),
},
```

which appears to do what you want. I also added the `mywidth` style for convenience and to comply with the DRY principle.

```
\documentclass[final]{ltugboat}
\usepackage[skins]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{tikzlings}
\usepackage{blindtext}

\makeatletter
\newlength{\carlatex@len}
\newdimen\myrightskip
\myrightskip=9mm                % for the green box

\tcbset{
  commonoptions/.style={
    enhanced, show bounding box,
    fonttitle=\bfseries\scriptsize,
    titlerule=0pt, boxrule=0pt, boxsep=0pt,
    toptitle=6pt, bottomtitle=4pt,
    opacityframe=0,
  },
  my flush right/.style={
    enlarge left by=\linewidth - (\kvtcb@width + #1),
  },
  mywidth/.code={%
    \settowidth{\carlatex@len}{\bfseries\scriptsize #1}%
    \tcbset{width=\carlatex@len + \myrightskip}%
  },
}
\makeatother

\newtcolorbox{receivedmsg}[2][]{
  commonoptions,
  left skip=9mm,
  overlay={
    \pic[scale=.3] at ([shift={(-6mm,-6mm)}]frame.north west) {bear};
    \path[fill=yellow!30] (frame.north) -- ([xshift=-3mm]frame.north west) --
                          ([yshift=-3mm]frame.north west) -- cycle;
    },
  coltitle=red, colbacktitle=yellow!30, colback=yellow!30,
  title={#2}, #1,
}

\newtcolorbox{sentmsg}[2][]{
  commonoptions,
  my flush right=\myrightskip, right skip=\myrightskip,
  overlay={
    \pic[scale=.3] at ([shift={(6mm,-6mm)}]frame.north east) {elephant};
    \path[fill=green!30] (frame.north) -- ([xshift=3mm]frame.north east) --
                         ([yshift=-3mm]frame.north east) -- cycle;
  },
  coltitle=blue, colbacktitle=green!30, colback=green!30,
  title={#2}, #1,
}

\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}[mywidth={Edna:}]{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}
\blindtext
\end{document}
```

![new.png](/image?hash=cab696e033e4f11dbacbf5ad642e9affee33388e52f9f75c7781e77e17d8c488)

# With the `varwidth upper` style

`varwidth upper=.9\linewidth` seemed a bit too short here (I guess it is influenced by our options that leave room for the callout). The following code is essentially the same as above but uses `hbox, varwidth upper=.8\linewidth` for `receivedmsg` boxes.

Note that the `mywidth` style, which relies on `/tcb/width`, is incompatible with `hbox` and `varwidth upper`; therefore, in this example I have only used these two styles in `receivedmsg` boxes.

```
\documentclass[final]{ltugboat}
\usepackage[skins]{tcolorbox}
\usepackage{tikz}
\usetikzlibrary{tikzlings}
\usepackage{varwidth}
\usepackage{blindtext}

\makeatletter
\newlength{\carlatex@len}
\newdimen\myrightskip
\myrightskip=9mm                % for the green box

\tcbset{
  commonoptions/.style={
    enhanced, show bounding box,
    fonttitle=\bfseries\scriptsize,
    titlerule=0pt, boxrule=0pt, boxsep=0pt,
    toptitle=6pt, bottomtitle=4pt,
    opacityframe=0,
  },
  my flush right/.style={
    enlarge left by=\linewidth - (\kvtcb@width + #1),
  },
  mywidth/.code={%
    \settowidth{\carlatex@len}{\bfseries\scriptsize #1}%
    \tcbset{width=\carlatex@len + \myrightskip}%
  },
}
\makeatother

\newtcolorbox{receivedmsg}[2][]{
  commonoptions,
  hbox, varwidth upper=.8\linewidth,
  left skip=9mm,
  overlay={
    \pic[scale=.3] at ([shift={(-6mm,-6mm)}]frame.north west) {bear};
    \path[fill=yellow!30] (frame.north) -- ([xshift=-3mm]frame.north west) --
                          ([yshift=-3mm]frame.north west) -- cycle;
    },
  coltitle=red, colbacktitle=yellow!30, colback=yellow!30,
  title={#2}, #1,
}

\newtcolorbox{sentmsg}[2][]{
  commonoptions,
  my flush right=\myrightskip, right skip=\myrightskip,
  overlay={
    \pic[scale=.3] at ([shift={(6mm,-6mm)}]frame.north east) {elephant};
    \path[fill=green!30] (frame.north) -- ([xshift=3mm]frame.north east) --
                         ([yshift=-3mm]frame.north east) -- cycle;
  },
  coltitle=blue, colbacktitle=green!30, colback=green!30,
  title={#2}, #1,
}

\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}[mywidth={Edna:}]{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}
\blindtext
\end{document}
```

![new2.png](/image?hash=80995db0733c331088b90435ded45a745867216d04931ad21bf2506a4851a217)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.