![tcolorbox:动态宽度且可破坏](https://linux22.com/image/467162/tcolorbox%EF%BC%9A%E5%8A%A8%E6%80%81%E5%AE%BD%E5%BA%A6%E4%B8%94%E5%8F%AF%E7%A0%B4%E5%9D%8F.png)
我正在尝试使用 在文档中实现一种聊天功能tcolorbox
。但我可以动态设置宽度或允许在“聊天消息”内分页。
\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}
有人知道如何满足这两个要求吗?
先感谢您。
答案1
如果用一个声明来解决问题\ifdim
,那就满足我的要求了。
\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}