help in finding error using \newenvironment

I am learning \newenvironment and I need help in finding where my error is in making \newenvironment. I get a Latex error and I do not see what I am doing wrong. I am trying to reduce code duplication by making new environment. I will show the code before, and then the code when I try to make an environment.

Here is before, which works with no error:

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes
\begin{document}

\begin{tcblisting}{colback=bg,size=minimal,hbox,listing only,
listing options={style=Matlab-editor,
basicstyle=\ttfamily\normalsize,
breaklines         = false,
escapechar         = 
}%
}%
K=[3 -2;-2 2]; M=[1 0;0 3];
[phi,lam]=eig(K,M)
\end{tcblisting}
\end{document}


Compiled with lualatex and it gives

Here is the new code, which tries to make \newenvironment called matlab with same exact definition as above.

\documentclass[12pt]{book}%
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes

\newenvironment{matlab}
{
\begin{tcblisting}{colback=bg,size=minimal,hbox,listing only,
listing options={style=Matlab-editor,
basicstyle=\ttfamily\normalsize,
breaklines         = false,
escapechar         =
}%
}%
}%
{
\end{tcblisting}\ignorespacesafterend
}%

\begin{document}

\begin{matlab}
K=[3 -2;-2 2]; M=[1 0;0 3];
[phi,lam]=eig(K,M)
\end{matlab}
\end{document}


When I compile the above, I get

lualatex foo2.tex
This is LuaTeX, Version beta-0.80.0.1 (TeX Live 2015) (rev 5863)
restricted \write18 enabled.
(./foo2.tex
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/book.cls
Document Class: book 2014/09/29 v1.4h Standard LaTeX document class
...
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/etexcmds.sty)))
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/epstopdf-sys.cfg)))
Runaway argument?
! File ended while scanning use of \next.
<inserted text>
\par
<*> foo2.tex

?


I looked up the definition of \newenvironment and I do not see what mistake I am doing. Definition

    \newenvironment{nam}[args]{begdef}{enddef}


Using TL 2015

• Isn't that just another verbatim environment? Like lstlisting, align and others? – Johannes_B Jun 9 '16 at 5:25
• @Johannes_B I do not know. Is there a limitation in making newenvironments? I keep forgetting if there is. So many rules to keep track of when programming Latex. Is there a way to go around it then so I do not have to duplicate same code each time? – Nasser Jun 9 '16 at 5:32
• It is the very same limitation as with align and lstlisting. The rule is pretty simple: Some environments are impossible to create. – Johannes_B Jun 9 '16 at 5:35
• And to be honest, i pretty much think that LaTeX is the wrong input tool for you. People are doing some great stuff based on xml to create different output formats. – Johannes_B Jun 9 '16 at 5:36
• but how to do math in XML? Any way, I simply wanted to make a "function" like, which I understand is what newenvironment is supposed to be, so that I can "call" it each time, instead of repeating the same boiler plate code each time. – Nasser Jun 9 '16 at 5:42

You can't use \begin{tcblisting} inside the argument to another command, just like \begin{verbatim} or \begin{lstlisting}.

The listings package has \lstnewenvironment for coping with the case of defining new environments based on lstlisting and, of course, also the tcolorbox package has the appropriate feature, called \newtcblisting (see page 290 in the manual, section 15.2).

\documentclass[12pt]{book}
\usepackage[T1]{fontenc}
\usepackage{color}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}
\usepackage{matlab-prettifier}
\definecolor{bg}{RGB}{240,240,240}
\usepackage{upquote} %to fix string quotes

\newtcblisting{matlab}{
colback=bg,
size=minimal,
hbox,
listing only,
listing options={
style=Matlab-editor,
basicstyle=\ttfamily\normalsize,
breaklines= false,
escapechar=,
},
}

\begin{document}

\begin{matlab}
K=[3 -2;-2 2]; M=[1 0;0 3];
[phi,lam]=eig(K,M)
\end{matlab}
\end{document}


verbatim environments like listings can not be nested in definitions for the simple reason they disable parsing, after \begin{matlab} you start verbatim listing mode so \ does not have its normal meaning so that it can appear verbatim in code listings. so \end{matlab} is not the instruction to end an environment it is just the sequence of characters to be typeset.

Obviously the code has to have some way of ending so it looks for the particular string \end{tcblisting}`.

Many verbatim packages give ways of defining environments that have this behaviour (the way tcblisting is based on lstlisting) but you need to check the package documentation.