我正在尝试使用 在文档中实现一种聊天功能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}