2

Is there a way to define a new listings environment with some listings options by default?

For example, I am hoping to define a new environment, say myenv, such that

\begin{myenv}
\end{myenv}

and

\begin{lstlisting}[language = Java , frame = trBL , escapeinside={(*@}{@*)}, escapechar=\%]
\end{lstlisting}

are the same

Is there a way to achieve this?

1

1 Answer 1

2

Defining new verbatim environments is different from defining normal environment. listings already provides \lstnewenvironment for that

\documentclass{article}
\usepackage{listings}
\lstnewenvironment{myenv}
    {\lstset{language = Java , frame = trBL , escapeinside={(*@}{@*)}, escapechar=\%}}
    {}
\begin{document}
\begin{myenv}
/* HelloWorld.java
 */

public class HelloWorld
{
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

\end{myenv}
\end{document}

enter image description here

Hello world Java code taken from here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .