在 LaTeX 中创建新环境时出现问题
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个特殊情况,因为列表环境需要提前解析自身以找到自身的结尾。原因是列表环境内的宏不得扩展——当然包括环境的结束标记。
因此,基本上,如果该行包含
\end{lstlisting}
,它会在每一行中查找 - 但在您的情况下,不存在这样的行,因为\end{javacode}
宏没有尚未得到扩展。因此列表会继续搜索,直到文件末尾。清单定义了一个自己的命令来解决这个问题。来自文档:
例如:
编辑响应您编辑的问题:我尝试编译以下最小的“工作”示例。实际上,它并没有那么多工作——
latex
处理器只是停在中间并等待用户输入。由于列表文档没有提及对
singlespace
的特殊处理,我认为您可能已经发现了一个错误。最好的做法可能是从列表包的维护者那里获得反馈。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:
For example:
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.经过进一步研究,我发现了这个 http://www.tug.org /pipermail/texhax/2009-June/012699.html
要解决我的解决方案,我需要使用
\singlespacing
而不是singlespace
环境。以下是我现在的工作代码:
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 thesinglespace
environment.The following is now my working code: