6

Isodat language is used for Mass-spectrometers from ThermoFisher Scientific®, which is used for Stable Isotope Mass-spectrometry.

Parts of the language's syntax highlighting has been explored in listings package: formating all numbers in language definition? and the follow-up question listing package: colored numbers, but not colored in variable names.

In the end it should look like the image below:

Example of Isodat source code.

I get the following:

.png of .pdf from .tex.

Do you know what needs to be changed here? The problems are:
1. numbers within comment,
2. numbers within string,

So far the following have been fixed:
3. keywords in blue (if, else, string, number),
4. parenthesis following keywords in black,
5. block comments,
6. keywords in lightblue.
7. strings in blue

\documentclass[fleqn, a4paper, landscape]{article}
\usepackage{xcolor}

\definecolor{isored}{rgb}{0.6,0,0} % for strings
\definecolor{isogreen}{rgb}{0.25,0.6,0.2} % comments
\definecolor{isoblue}{rgb}{0.0,0.0,1} % keyword
\definecolor{isolightblue}{rgb}{0.5,0.5,1.0} % keywords
\definecolor{isocomgreen}{rgb}{0.25,0.35,0.75} % javadoc

\usepackage{etoolbox}

\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string
% https://tex.stackexchange.com/questions/34896/coloring-digits-with-the-listings-package?rq=1
\newcommand*{\ColorIfNotInString}[1]{\iftoggle{InString}{#1}{\textcolor{isored}#1}}%
\newcommand*{\ProcessQuote}[1]{#1\iftoggle{InString}{\global\togglefalse{InString}}{\global\toggletrue{InString}}}%

\usepackage{listings} 

\lstdefinelanguage{isodat}
{%
  basicstyle=\ttfamily,%
  literate=%
     %{"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
     {0}{{\ColorIfNotInString{0}}}{1}%
     {1}{{\ColorIfNotInString{1}}}{1}%
     {2}{{\ColorIfNotInString{2}}}{1}%
     {3}{{\ColorIfNotInString{3}}}{1}%
     {4}{{\ColorIfNotInString{4}}}{1}%
     {5}{{\ColorIfNotInString{5}}}{1}%
     {6}{{\ColorIfNotInString{6}}}{1}%
     {7}{{\ColorIfNotInString{7}}}{1}%
     {8}{{\ColorIfNotInString{8}}}{1}%
     {9}{{\ColorIfNotInString{9}}}{1}%
     {.0}{{\ColorIfNotInString{.0}}}{1}% Following is to ensure that only periods
     {.1}{{\ColorIfNotInString{.1}}}{1}% followed by a digit are changed.
     {.2}{{\ColorIfNotInString{.2}}}{1}%
     {.3}{{\ColorIfNotInString{.3}}}{1}%
     {.4}{{\ColorIfNotInString{.4}}}{1}%
     {.5}{{\ColorIfNotInString{.5}}}{1}%
     {.6}{{\ColorIfNotInString{.6}}}{1}%
     {.7}{{\ColorIfNotInString{.7}}}{1}%
     {.8}{{\ColorIfNotInString{.8}}}{1}%
     {.9}{{\ColorIfNotInString{.9}}}{1}%
     {\ }{{ }}{1}% handle the space
     ,
     %string=[s]{_}{=},
     %string=[s]{_}{(}, 
  mathescape=true, 
  emphstyle={[2]\color{isoblue}},
  tabsize=4,
  captionpos=b,
  showstringspaces=false,
  keywordstyle=\color{isoblue},
  keywordstyle=[2]\color{isoblue},
  commentstyle=\color{isogreen},%
  stringstyle=\color{isolightblue},%
  morestring=[s][\color{isoblue}]{"}{"},%
  morecomment = [l]{//},
  morecomment = [l]{///},
  morecomment = [s]{/*}{*/},
  sensitive = true,
  morekeywords = {if, else, string, number},%You need to add here
  emph={_Set, _Delay, _strstr, _strtod, _strmid},% You need to add here
  emphstyle=\color{isolightblue},
}

\begin{document}
    \begin{lstlisting}[caption=Isodat language sample..,language=isodat]

        _Set("Precon/Trap 3",0);
        _Delay(1000,1,"Evacuating Carbon trap")
        //_Set("ExtValveBlock/Valve 4",1); commented because interferes with extraction

        string SeqPrepText="";
        number AnzFull=1;

        SeqPrepText =_GetSequenceText("Preparation","1,0");
        //_MessageBox(SeqPrepText,MB_OK,MB_ICONEXCLAMATION);        

        Midpoint    = _strstr (SeqPrepText,",");
        if (Midpoint==-1) 
        {
        /* Format stimmt nicht falsches oder kein Trennzeichen
           no error haldling so far
           cut to 1 and zero by default */
        }
        else
        {
          AnzHalf = _strtod(_strmid(SeqPrepText,(Midpoint+1),(StrLaenge-Midpoint-1)));
        }
    \end{lstlisting}
\end{document}
Jonas
  • 3,353
  • Listings can only scan for tokens. You need more. So I think you need a more powerful tool to highlight such code. Maybe pygments. However therefor you have to write a new Lexer. – Marco Daniel Jun 07 '13 at 11:12
  • You say, it is not possible to highlight numbers within coments in comment color, while they are otherwise in red? i.e. is there no hierarchy in the colour scheme? – Jonas Jun 08 '13 at 06:43
  • This looks like C with different keywords to me, so modifying the Pygments C lexer to create one for this language shouldn't be too hard. – You Jun 08 '13 at 08:59
  • @You: I thought the same but was not able to find any rules for the required language. Jonas: Sorry for this bad news. What do you mean with hierarchy? – Marco Daniel Jun 08 '13 at 10:23
  • I meant, that numbers within comments and strings should not get colored: 1. color all the numbers, 2. color all the comments and strings. -> the numbers in string/comments will not be red. – Jonas Jun 16 '13 at 11:14
  • Blue keywords can be achieved adding keywords=[1]{if,string,number,else},keywordstyle=[1]{\color{blue}} in the Isodat language definition. – guillem Jun 18 '13 at 11:17
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – jub0bs Mar 01 '14 at 21:57

2 Answers2

3

I found it here: How can I change the color of digits when using the listings package?

@Clément: Add a star before the zero, i.e. {0}{{{\color{red}0}}}1 -> *{0}{{{\color{red}0}}}1 to only colour outside of strings and comments. You only need to do this once, so not for the rest of the numerals. – Mark S. Everitt Jan 31 '12 at 15:57

Change

 {"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
 {0}{{\ColorIfNotInString{0}}}{1}%

to

 *{0}{{\ColorIfNotInString{0}}}{1}%

So it looks like:

.png of .pdf from .tex.

\documentclass[fleqn, a4paper, landscape]{article}
\usepackage{xcolor}

\definecolor{isored}{rgb}{0.6,0,0} % for strings
\definecolor{isogreen}{rgb}{0.25,0.6,0.2} % comments
\definecolor{isoblue}{rgb}{0.0,0.0,1} % keyword
\definecolor{isolightblue}{rgb}{0.5,0.5,1.0} % keywords
\definecolor{isocomgreen}{rgb}{0.25,0.35,0.75} % javadoc

\usepackage{etoolbox}

\newtoggle{InString}{}% Keep track of if we are within a string
\togglefalse{InString}% Assume not initally in string
% https://tex.stackexchange.com/questions/34896/coloring-digits-with-the-listings-package?rq=1
\newcommand*{\ColorIfNotInString}[1]{\iftoggle{InString}{#1}{\textcolor{isored}#1}}%
\newcommand*{\ProcessQuote}[1]{#1\iftoggle{InString}{\global\togglefalse{InString}}{\global\toggletrue{InString}}}%

\usepackage{listings} 

\lstdefinelanguage{isodat}
{%
  basicstyle=\ttfamily,%
  literate=%
     *{0}{{\ColorIfNotInString{0}}}{1}%
     {1}{{\ColorIfNotInString{1}}}{1}%
     {2}{{\ColorIfNotInString{2}}}{1}%
     {3}{{\ColorIfNotInString{3}}}{1}%
     {4}{{\ColorIfNotInString{4}}}{1}%
     {5}{{\ColorIfNotInString{5}}}{1}%
     {6}{{\ColorIfNotInString{6}}}{1}%
     {7}{{\ColorIfNotInString{7}}}{1}%
     {8}{{\ColorIfNotInString{8}}}{1}%
     {9}{{\ColorIfNotInString{9}}}{1}%
     {.0}{{\ColorIfNotInString{.0}}}{1}% Following is to ensure that only periods
     {.1}{{\ColorIfNotInString{.1}}}{1}% followed by a digit are changed.
     {.2}{{\ColorIfNotInString{.2}}}{1}%
     {.3}{{\ColorIfNotInString{.3}}}{1}%
     {.4}{{\ColorIfNotInString{.4}}}{1}%
     {.5}{{\ColorIfNotInString{.5}}}{1}%
     {.6}{{\ColorIfNotInString{.6}}}{1}%
     {.7}{{\ColorIfNotInString{.7}}}{1}%
     {.8}{{\ColorIfNotInString{.8}}}{1}%
     {.9}{{\ColorIfNotInString{.9}}}{1}%
     {\ }{{ }}{1}% handle the space
     ,
     %string=[s]{_}{=},
     %string=[s]{_}{(}, 
  mathescape=true, 
  emphstyle={[2]\color{isoblue}},
  tabsize=4,
  captionpos=b,
  showstringspaces=false,
  keywordstyle=\color{isoblue},
  keywordstyle=[2]\color{isoblue},
  commentstyle=\color{isogreen},%
  stringstyle=\color{isolightblue},%
  morestring=[s][\color{isoblue}]{"}{"},%
  morecomment = [l]{//},
  morecomment = [l]{///},
  morecomment = [s]{/*}{*/},
  sensitive = true,
  morekeywords = {if, else, string, number},%You need to add here
  emph={_Set, _Delay, _strstr, _strtod, _strmid},% You need to add here
  emphstyle=\color{isolightblue},
}

\begin{document}
    \begin{lstlisting}[caption=Isodat language sample..,language=isodat]

        _Set("Precon/Trap 3",0);
        _Delay(1000,1,"Evacuating Carbon trap")
        //_Set("ExtValveBlock/Valve 4",1); commented because interferes with extraction

        string SeqPrepText="";
        number AnzFull=1;

        SeqPrepText =_GetSequenceText("Preparation","1,0");
        //_MessageBox(SeqPrepText,MB_OK,MB_ICONEXCLAMATION);        

        Midpoint    = _strstr (SeqPrepText,",");
        if (Midpoint==-1) 
        {
        /* Format stimmt nicht falsches oder kein Trennzeichen
           no error haldling so far
           cut to 1 and zero by default */
        }
        else
        {
          AnzHalf = _strtod(_strmid(SeqPrepText,(Midpoint+1),(StrLaenge-Midpoint-1)));
        }
    \end{lstlisting}
\end{document}
Alles
  • 26
1

I am not able to fix the color of the number, can you check with the following code:

\lstdefinelanguage{isodat}
{
  basicstyle=\ttfamily,%
  literate=%
  %         {"}{{{\ProcessQuote{"}}}}1% Disable coloring within double quotes
     {0}{{\ColorIfNotInString{0}}}{1}%
     {1}{{\ColorIfNotInString{1}}}{1}%
     {2}{{\ColorIfNotInString{2}}}{1}%
     {3}{{\ColorIfNotInString{3}}}{1}%
     {4}{{\ColorIfNotInString{4}}}{1}%
     {5}{{\ColorIfNotInString{5}}}{1}%
     {6}{{\ColorIfNotInString{6}}}{1}%
     {7}{{\ColorIfNotInString{7}}}{1}%
     {8}{{\ColorIfNotInString{8}}}{1}%
     {9}{{\ColorIfNotInString{9}}}{1}%
     {.0}{{\ColorIfNotInString{.0}}}{1}% Following is to ensure that only periods
     {.1}{{\ColorIfNotInString{.1}}}{1}% followed by a digit are changed.
     {.2}{{\ColorIfNotInString{.2}}}{1}%
     {.3}{{\ColorIfNotInString{.3}}}{1}%
     {.4}{{\ColorIfNotInString{.4}}}{1}%
     {.5}{{\ColorIfNotInString{.5}}}{1}%
     {.6}{{\ColorIfNotInString{.6}}}{1}%
     {.7}{{\ColorIfNotInString{.7}}}{1}%
     {.8}{{\ColorIfNotInString{.8}}}{1}%
     {.9}{{\ColorIfNotInString{.9}}}{1}%
     {\ }{{ }}{1}% handle the space
     ,
  mathescape=true, 
  emphstyle={[2]\color{isoblue}},
  tabsize=4,
  captionpos=b,
  showstringspaces=false,
  keywordstyle=\color{isoblue},
  keywordstyle=[2]\color{isoblue},
  commentstyle=\color{isogreen},%
  stringstyle=\color{isolightblue},%
  morestring=[s][\color{isoblue}]{"}{"},%
  morecomment = [l]{//}, 
  morecomment = [l]{///},
  morecomment = [s]{/*}{*/},
  sensitive = true,
  morekeywords = {if, else, string, number},%You need to add here
  emph={_Set, _Delay, _strstr, _strtod, _strmid},% You need to add here
  emphstyle=\color{isolightblue},
}
Jagath
  • 4,287
  • Nice one! I included your solution into the question, so that it gets easier to follow. But too bad you had to remove string=[s]{_}{=}, and string=[s]{_}{(},. - Is there no way of setting like from (inclusive) to (exclusive)? – Jonas Jun 19 '13 at 18:49