使用 varwidth 和 tikz 定义一个 newenvironment,以获取宽度等于其中内容的块。除非涉及分项,否则它可以正常工作。这里给出了 MWE。
\documentclass[]{beamer}
\usepackage{tikz}
\usepackage{varwidth}
\tikzstyle{example-box} = [fill=yellow, rectangle]
\newenvironment{autowidthblock}{
\begin{tikzpicture}
\node [example-box] (box)
\bgroup
\begin{varwidth}{\linewidth}
}{
\end{varwidth}
\egroup;
\end{tikzpicture}
}
\begin{document}
\begin{frame}
\begin{autowidthblock}
With normal sentences, the width is rightly fit
\end{autowidthblock}
\begin{autowidthblock}
\begin{itemize}
\item With itemize environment,
\item The width is not auto-set
\item Instead spans the entire textwidth
\end{itemize}
\end{autowidthblock}
\end{frame}
\end{document}