2

Having trouble using listings package...

\lstset{
otherkeywords={0,1,2,3,4,5,6,7,8,9,(,),[,],:},
morekeywords={0,1,2,3,4,5,6,7,8,9,(,),[,],:},
classoffset=0, keywordstyle=\color{violet}, deletekeywords={0,1,2,3,4,5,6,7,8,9,(,),[,],:,function,endfunction,length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs},
classoffset=1, keywordstyle=\color{brown!75}, morekeywords={0,1,2,3,4,5,6,7,8,9},
classoffset=2, keywordstyle=\color{blue!50}, morekeywords={(,),[,]},
classoffset=3, keywordstyle=\color{orange!50!yellow}, morekeywords={:},
classoffset=4, keywordstyle=\color{red!80!black}, morekeywords={function,endfunction},
classoffset=5, keywordstyle=\color{blue!40!cyan}, morekeywords={length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs},
classoffset=0,
}

But the problem is in comments. When I put:

commentstyle=\color{green!30!gray},

my comments aren't green but each number becomes purple.

How can I force the comments to ignore classoffset styles?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
language=Scilab,
literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ű}{{\H{u}}}1 {Ű}{{\H{U}}}1 {ő}{{\H{o}}}1 {Ő}{{\H{O}}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\euro}}1 {£}{{\pounds}}1,
otherkeywords={\%pi,0,1,2,3,4,5,6,7,8,9,(,),[,],:},
morekeywords={\%pi,0,1,2,3,4,5,6,7,8,9,(,),[,],:},
classoffset=0, keywordstyle=\color{violet}, deletekeywords={\%pi,0,1,2,3,4,5,6,7,8,9,(,),[,],:,function,endfunction,length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs,rand},
classoffset=1, keywordstyle=\color{brown!75}, morekeywords={0,1,2,3,4,5,6,7,8,9},
classoffset=2, keywordstyle=\color{blue!50}, morekeywords={(,),[,]},
classoffset=3, keywordstyle=\color{orange!50!yellow}, morekeywords={:},
classoffset=4, keywordstyle=\color{red!80!black}, morekeywords={function,endfunction},
classoffset=5, keywordstyle=\color{blue!40!cyan}, morekeywords={length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs,rand},
classoffset=6, keywordstyle=\color{red!50!white}, morekeywords={\%pi},
classoffset=0,
numbers=left,
breaklines=true,
commentstyle=\color{green!30!gray},
}
\begin{document}
\begin{lstlisting}
clear;
clf();
// Définition de la fonction f (signal) :
function y=signal(t)
    y=cos(t*2*%pi*4/128)+cos(t*2*%pi*13/128)+cos(t*2*%pi*40/128)+(1/2)*rand(1,1);
endfunction
// Valeur moyenne de f(x) pour un x donné :
function y=signalMoy(t)
    y=cos(t*2*%pi*4/128)+cos(t*2*%pi*13/128)+cos(t*2*%pi*40/128)+(1/4);
endfunction
// ------------------------------ Question 2 ------------------------------ //
// ----- Méthode de Newton Cotes ----- //
// Méthode de Simpson composée
// J'utilise le programme fait au Tp1 pour la méthode de Newton Cotes et je
// l'applique aux poids [1/3,4/3,1/3] pour avoir la méthode de Simpson 1/3
// Comme dans le TP nous reprenons le fonctionnement elem et comp.
\end{lstlisting}
\end{document}

img

To get there that was quite hard but I've not a single clue to continue to modify the color...

If someone can help me that's wonderful!

Proton
  • 23
  • Welcome to TeX.SX! Can you please also show an example of wrong behavior? – egreg Nov 11 '16 at 20:34
  • I just did, hope that's enough. Tell me if I can give you more info – Proton Nov 12 '16 at 16:21
  • I'm quite new with LaTeX but someone as a clue : why the ')' is not colored in comments while everything else is? What's the difference between '(' and ')' in LaTeX that can cause that? – Proton Nov 16 '16 at 09:39
  • (a) If you turn off breaklines then the right parenthesis is colored; (b) It seems otherkeywords are still going to be highlighted in comments. – Symbol 1 Nov 17 '16 at 06:26

1 Answers1

2

So after reading Colored strings by listings package and Problem with literate and breaklines=true in listings package I can somehow fix the problem.

Since is used, You need to run this code with XeLaTeX and LuaLaTeX. The reason is that literate=* is broken by your accent characters; and also, if you are going to use a lot of them, using a unicode compiler will make your life easier.

\documentclass{article}

\usepackage{fontspec}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{etoolbox}
\lstset{
language=Scilab,
literate=*
  {0}{{{\color{brown!75}0}}}1 {(}{{{\color{blue!50}(}}}1
  {1}{{{\color{brown!75}1}}}1 {)}{{{\color{blue!50})}}}1
  {2}{{{\color{brown!75}2}}}1 {[}{{{\color{blue!50}[}}}1
  {3}{{{\color{brown!75}3}}}1 {]}{{{\color{blue!50}]}}}1
  {4}{{{\color{brown!75}4}}}1 {:}{{{\color{orange!50!yellow}:}}}1
  {5}{{{\color{brown!75}5}}}1
  {6}{{{\color{brown!75}6}}}1
  {7}{{{\color{brown!75}7}}}1
  {8}{{{\color{brown!75}8}}}1
  {9}{{{\color{brown!75}9}}}1,
classoffset=0, keywordstyle=\color{violet}, deletekeywords={function,endfunction,length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs,rand},
classoffset=4, keywordstyle=\color{red!80!black}, morekeywords={function,endfunction},
classoffset=5, keywordstyle=\color{blue!40!cyan}, morekeywords={length,deff,zeros,sqrt,xtitle,cos,sin,intg,abs,rand},
classoffset=6, keywordstyle=\color{red!50!white}, morekeywords={\%pi},
classoffset=0,
numbers=left,
breaklines=true,
commentstyle=\color{green!30!gray},
}
\makeatletter
\patchcmd{\lsthk@SelectCharTable}{)}{`}{}{} 
\makeatother 
\begin{document}
\begin{lstlisting}
clear;
clf();
// Définition de la fonction f (signal) :
function y=signal(t)
    y=cos(t*2*%pi*4/128)+cos(t*2*%pi*13/128)+cos(t*2*%pi*40/128)+(1/2)*rand(1,1);
endfunction
// Valeur moyenne de f(x) pour un x donné :
function y=signalMoy(t)
    y=cos(t*2*%pi*4/128)+cos(t*2*%pi*13/128)+cos(t*2*%pi*40/128)+(1/4);
endfunction
// ------------------------------ Question 2 ------------------------------ //
// ----- Méthode de Newton Cotes ----- //
// Méthode de Simpson composée
// J'utilise le programme fait au Tp1 pour la méthode de Newton Cotes et je
// l'applique aux poids [1/3,4/3,1/3] pour avoir la méthode de Simpson 1/3
// Comme dans le TP nous reprenons le fonctionnement elem et comp.
\end{lstlisting}
\end{document}

Symbol 1
  • 36,855
  • Gonna try this that week end and valid your answer just after. That's awesome ! I'm not sure I understand why the "é" are gone and how to keep them but I'll get throw this rereading your comment ! Thank's a lot ! – Proton Nov 17 '16 at 19:56
  • @Remi Because I captured my screen before I loaded fontspec and without fontspec there is no glyph for é. But this is not a big issue--as long as you load whatever your favorite font, Comic Sans or Times New Roman, it will automatically be fixed. – Symbol 1 Nov 17 '16 at 20:14
  • Well, I'm almost there. I'll stop using é to avoid fontspec for now. Everything else work, now I'll try to have real space on strings and to color them, hope I'll make it without help :). Thank you for the help ! The only thing still annoying is if I call a variable x1 the 1 is colored. That's why I didn't try hard with literate in the first place. But thank you again. Great help here ! – Proton Nov 18 '16 at 14:29
  • @Remi I did not read those answers in links carefully. But there is a chance that in the literate of digits you can put some if-clause, distinguishing if it is a lonely digit or part of a variable. – Symbol 1 Nov 18 '16 at 14:53
  • Still some tiny issues and I did delete 2 comments. I'll make a new post (I think it's better) for it soon and make a link from this one. – Proton Nov 18 '16 at 15:49