我想在程序代码中为“if”着色,我是这样做的:
\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}
如何使用 lstset 中的 colorize 仅为分隔的“if”着色?