在 LaTeX 中创建新环境时出现问题

发布于 2024-08-27 21:34:29 字数 952 浏览 7 评论 0原文

我正在尝试在 LaTeX 中实现这个新环境:

\newenvironment{javacode}[2]
{\begin{lstlisting}[language=java, label=#1, caption=#2]}
{\end{lstlisting}}

然后像这样使用它:

\begin{javacode}{c}{some code}
int x = 5;
\end{javacode}

但是我收到以下错误:

Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][] 
[1] [2]) [3])
*

任何人都可以帮助解决这个问题吗?

[更新]

我尝试这样做 红鼻子独角兽指示,并且工作正常。

但现在我尝试添加 \begin{singlespace} 像这样:

\lstnewenvironment{javacode}[2]
{
\begin{singlespace}
\lstset{language=java, label=#1, caption=#2}}
{
\end{singlespace}
}

我得到了同样的错误:

Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][] 
[1]) [2] [3])
*

I am trying to implement this new environment in LaTeX:

\newenvironment{javacode}[2]
{\begin{lstlisting}[language=java, label=#1, caption=#2]}
{\end{lstlisting}}

And then use it like such:

\begin{javacode}{c}{some code}
int x = 5;
\end{javacode}

But I am getting the following error:

Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][] 
[1] [2]) [3])
*

Can anyone help as regards fixing this problem?

[Update]

I tried it doing it like Red-nosed unicorn instructed, and it worked correctly.

But now I tried adding a \begin{singlespace} like such:

\lstnewenvironment{javacode}[2]
{
\begin{singlespace}
\lstset{language=java, label=#1, caption=#2}}
{
\end{singlespace}
}

And I got the same error:

Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6
[][][][][][][] 
[1]) [2] [3])
*

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

皇甫轩 2024-09-03 21:34:29

这是一个特殊情况,因为列表环境需要提前解析自身以找到自身的结尾。原因是列表环境内的宏不得扩展——当然包括环境的结束标记。

因此,基本上,如果该行包含 \end{lstlisting} ,它会在每一行中查找 - 但在您的情况下,不存在这样的行,因为 \end{javacode} 宏没有尚未得到扩展。因此列表会继续搜索,直到文件末尾。

清单定义了一个自己的命令来解决这个问题。来自文档:

\lstnewenvironment 
  {⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
  {⟨starting code⟩}
  {⟨ending code⟩}

例如:

\lstnewenvironment{javacode}[2]
  {\lstset{language=java, label=#1, caption=#2}}
  {}

编辑响应您编辑的问题:我尝试编译以下最小的“工作”示例。实际上,它并没有那么多工作——latex 处理器只是停在中间并等待用户输入。

由于列表文档没有提及对 singlespace 的特殊处理,我认为您可能已经发现了一个错误。最好的做法可能是从列表包的维护者那里获得反馈。

% mini.dvi
\documentclass{article}

\usepackage{listings}
\usepackage{setspace}
\doublespacing

\lstnewenvironment{javacode}
 {\begin{singlespace}
  \lstset{language=java}}
 {\end{singlespace}}

\begin{document}
\begin{javacode}
int a = 1;
int b = 2;
\end{javacode}
\end{document}

This is a special case because the listings environment needs to parse ahead itself to find the end of itself. The reason is that macros inside the listings environment must not get expanded – that of course includes the end tag of the environment.

So basically it looks in each line if the line contains \end{lstlisting} – but in your case, no such line exists since the \end{javacode} macro has not yet been expanded. So listings continues to search until the end of the file.

Listings defines an own command to work around this. From the documentation:

\lstnewenvironment 
  {⟨name⟩}[⟨number⟩][⟨opt. default arg.⟩]
  {⟨starting code⟩}
  {⟨ending code⟩}

For example:

\lstnewenvironment{javacode}[2]
  {\lstset{language=java, label=#1, caption=#2}}
  {}

EDIT In response to your edited question: I tried to compile the following minimal “working” example. Actually, it’s not so much working – the latex processor just stops right in the middle and waits for a user input.

Since the listings documentation makes no mention of a special treatment of singlespace, I think you may have uncovered a bug. The best course of action is probably to get feedback from the maintainer of the listings package.

% mini.dvi
\documentclass{article}

\usepackage{listings}
\usepackage{setspace}
\doublespacing

\lstnewenvironment{javacode}
 {\begin{singlespace}
  \lstset{language=java}}
 {\end{singlespace}}

\begin{document}
\begin{javacode}
int a = 1;
int b = 2;
\end{javacode}
\end{document}
泡沫很甜 2024-09-03 21:34:29

经过进一步研究,我发现了这个 http://www.tug.org /pipermail/texhax/2009-June/012699.html

要解决我的解决方案,我需要使用 \singlespacing 而不是 singlespace 环境。

以下是我现在的工作代码:

\lstnewenvironment{javacode}[2]
{\singlespacing\lstset{language=java, label=#1, caption=#2}}
{}

Upon further research, I found this http://www.tug.org/pipermail/texhax/2009-June/012699.html

To workaround my solution, I need to use \singlespacing instead of the singlespace environment.

The following is now my working code:

\lstnewenvironment{javacode}[2]
{\singlespacing\lstset{language=java, label=#1, caption=#2}}
{}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文