0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.