1

I've just started using matlab-prettifier. Since the line \begin{lstlisting}[style=Matlab-editor] is a lot to type, I created my own environment:

\newenvironment{myMP}
     {\begin{samepage}\begin{lstlisting}[style=Matlab-editor]}
     {\end{lstlisting}\end{samepage}}

I've used this syntax many times before, but in this case it doesn't work. Is there any reason why I can't enclose the lstlisting inside my own one?

Here's sample code, illustrating the problem.

\documentclass{minimal}
\usepackage[framed]{matlab-prettifier}%For matlab code
\newenvironment{myMP}
     {\begin{samepage} \begin{lstlisting}[style=Matlab-editor]}
     {\end{lstlisting} \end{samepage}}
\begin{document}
\begin{myMP}
matlab word
\end{myMP}
\end{document}

When I run this with pdflatex it returns a *, indicating that the environment wasn't closed properly.

Thanks!

1

1 Answer 1

2

You can't embed a verbatim - like environment like lstlisting inside another environment, at least not without much tricks (like \scantokens etc.)

listings provides the facility to generate own, customizable environments with lstnewenvironment. Use \lstset{style=Matlab-editor} to provide the relevant settings in the start-up-code section of the environment.

\documentclass{article}

\usepackage[framed]{matlab-prettifier}%For matlab code



\lstnewenvironment{myMP}{%
  \lstset{style={Matlab-editor}%
  }
}{% End code -- empty here
}

\begin{document}
\begin{myMP}
  a = 1
  matlab word
\end{myMP}
\end{document}

enter image description here

2
  • This works great, but I need to embed it in a conditional, i.e., I'm using the environment in an answer key. So I want to do an \ifthen construction, of the form \ifthenelse{\theanswerkey=1}{ \begin{myMP} a = 1 \end{myMP}} {} where answerkey is a counter defined previously. This construction runs into the usual problems associated with verbatim-like environments. Is there some way that I can do this? Thanks!
    – Leo Simon
    Apr 18, 2016 at 15:11
  • @LeoSimon: That's another question and at the moment not answerable without seeing a real example
    – user31729
    Apr 18, 2016 at 15:33

You must log in to answer this question.

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