1

I´m using the Matlab-prettifier package to include Matlab-code in my Latex-file, and it generally works, i.e, when I´m using the custom environment from the package:

\begin{lstlisting}[style=Matlab-editor]
format long
f=@(x) x^5 + x-1;
fzero(f, 1), hm(f, 0, 1, 0.5*10^-8)
ans =
   0.754877666246693

ans =
   0.754877667874098
\end{lstlisting}

So this works, but since this is an environment I use a lot, I would like to rename it to something shorter, and I´ve tried this:

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

That way I could just write

\begin{matlab}
Matlab-code here
\end{matlab}

to typeset the Matlab-code, but this doesn´t work, and I get a long and -- at least for me - incomprehensible error message, with a lot about undefined control sequences. Can anybody help me out with this? Thanks in advance!

Best answer (for me): enclosing a lstlisting environment inside my own environment using matlab-prettifier

marked as duplicate by Troy, TeXnician, Mensch, Schweinebacke, Stefan Pinnow Oct 29 '17 at 9:39

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    do you really have blank lines in \newenvironment{matlab} definition? – user4686 Sep 17 '17 at 12:16
  • 3
    Use \VerbatimEnvironment, for example, see here: tex.stackexchange.com/questions/171702/… – CarLaTeX Sep 17 '17 at 12:17
  • @jfbu No, I don´t have a blank line in the definition, I just wanted each argument to be on a new line. Thanks for the edit! – Niels Petter Liset Sep 17 '17 at 12:24
  • 3
    If you're using listings, see section 5.14 of the package documentation for the \lstnewenvironment macro. You're running into a problem similar to the verbatim environment answer above, but the listings package has already solved it. – Mike Renfro Sep 17 '17 at 12:41
  • You can use the {} icon to convert a region into indented format making it rendered as code. I will suppress the blank lines. – user4686 Sep 17 '17 at 12:46