I'm trying to make code formatting a conversation between two ends with boxes left and right respectively. So far i have seen examples in which two \NewEnviron were used respectively for left side boxes and right ones.

I'd like to make a single \newcommand that will decide on the basis of a parameter value whether it should be \begin{flushleft} or \begin{flushright}

here is my non-working try:


    \begin{tcolorbox}[width=0.5\textwidth, title=#2]

% use it like this:
\bubble{flushleft}{friend}{hello, what's your plan today?}
  • There are a few issues in your code. With \newcommand you have to give the name of the macro with a leading backslash, so it should be \newcommand\bubbles.... Whereas in \begin you should not use a leading backslash, so correct would be \begin{flushright} or \begin{flushleft}, same for \end. Is the title in #2 to be always the same for the same alignment? How do you intend to use \bubbles? Can you show a (non-)working minimal example how you intend to use it?
    – Skillmon
    Feb 23, 2021 at 11:44
  • for the backslash before "bubbles" it's a typo Feb 23, 2021 at 11:59

3 Answers 3


If you give the first parameter to \bubbles without backslash, you can just use it as parameter to \begin. This simplifies the command a lot, with no test being necessary.

I suppose varwidth is to be used somewhere else, because for this simple example it isn't necessary.


    \begin{tcolorbox}[width=0.5\textwidth, title=#2]



\bubbles{flushleft}{Title Left}{Contents left\\\lipsum[1][1-2]}

\bubbles{flushright}{Title Right}{Contents right\\\lipsum[1][3-4]}

More text


enter image description here

  • i left \usepackage{varwidth} in the example here because i was not sure whether \textwidth was part of it. Feb 23, 2021 at 12:26
  • \textwidth is standard LaTeX. Feb 23, 2021 at 12:42
  • without additional options what would be the simplest way to make left boxes with title and content right aligned, and right boxes left aligned? Nothing justified! Feb 23, 2021 at 14:57

You can achieve a much simpler syntax:


  \mbox{\begin{tcolorbox}[width=0.5\textwidth, title=#2]


\bubble{<}{friend}{hello, what's your plan today?}


enter image description here


If you want to use the same title and alignment for each speaker, it might be a better idea to store that information inside a key you can use as the first mandatory argument to \bubble. I added an optional argument to \bubble with which individual customizations can be made.

The following does so using expkv-def for a key=value interface (of course other packages could be used for this as well).

The box key can be used to forward additional keys to the tcolorbox environment (in the example I use this to specify colours for each speaker).

The align key might take the value center in which case a center environment is used, else the used environment will be flush<align>, so right and left are possible values.



% we define new keys here. The `store' type of keys will store the value during
% its usage inside the macros. The `code' type allows own definitions. We use it
% to define a key that will add additional contents to an existing macro
% (\bubble@box) with a comma between the old and new material.
% With `initial' we set initial values for the keys `width` and `align`, all
% other macros are initially empty (title and box).
% The `BOX' key will overwrite any other option passed to `tcolorbox', whereas
% the `box' key will add to the list of options.
     store title = \bubble@title
    ,store align = \bubble@align
    ,store BOX   = \bubble@box
    ,code  box   = \edef\bubble@box{\unexpanded\expandafter{\bubble@box,#1}}
    ,store width = \bubble@wd
    ,initial width = .5\textwidth
    ,initial align = left
% we define a shorthand macro to set options of bubbles.
% the real macro
    % contain the definitions done by the key=value interface inside a group so
    % that one call can't affect another one.
      % test if the macro holding defaults exists, if so call it 
      \ifcsname bubble@defaults@\detokenize{#2}\endcsname
        \csname bubble@defaults@\detokenize{#2}\endcsname
      % parse the additional options with our key=value interface
      % handle the `center' alignment by comparing the current alignment with a
      % temporary macro
      \begin{\ifx\bubble@tmp\bubble@align\else flush\fi\bubble@align}
        % expand the contents of `\bubble@box' so that the key=value interface
        % of tcolorbox can do its job
      \end{\ifx\bubble@tmp\bubble@align\else flush\fi\bubble@align}
% helper macro to expand the contents of `\bubble@box' before calling
% `tcolorbox'.
    % check if the title is empty, if so, don't use the title key of tcolorbox.
      \begin{tcolorbox}[width=\bubble@wd, #1]
      \begin{tcolorbox}[width=\bubble@wd, title=\bubble@title, #1]
% define a default option list. The optional argument is used as is for the
% `tcolorbox' options, the rest is parsed by our key=value interface. Since we
% use it inside of `\edef' the values passed to the keys here are fully
% expanded. This shouldn't be an issue normally.
    \expandafter\edef\csname bubble@defaults@\detokenize{#2}\endcsname

% define the two speakers (with short name A and B)
\bubbledef[colframe=red!50!black]{A}{title=Speaker A, align=left}
\bubbledef[colframe=green!50!black]{B}{title=Speaker B, align=right}

\usepackage{duckuments} % just for some dummy text

\bubble[title=New Speaker]{}{A new speaker shows up.}
\bubble[align=center, width=.7\textwidth]{}{Another fat speaker is here.}

enter image description here


2021-02-23, 18:38 (UTC+1)

  • added a lot of comments
  • added an optional argument to \bubbledef to set the default tcolorbox options in an, imho, nicer way.
  • changed the box key to add additional options instead of clearing the list
  • added the BOX key (does what the box key did previously)
  • the end result looks really nice, but i don't know as much of latex. Most of your code would require a lot of homework. Feb 23, 2021 at 14:50
  • @user1850133 just ask what you don't understand and I'll try to explain... :)
    – Skillmon
    Feb 23, 2021 at 17:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .