1

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.

zeeMonkeez
  • 5,057
  • 3
  • 33
  • 56
leandro
  • 175
  • 8

1 Answers1

0

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

Community
  • 1
  • 1
leandro
  • 175
  • 8