在字符上覆盖\覆盖颜色

在字符上覆盖\覆盖颜色

在大多数情况下,我希望为简单字符着色 - 例如:“<” ">”,但在某些情况下,我需要其他颜色。我尝试使用以下方法进行覆盖:

moredelim=**[is][{\color{olivegreen}}]{@startgreen}{@endgreen}

    \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}
    \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}}]{@startgreen}{@endgreen},
        }
    \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{red}void}}}4
                 {main}{{{\color{red}main}}}4 
                 {(}{{{\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}

Example:

\begin{lstlisting}
// dlugosc bufora nadawania
#define _Cobsluga_com2_fifo_nadawania 16    

// zmienna globalna (wykorzystywana także na zewnątrz bloku)
uint obsluga_com2_status; 

@startgreen// zmienna globalna (wykorzystywana tylko wew. bloku)
uchar _obsluga_com2_fifo_nadawania[_Cobsluga_com2_fifo_nadawania]; @endgreen    }

// deklaracja funkcji (wykorzystywanej także na zewnątrz bloku)
uchar obsluga_com2_nadawanie(uchar znak) 
\end{lstlisting}

\end{document}

但是像“(”“[”“]”这样的字符仍然是蓝色的 - 如何覆盖/覆盖颜色?

蓝色字符的问题

答案1

定义\nochangecolor如下:

\def\nochangecolor{\def\color##1{}}

然后改变选项moredelim

moredelim=[is][\color{olivegreen}\nochangecolor]{@startgreen}{@endgreen}

完整代码:

\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}
    \usepackage{listings}

    \def\nochangecolor{\def\color##1{}}
    \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{oliveyellow2}\nochangecolor]{@startkom}{@endkom},
        moredelim=[is][\color{olivegreen}\nochangecolor]{@startgreen}{@endgreen}
        }
    \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{red}void}}}4
                 {main}{{{\color{red}main}}}4%
                 {(}{{{\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}

Example:

\begin{lstlisting}
// dlugosc bufora nadawania
#define _Cobsluga_com2_fifo_nadawania 16    

// zmienna globalna (wykorzystywana także na zewnątrz bloku)
uint obsluga_com2_status; 

@startgreen// zmienna globalna (wykorzystywana tylko wew. bloku)
uchar _obsluga_com2_fifo_nadawania[_Cobsluga_com2_fifo_nadawania]; @endgreen    }

// deklaracja funkcji (wykorzystywanej także na zewnątrz bloku)
uchar obsluga_com2_nadawanie(uchar znak)
\end{lstlisting}

\begin{lstlisting}
@startkom  // dlugosc bufora nadawania @endkom
@startgreen #define _Cobsluga_com2_fifo_nadawania 16 @endgreen
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

我定义了两种颜色:

\definecolor{olivegreen}{rgb}{0,0.6,0}
\definecolor{oliveyellow2}{HTML}{AE9C2C}
\def\nochangecolor{\def\color##1{}}

...

    moredelim=**[is][\color{olivegreen}\nochangecolor]{@startgreen}{@endgreen},
    moredelim=**[is][\color{oliveyellow2}\nochangecolor]{@startkom}{@endkom},

...
\begin{document}
\begin{lstlisting}
@startkom// dlugosc bufora nadawania @endkom
@startgreen #define _Cobsluga_com2_fifo_nadawania 16 @endgreen
\end{lstlisting}
\end{document}

最后我得到了两条黄色的线,但我希望第一条线是黄色,第二条线是绿色。

如何解决?

相关内容