# newenvironment for tcolorbox and lstlisting

MWE:

\documentclass{article}
\usepackage{listings, tcolorbox}
\tcbuselibrary{listings,skins,theorems,xparse}

\newenvironment{mybox}
{\begin{tcolorbox}\begin{lstlisting}}
{\end{lstlisting}\end{tcolorbox}}

\begin{document}

\begin{tcolorbox}\begin{lstlisting}
This is my \LaTeX\ box.
\end{lstlisting}\end{tcolorbox}

% \begin{mybox}
% This is my \LaTeX\ box too!
% \end{mybox}

\end{document}


My environment doesn't work. Maybe I need a primer on \newenvironment?

• Use \tcbuselibrary{listings} and \newtcblisting instead. This gets around the problems with the way listings looks for the end of the environment. – Andrew Swann Apr 26 '18 at 12:54
• The issue is explained e.g. in tex.stackexchange.com/a/86765/82917. A solution (to a rather similar question, though not en exact duplicate) is given in tex.stackexchange.com/a/132358/82917. – campa Apr 26 '18 at 13:21

Here is an example of the tcolorbox manual section 4.13 p 302:

\documentclass{article}
\usepackage[most]{tcolorbox}

\newtcblisting{mybox}[2][]{%
colback=red!5!white,
colframe=red!75!black,
fonttitle=\bfseries,
title=#2,#1}

\begin{document}

\begin{mybox}[listing only]{Listing Box}
This is my \LaTeX\ box.
\end{mybox}
\bigskip

\begin{mybox}[listing side text]{Listing Box}
This is my \LaTeX\ box.
\end{mybox}

\end{document}


Output: