创建启动/结束另一个环境的 lstnewenvironment 时出现问题
我目前正在使用 Beamer 和列表包将代码漂亮地打印到 Beamer 块中。所以我正在做的事情看起来像:
\begin{block}{}
\begin{lstlisting}
int foobar(void) { return 0; }
\end{lstlisting}
\end{block}
现在,我发现每次启动 block
和 lstlisting
环境都很麻烦。我想要一个简单的 codeblock
环境来完成它:
\begin{codeblock}
int foobar(void) { return 0; }
\end{codeblock}
所以,我尝试了类似的方法:
\lstnewenvironment{codeblock}
{\begin{block}{}}
{\end{block}}
但不幸的是,Beamer 文档不再编译,并出现以下错误:
! Missing } inserted.
<inserted text>
}
l.178 \end{frame}
?
有什么方法可以做这 ?
在 Problem with create a newenvironment in LaTeX 中,Andreas Grech 也有同样的问题问题,但它可以解决它,因为有另一种方法可以进入/退出封闭环境。但在 block
Beamer 环境中,似乎除了 \begin{block}...\end{block}
之外别无他法。
I am currently using Beamer and the listing package to pretty-print code into Beamer blocks. So what I'm doing looks like :
\begin{block}{}
\begin{lstlisting}
int foobar(void) { return 0; }
\end{lstlisting}
\end{block}
Now, I find it cumbersome to start the block
and lstlisting
environments everytime. I'd like to have a simple codeblock
environment that just does it:
\begin{codeblock}
int foobar(void) { return 0; }
\end{codeblock}
So, I tried something like :
\lstnewenvironment{codeblock}
{\begin{block}{}}
{\end{block}}
But unfortunately, the Beamer document no longer compiles, with the following error:
! Missing } inserted.
<inserted text>
}
l.178 \end{frame}
?
Is there some way to do this ?
In Problem with creating a newenvironment in LaTeX, Andreas Grech had the same problem, but it could solve it since there was another way to enter/exit the enclosing environment. But in the case of the block
Beamer environment, it seems there is no other way than doing \begin{block}...\end{block}
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,但找不到解决方案。我的解决方法是使用 \lstinputlisting 命令并将代码放在单独的文件中。如果您有想要包含的真实代码,那就太好了。对于小例子则不然。
另一种解决方法是在启动 {frame} 环境之前将代码片段放入变量中,然后引用它。 Latex-beamer 文档中解释了如何执行此操作。它还允许您使用自定义环境/命令。
I had the same problem and could not find a solution for it. My workaround was to use the \lstinputlisting command and have the code in a separate file. That's great if you have real code you want to include. Not so for small examples.
Another workaround is to put the code snipplet into a variable before starting the {frame} environment and then reference it. How to do this is explained in latex-beamer docs. It would also allow you to employ your custom environment/command.
我通过使用
fancyvrb
包的\VerbatimOut
“解决”了这个问题(参见将环境变量逐字写入文件)以创建一个临时文件,然后可以将其包含在lstinputlisting
中:出于某种原因,我无法不过,将环境参数设置为可选。
像这样使用:
不是最佳解决方案,但我猜它有效。
I "solved" this by using the
fancyvrb
package's\VerbatimOut
(See write environmnet body verbatim to a file) to create a temporary file which then can be included withlstinputlisting
:For some reason i could not make the environment-argument optional, though.
Used like this:
Not the optimal solution, but it works, i guess.