创建启动/结束另一个环境的 lstnewenvironment 时出现问题

发布于 2024-10-20 06:38:11 字数 964 浏览 3 评论 0原文

我目前正在使用 Beamer 和列表包将代码漂亮地打印到 Beamer 块中。所以我正在做的事情看起来像:

\begin{block}{}
\begin{lstlisting}
int foobar(void) { return 0; }
\end{lstlisting}
\end{block}

现在,我发现每次启动 blocklstlisting 环境都很麻烦。我想要一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤君无依 2024-10-27 06:38:11

我遇到了同样的问题,但找不到解决方案。我的解决方法是使用 \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.

另类 2024-10-27 06:38:11

我通过使用 fancyvrb 包的 \VerbatimOut“解决”了这个问题(参见将环境变量逐字写入文件)以创建一个临时文件,然后可以将其包含在 lstinputlisting 中:

\usepackage{fancyvrb}
\usepackage{listings}

\newenvironment{blocklisting}[1]
{\begingroup\lstset{#1}\VerbatimOut{blocklisting-tmp.txt}}
{\endVerbatimOut\begin{block}{Code}\lstinputlisting{blocklisting-tmp.txt}\end{block}\endgroup}

出于某种原因,我无法不过,将环境参数设置为可选。

像这样使用:

\begin{document}
\begin{frame}[fragile]
\frametitle{Whatever}
\begin{blocklisting}{language=Java, basicstyle=\Huge}
Code
\end{blocklisting}

\begin{blocklisting}{}
Code 2
\end{blocklisting}
\end{frame}
\end{document}

不是最佳解决方案,但我猜它有效。

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 with lstinputlisting:

\usepackage{fancyvrb}
\usepackage{listings}

\newenvironment{blocklisting}[1]
{\begingroup\lstset{#1}\VerbatimOut{blocklisting-tmp.txt}}
{\endVerbatimOut\begin{block}{Code}\lstinputlisting{blocklisting-tmp.txt}\end{block}\endgroup}

For some reason i could not make the environment-argument optional, though.

Used like this:

\begin{document}
\begin{frame}[fragile]
\frametitle{Whatever}
\begin{blocklisting}{language=Java, basicstyle=\Huge}
Code
\end{blocklisting}

\begin{blocklisting}{}
Code 2
\end{blocklisting}
\end{frame}
\end{document}

Not the optimal solution, but it works, i guess.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文