如何将自定义的myframe的参数传递给frame?

发布于 2025-09-08 14:23:06

我希望创建一个frame级别的环境,也就是基于frame的自定义环境。它可以使用户比较方便地调节行距,而不是每次都调用\begin{spacing}{<arg>}...\end{spacing}之类的代码,同时它要接受一些参数,并将其传递给frame环境,考虑到frame环境是一个脆弱的环境,我的尝试总是失败。

\documentclass{beamer}
\usepackage{setspace}
%\newenvironment{myframe}[2][1.5]
%    {\begin{frame}[#2]\begin{spacing}{#1}}
%    {\end{spacing}\end{frame}}
\begin{document}
%    \begin{myframe}
%        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. 
%    \end{myframe}
    \begin{frame}
    \begin{spacing}{1.5}
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
    \end{spacing}
    \end{frame}
\end{document}

查看更多

关注者
0
被浏览
106
1 个回答
Sagittarius Rover
这家伙很懒,什么也没写!

先给一个workaround:

\documentclass{beamer}
\usepackage{lipsum}
\usepackage{setspace}
\newenvironment{myframe}[2][1.5]
   {%
        \begin{frame}[#2]
        \setstretch{#1}
   }%
   {%
        \end{frame}
   }%
\begin{document}
    \begin{frame}
        \lipsum[1][1-5]
    \end{frame}

    \begin{myframe}[3]{t}
        \lipsum[1][1-5]
    \end{myframe}

   \begin{myframe}[2]{c}
       \lipsum[1][1-5]
   \end{myframe}

    \begin{frame}
           \lipsum[1][1-5]
    \end{frame}
\end{document}

image.png


同时它要接受一些参数,并将其传递给frame环境,考虑到frame环境是一个脆弱的环境,我的尝试总是失败。

当提到「总是失败」时,最好贴一下报错信息,这样更容易让这个问题被别人搜索到,也更完整。下面看我的几个探索。

探索一:

你的问题和参数有关吗?并不是。下面的例子中,frame没问题;但myframe有问题。

\documentclass{beamer}
\usepackage{lipsum}
\usepackage{setspace}
\newenvironment{myframe}
   {%
        \begin{frame}
        \begin{spacing}{2}
   }%
   {%
        \end{spacing}
        \end{frame}
   }%
\begin{document}
    \begin{frame}
        \begin{spacing}{2}
        \lipsum[1][1-5]
        \end{spacing}
    \end{frame}

    \begin{myframe}
        \lipsum[1][1-5]
    \end{myframe}
\end{document}

报错信息:

! File ended while scanning use of \beamer@collect@@body.
<inserted text>
                \par
<*> mwe

?

探索二:

这个问题和beamer提供的frame环境有关吗?换成minipage看看(?)

\documentclass{beamer}
\usepackage{lipsum}
\usepackage{setspace}
\newenvironment{myframe}
   {%
        \begin{minipage}{\textwidth}
        \begin{spacing}{2}
   }%
   {%
        \end{spacing}
        \end{minipage}
   }%
\begin{document}
    \begin{minipage}{\textwidth}
        \begin{spacing}{2}
        \lipsum[1][1-5]
        \end{spacing}
    \end{minipage}

    \begin{myframe}
        \lipsum[1][1-5]
    \end{myframe}
\end{document}

image.png

并没有报错,所以这是"非常独特的",当且仅当同时使用frame内嵌spacing才会触发错误。

最后从这个链接来看,用\setstretch可以规避这一问题,所以有了workaround...

最后的最后,不要忘记问题并没有真正解决...


翻到一个神奇的结论:

由于beamer实在是太坏了。egreg说:

Moral of the story
Don't use setspace with beamer😀.

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览