简体   繁体   English

Newcommand挂断LaTeX编译

[英]Newcommand hangs LaTeX compilation

I have a problem in the definition of a new command in LaTeX for lstlisting . 我在LaTeX中为lstlisting定义新命令时遇到问题。 The result is that the compilation hangs without error message, just a * . 结果是编译挂起而没有错误消息,只是一个*

This hangs the LaTeX compilation: 这挂起了LaTeX编译:

\documentclass[10pt,a4paper,ssfamily]{exam}
\usepackage{listings}
\newcommand{\cs}{\begin{lstlisting}}
\newcommand{\ce}{\end{lstlisting}}
\begin{document}
\cs
program test
  ! This is a commentary
end program test
\ce
\end{document}

But this one does not: 但是这个没有:

\documentclass[10pt,a4paper,ssfamily]{exam}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
program test
  ! This is a commentary
end program test
\end{lstlisting}
\end{document}

The only difference is that the beginning and end of endmargin and of lstlisting are defined in a new command in the first place. 唯一的区别在于,开始和endmargin的和的端lstlisting在一个新的命令,在第一个地方所定义。 The problem occurs for lstlisting , but not for \\begin{center} , for example. 例如,对于lstlisting会发生此问题,而对于\\begin{center}不会发生此问题。

Well, I found a similar problem, which was reported here: 好吧,我发现了一个类似的问题,在这里报告:

https://tex.stackexchange.com/questions/25597/wrapping-code-listings-verbatim-or-other-method-inside-a-newcommand https://tex.stackexchange.com/questions/25597/wrapping-code-listings-verbatim-or-other-method-inside-a-newcommand

It seems that it is not possible to do what I want because the compilation of \\begin{lstlisting} needs to search for the corresponding \\end{lstlisting} and the newcommands break that. 似乎不可能做我想要的事情,因为\\ begin {lstlisting}的编译需要搜索相应的\\ end {lstlisting}并且new命令会破坏它。

However, the same result can be obtained defining a new lst environment, using, for instance: 但是,定义新的lst环境可以获得相同的结果,例如:

 \\lstnewenvironment{code}{\\lstset{language=[90]Fortran, xleftmargin=1.5cm }}{} 

for example. 例如。 Then, it is possible to define many properties of the code section and the syntax is easier (\\begin{code},\\end{code}). 然后,可以定义代码段的许多属性,并且语法更容易(\\ begin {code},\\ end {code})。

The full lstnewenvironment, in my case, is: 在我的例子中,完整的新环境是:

 \\lstnewenvironment{code}{\\lstset{language=[90]Fortran, basicstyle=\\ttfamily, keywordstyle=\\color{blue}, commentstyle=\\color{gray}, xleftmargin=1.5cm, morecomment=[l]{!\\ }% Comment only with space after ! }}{} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM