1

I want to colorize "if" in code of program and I did in that way:

\documentclass[a4paper,12pt,titlepage,headsepline,footsepline,hidelinks]{scrartcl}
    \usepackage[margin=2.5cm]{geometry}
    \usepackage{ucs}
    \usepackage[T1]{fontenc}
    \usepackage[polish]{babel}
    \usepackage[utf8]{inputenc}
    \usepackage{lmodern,scrpage2,lastpage,graphicx,indentfirst,setspace}
    \usepackage{hyperref}
    \usepackage[owncaptions]{vhistory}
    \usepackage{tabularx}
    \usepackage{listings}
    \usepackage{caption}
    \captionsetup{justification=centering}
    \usepackage{color} % additional packages
    \usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
    \definecolor{gainsboro}{HTML}{dcdcdc}
    \definecolor{gainsboro2}{HTML}{f8f8f8}
    \definecolor{olivegreen}{rgb}{0,0.6,0}
    \definecolor{oliveyellow2}{HTML}{AE9C2C}
    \def\nochangecolor{\def\color##1{}}
    \usepackage{listings}
    \lstset{ 
        language=C++, % choose the language of the code to colorize code 
        basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},
        keywordstyle=\color{black}\bfseries, % style for keywords
        numbers=none, % where to put the line-numbers
        numberstyle=\tiny, % the size of the fonts that are used for the line-numbers     
        backgroundcolor=\color{gainsboro2},
        showspaces=false, % show spaces adding particular underscores
        showstringspaces=false, % underline spaces within strings
        showtabs=false, % show tabs within strings adding particular underscores
        frame=single, % adds a frame around the code
        tabsize=2, % sets default tabsize to 2 spaces
        rulesepcolor=\color{gray},
        rulecolor=\color{black},
        captionpos=b, % sets the caption-position to bottom
        breaklines=true, % sets automatic line breaking
        breakatwhitespace=false, 
        moredelim=[is][\color{olivegreen}\nochangecolor]{@startgreen}{@endgreen},
        moredelim=[is][\color{oliveyellow2}\nochangecolor]{@startkom}{@endkom},
    }
    \lstset{  
        %colorize code
    literate={ą}{{\k{a}}}1
                 {Ą}{{\k{A}}}1
                 {ę}{{\k{e}}}1
                 {Ę}{{\k{E}}}1
                 {ó}{{\'o}}1
                 {Ó}{{\'O}}1
                 {ś}{{\'s}}1
                 {Ś}{{\'S}}1
                 {ł}{{\l{}}}1
                 {Ł}{{\L{}}}1
                 {ż}{{\.z}}1
                 {Ż}{{\.Z}}1
                 {ź}{{\'z}}1
                 {Ź}{{\'Z}}1
                 {ć}{{\'c}}1
                 {Ć}{{\'C}}1
                 {ń}{{\'n}}1
                 {Ń}{{\'N}}1
                 {void}{{{\color{blue}void}}}4
                 {main}{{{\color{blue}main}}}4 
                 {uchar}{{{\color{blue}uchar}}}5 
                 {while}{{{\color{blue}while}}}5
                 {for}{{{\color{blue}for}}}3
                 {slong}{{{\color{blue}slong}}}5
                 {ulong}{{{\color{blue}ulong}}}5
                 {sint}{{{\color{blue}sint}}}4
                 {return}{{{\color{blue}return}}}6
                 {uint}{{{\color{blue}uint}}}4
                 {if}{{{\color{blue}if}}}2
                 %keywordstyle = [2]{\color{yellow}},
                 %morekeywords = [2]{if},
                 {(}{{{\color{blue}(}}}1             
                 {&)}{{{\color{blue})}}}1
                 {*)}{{{\color{blue})}}}1
                 {<}{{{\color{blue}<}}}1
                 {>}{{{\color{blue}>}}}1    
                 {[}{{{\color{blue}[}}}1
                 {]}{{{\color{blue}]}}}1  
                 {=}{{{\color{blue}=}}}1  
                 {\+}{{{\color{blue}+}}}1
                 {\-}{{{\color{blue}\-}}}1   
                 {\;}{{{\color{blue};}}}1         
     }           
    \lstset{breaklines=true,
    basicstyle=\ttfamily}
    \lstset{basewidth=0.5em}
    \lstset{extendedchars=\true}
    \lstset{inputencoding=ansinew} 
\begin{document}

\begin{lstlisting}
{
}

// deklaracja funkcji (wykorzystywanej tylko wew. bloku)
void _obsluga_com2_zajetosc_fifo(uchar wartosc) 
{
}
\end{lstlisting}

\end{document}

But I don't want colorize "if" in middle word for example I've got this: enter image description here

How to use colorize in lstset to colorize only separated "if"?

  • 1
    Quick guess: Don't use literate for keywords. There should be a keyword or similar option for \lstset. Check the manual. – Martin Scharrer Dec 11 '17 at 9:47
  • I tried using \lstset{ keywordstyle={\color{blue}}, otherkeywords={if} } But finally I've the same results... – John Saladin Dec 11 '17 at 10:15
  • 1
    \lstset{keywordstyle=\color{blue}, morekeywords={if,uchar} } and delete: {if}{{{\color{blue}if}}}2, etc. – user91669 Dec 11 '17 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.