3

I'm trying to implement a kind of chat inside my document using tcolorbox. But I either can set the width dynamically or allow a pagebreak inside a "chat-message".

\documentclass[11pt, a4paper]{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{blindtext}

\tcbset{breakable, width=.8\linewidth, tcbox width=auto limited, size=title, arc=2mm, boxrule=.5pt, boxsep=1mm}

%page break possible
\newcommand{\coach}[1]{\begin{tcolorbox}[colback=yellow, sharp corners=northwest]#1\end{tcolorbox}}

%dynamic width
\newcommand{\klient}[1]{\tcbox[colback=green, sharp corners=southeast, flush right]{#1}}

\begin{document}

\blindtext[2]
\coach{Hello World}
\klient{Hello World}

\coach{\blindtext[2]}
\klient{\blindtext[3]}

\end{document}

Does anyone know how to meet both requirements?

Thank you in advance.

3
  • 1
    Welcome. I don't think this is possible. auto limited is an option for \tcbox only, as I understand the documentation, whereas breakable is a setting applicable to \tcolorboxes. These rely on different underlying structures, I think, so what's possible for one is not always possible for the other.
    – cfr
    Jul 19 at 19:24
  • Thanks. I worked around with an \ifdim statement, comparing the textlength. This meets my requirements.
    – Tony
    Jul 20 at 11:20
  • 1
    Perhaps you could post that as an answer? It might help somebody else with the same problem.
    – cfr
    Jul 20 at 13:19

1 Answer 1

1

If made a workaround with an \ifdim statement, which meets my requirements.

\documentclass[11pt, a4paper]{article}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\usepackage{blindtext}

\tcbset{breakable, width=.8\linewidth, tcbox width=auto limited, size=title, arc=2mm, boxrule=.5pt, boxsep=1mm}

\newlength{\mylen}

\newcommand{\coach}[1]{
    \def\content{#1}
    
    \settowidth{\mylen}{\content}
    \ifdim\mylen<.8\textwidth
        \tcbox[colback=yellow, sharp corners=northwest]{\content}
    \else 
        \begin{tcolorbox}[colback=red, sharp corners=northwest]\content\end{tcolorbox}
    \fi
}

\begin{document}

\blindtext[2]
\coach{Hello World}

\coach{\blindtext[2]}

\end{document}

You must log in to answer this question.

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