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}}
\AfterEndEnvironment{mycode}{\end{minipage}\par\addvspace{\topskip}}
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}}
\AfterEndEnvironment{mycode}{\end{minipage}\par\addvspace{\topskip}}
\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.
mycode? Or is the TeX run failing (without your\newcommand\commentstuff)? If the latter, can you please provide a complete minimal working example (MWE) that starts at\documentclassand ends at\end{document}including everything necessary to reproduce your issue but nothing more? – Skillmon Aug 05 '18 at 20:23mycodeit does compile and show it, but in the editor it is marked and it tells me the command is not known. When usinglstlistingthe content in it gets green and command are ignored. So yes your first assumption is right – Alex Aug 05 '18 at 20:43.cwlfile with\begin{mycode}#Vin it. If you don't know how to use.cwlfiles, see a related answer of mine. – Troy Aug 05 '18 at 21:06{verbatim}is hard coded and not generally applied to all verbatim like environments. – Tobi Aug 05 '18 at 21:13.cwlfile with\begin{mycode}#Vin it and added it in settings under completion. While @Tobi 's Answer also worked for disabling spell checking (and coloring it green like in lstlisting) your solution also disabled highlighting of latex commands and this way is even better than lstlisting which does this not. @Troy do you want to make an answer that I can accept or should I do this myself? – Alex Aug 14 '18 at 20:03