![具有可变尺寸和框架位置的 Beamer 框](https://linux22.com/image/344062/%E5%85%B7%E6%9C%89%E5%8F%AF%E5%8F%98%E5%B0%BA%E5%AF%B8%E5%92%8C%E6%A1%86%E6%9E%B6%E4%BD%8D%E7%BD%AE%E7%9A%84%20Beamer%20%E6%A1%86.png)
我想构建灵活的投影仪盒,以便可以更改框架内的宽度和对齐方式。传递选项width=
和alignment=
,其中宽度是有效值,对齐方式可以是左对齐、居中对齐和右对齐。
我有以下 MWE,但是它不起作用。
\documentclass{beamer}
\usepackage{xstring}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\processlist}{ m m }
{%
\clist_map_inline:nn { #1 } {%
\IfSubStr{##1}{#2}{%
\StrBehind{##1}{=}
}{}
}
}
\ExplSyntaxOff
\setbeamercolor{block title alerted}{fg=blue,bg=green}
\setbeamercolor{block body alerted}{bg=red}
\newenvironment<>{varalertblockA}[2][0.9\textwidth]{%
\setlength{\textwidth}{#1}
\begin{actionenv}#3%
\def\insertblocktitle{#2}%
\par%
\setbeamercolor{local structure}{parent=alerted text}%
\usebeamertemplate{block alerted begin}}
{\par%
\usebeamertemplate{block alerted end}%
\end{actionenv}}
\newcommand{\cvaralertblock}[3][0.9\textwidth]{%
\hfill{}\vbox{%
\begin{varalertblockA}[#1]{#2}%
#3%
\end{varalertblockA}%
}\hfill{}%
}
\newenvironment<>{varalertblockB}[2][]{%
\ifnum\pdfstrcmp{\processlist{#1}{align}}{center}=0%
\hfill{}%
\else%
\fi%
\setbox0=\hbox{\processlist{#1}{width}}\ifdim\wd0=0pt%
\else%
\setlength{\textwidth}{\processlist{#1}{width}}%
\fi%
\hfill{}%
\begin{actionenv}#3%
\def\insertblocktitle{#2}%
\par%
\setbeamercolor{local structure}{parent=alerted text}%
\usebeamertemplate{block alerted begin}}
{\par%
\usebeamertemplate{block alerted end}%
\end{actionenv}
\ifnum\pdfstrcmp{\processlist{#1}{align}}{center}=0%
\hfill{}%
\else%
\fi%
}
\begin{document}
%\processlist{align=right,a,b,c, d z}{align}
% No working
\begin{varalertblockB}[width=2cm,align=center]{Test}
Test
\end{varalertblockB}{Test}
\cvaralertblock[0.5\textwidth]{Test}{Test}
\begin{varalertblockA}{Test}
Test
\end{varalertblockA}
\end{document}
答案1
为什么要重新发明轮子?tcolorbox
已经提供了所有这些功能!
\documentclass{beamer}
\usepackage[most]{tcolorbox}
\newtcolorbox{myblock}[2][]{%
beamer,breakable,colback=blue!10!white,colframe=blue!50!black,#1,title=#2
}%
\begin{document}
\begin{frame}

\begin{myblock}{test}
bla
\end{myblock}
\begin{myblock}[width=3cm]{test}
bla
\end{myblock}
\begin{myblock}[halign=center]{test}
bla
\end{myblock}
\end{frame}
\end{document}
使用 tcolorbox 内部主题您可以保留正常的block
环境语法:
\documentclass{beamer}
\usetheme{Warsaw}
\useinnertheme{tcolorbox}
\begin{document}
\begin{frame}
\begin{block}{test}
bla
\end{block}
{
\tcbset{width=4cm}
\begin{block}{test}
bla
\end{block}
}
{
\tcbset{halign=center}
\begin{block}{test}
bla
\end{block}
}
\end{frame}
\end{document}