# Commands inside lstnewenvironment not ignored

I am using an lstenvironment to make sure the code is on one page:

\lstnewenvironment{mycode}{}{}

\usepackage{etoolbox}
\BeforeBeginEnvironment{mycode}{\par\noindent\begin{minipage}{\linewidth}\lstset{style=sharpc}}


this works but the problem I have when using this is, that the content between \begin{mycode} and \end{mycode} is not treated like a comment (so commands are not ignored, and spell checking is applied). I would like to have the same behavior like when using lstlisting directly.

I tried using the \comment command of verbatim and comment packages like this:

\newenvironment{mycode}{\comment}{\endcomment}


and tried using it inside \BeforeBeginEnvironment and \AfterEndEnvironment based on some answers I found on related questions (that did not solve my problem).

But it tells me the commands are not known. but when I use \newcommand{\comment}[1]{} and same for \endcomment it tells me they are already defined. Furthermore I got also errors when I tried using begin{comment} and \end{comment} there.

So does someone know what I am doing wrong or how to get the same behavior like using lstlisting directly?

(Btw. I am using TexStudio 2.12.10 if this is relevant)

EDIT as requested here is my minimal working code example:

\documentclass[12pt,oneside]{report}

\usepackage[svgnames,table,hyperref]{xcolor}
\definecolor{othgray}{RGB}{243, 241, 239}
\usepackage{listings}           % Darstellung von Quellcode
\lstdefinestyle{sharpc}{language=[Sharp]C, basicstyle=\sffamily, keywordstyle={\color{Navy}\bfseries}, commentstyle={\color{DarkGreen}\slshape}, stringstyle={\color{DarkOrange}}, backgroundcolor={\color{othgray}}, numbers=left, numberstyle=\tiny, tabsize=2}

\lstnewenvironment{mycode}[1][]{}{}

\usepackage{etoolbox}
\BeforeBeginEnvironment{mycode}{\par\noindent\begin{minipage}{\linewidth}\lstset{style=sharpc}}

\begin{document}

\chapter{Code}

\begin{mycode}
Excel.Range usedRange = workbook.UsedRange;
Excel.Range firstCell = usedRange[1, 1];
\end{mycode}

\end{document}


EDIT 2 Seems to be just an editor problem, because it compiles and shows it right when using a nonsense command inside mycode without using any comment command. But the editor highlights the command and tells me it is unkown before compiling.

• Welcome to TeX.SX! Do I understand correctly, that this is an editor issue? So you want your editor not to highlight unknown commands as such and don't apply spellchecking inside of mycode? Or is the TeX run failing (without your \newcommand\comment stuff)? If the latter, can you please provide a complete minimal working example (MWE) that starts at \documentclass and ends at \end{document} including everything necessary to reproduce your issue but nothing more? – Skillmon Aug 5 '18 at 20:23
• updated with MWE. Tested again, seems to be only a editor problem, because if I put a nonsense command inside mycode it does compile and show it, but in the editor it is marked and it tells me the command is not known. When using lstlistingthe content in it gets green and command are ignored. So yes your first assumption is right – Alex Aug 5 '18 at 20:43
• Unfortunately I don't know my way around TeXstudio and can't really help you with that (but chances are that in the next hours there is someone who can). You should reword your question though to make it clear that it is only about the syntax highlighting of TeXstudio in this case. – Skillmon Aug 5 '18 at 20:45
• If I understand your question correctly, you just need a custom .cwl file with \begin{mycode}#V in it. If you don't know how to use .cwl files, see a related answer of mine. – Troy Aug 5 '18 at 21:06
• You can also add the environment as a custom environment in the preferencesown highlighting (Eigene Hervorhebung in my german version). However it’ll still try to spell check that. Seems like the disabled spellcheck for {verbatim} is hard coded and not generally applied to all verbatim like environments. – Tobi Aug 5 '18 at 21:13

Syntax highlighting in TeXstudio is controlled by .cwl files. With the creation of a new (verbatim) environment called mycode, the way to let TXS know about this is to do so with a user-created .cwl file (it's just a text file).
\begin{mycode}#V

where the #V classifier declares a verbatim-like environment (thus all syntax checking turned off, just like in \lstlisting).
For info on how to use .cwl files, see my previous answers on the topic.