简体   繁体   中英

Newcommand hangs LaTeX compilation

I have a problem in the definition of a new command in LaTeX for lstlisting . The result is that the compilation hangs without error message, just a * .

This hangs the LaTeX compilation:

\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. The problem occurs for lstlisting , but not for \\begin{center} , for example.

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

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.

However, the same result can be obtained defining a new lst environment, using, for instance:

 \\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}).

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 ! }}{} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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