AucTeX issue with BVerbatim in newenvironment

I have the following newenvironment command:

\newenvironment{code}
{ \VerbatimEnvironment
\begin{mdframed}[
backgroundcolor=black!70,
leftmargin=1, rightmargin=1,
innerleftmargin=4, innertopmargin=4, innerbottommargin=4,
leftline=false, topline=false, rightline=false,·bottomline=false
]\color{white}\footnotesize\begin{BVerbatim}[commandchars=\\\{\}] }
{ \end{BVerbatim} \end{mdframed} }


After this, the syntax coloration in my emacs is completely broken, everything is as if I was in a verbatim environment (yellowish color). The only way to "fix" the coloration is by doing:

\end{ BVerbatim}


...but obviously this makes the code not compilable.

Is there a way to fix this correctly?

AUCTeX's syntax highlighting system expects the \begin{verbatim-env-name} in a line of its own. You can fix your issue easily by slightly rearranging your code:

\newenvironment{code}{%
\VerbatimEnvironment
\begin{mdframed}[
backgroundcolor   = black!70 ,
leftmargin        = 1        ,
rightmargin       = 1        ,
innerleftmargin   = 4        ,
innertopmargin    = 4        ,
innerbottommargin = 4        ,
leftline          = false    ,
topline           = false    ,
rightline         = false    ,
bottomline        = false ]
\color{white}
%%% Relevant rearrangement here:
\begin{BVerbatim}[commandchars=\\\{\}]
}{
\end{BVerbatim}%
\end{mdframed}%
}


It looks like this for me:

Note the color of }{.

As a note, I think you're better off if you use the facilities described in the manual of fancyvrb and mdframed. Code:

\documentclass[a4paper,draft]{article}

\usepackage[T1]{fontenc}
\usepackage{lmodern,fancyvrb}

\DefineVerbatimEnvironment{code}{BVerbatim}{%
commandchars = \\\{\}        ,
formatcom    = \color{white} ,
fontsize     = \footnotesize ,
}

\usepackage{mdframed,xcolor}
\surroundwithmdframed[%
backgroundcolor   = black!70 ,
leftmargin        = 1        ,
rightmargin       = 1        ,
innerleftmargin   = 4        ,
innertopmargin    = 4        ,
innerbottommargin = 4        ,
leftline          = false    ,
topline           = false    ,
rightline         = false    ,
bottomline        = false
]{code}

\begin{document}

\begin{code}
{\color{cyan}\textbackslash{}surroundwithmdframed[%}
backgroundcolor   = black!70 ,
\textcolor{red}{leftmargin        = 1        ,}
bottomline        = false
\fcolorbox{green}{black}{]{code}}
\end{code}

\end{document}


The result looks like this:

AUCTeX is able to parse this version and updates its internal variables in order to be aware of this new verbatim environment.

• Re-arranging the environment did fix the issue. I tried the version with DefineVerbatimEnvironment but I faced two issues: 1) I don't know how to set the font size to footnotesize in this case (I forgot to put this in my MCVE), 2) the \textcolor{} commands I use inside the code environment do not work anymore due to the formatcom parameter. – Holt Nov 8 '18 at 14:11
• @Holt - See my updated answer. – Arash Esbati Nov 9 '18 at 12:22

Try this:

\newenvironment{code}
{ \VerbatimEnvironment
\begin{mdframed}[
backgroundcolor=black!70,
leftmargin=1, rightmargin=1,
innerleftmargin=4, innertopmargin=4, innerbottommargin=4,
leftline=false, topline=false, rightline=false,·bottomline=false
]\color{white}%
\begin{BVerbatim}[commandchars=\\\{\}] }
{ \end{BVerbatim}
\end{mdframed} }


(I obeyed the various space tokens in OP)

• ah, Arash answer was posted while I was completing mine... – user4686 Nov 7 '18 at 21:23