0

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.

siracusa
  • 13,411
Alex
  • 3
  • 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 05 '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 05 '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 05 '18 at 20:45
  • 1
    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 05 '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 05 '18 at 21:13
  • @Tobi spellcheck for verbatim environments is controlled by cwl as well. The custom environment setting is only for syntax highlighting. – Troy Aug 05 '18 at 21:38
  • @Troy: Ah. Thanks for the information. Maybe you like to make an answer and explain how to write the CWL, where to save it and how to use/activate it in TeXstudio. – Tobi Aug 06 '18 at 08:23
  • @Alex Are there any updates here? Can you confirm whether I've understood your question correctly, or not? And whether my suggestion works for you, or not? – Troy Aug 14 '18 at 03:43
  • @Troy Thanks, yes you understood my question correctly and you provided me the right solution. I created a .cwl file with \begin{mycode}#V in 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
  • @Alex Thanks for confirming, I was reluctant to write up an answer if I misunderstood your question. :) I'll write one up now. – Troy Aug 14 '18 at 20:05

1 Answers1

0

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).

Put this inside your cwl file:

\begin{mycode}#V
\end{mycode}

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.

Troy
  • 13,741