代码块颜色

代码块颜色

你好,这是我目前使用的代码块代码


\lstset{
  backgroundcolor=\color{gray!10},  
  basicstyle=\ttfamily,
  columns=fullflexible,
  breakatwhitespace=false,      
  breaklines=true,                
  captionpos=b,                    
  commentstyle=\color{mygreen}, 
  extendedchars=true,              
  frame=single,                   
  keepspaces=true,             
  keywordstyle=\color{blue},      
  language=c++,                 
  numbers=none,                
  numbersep=5pt,                   
  numberstyle=\tiny\color{blue}, 
  rulecolor=\color{black},        
  showspaces=false,               
  showstringspaces=false,
  showtabs=false,                 
  stepnumber=5,                  
  stringstyle=\color{mymauve},    
  tabsize=3,                      
  title=\lstname                
}

在cp算法的代码中 https://cp-algorithms.com/sequences/longest_increasing_subsequence.html

我怎样才能将数字颜色改为红色?还有,是否可以更改函数颜色?(使用 lstlisting)

答案1

这是一个非常适合您具体问题的解决方案。我尝试坚持您链接的网站上的代码风格。您必须手动添加数字和函数名称,以所需的颜色突出显示它们。

\documentclass{article}

\usepackage{xcolor}
\usepackage{listings} % Display code / shell commands
\usepackage{lstautogobble}

\lstset{  
    language=C++,
    backgroundcolor=\color{gray!10},  
    basicstyle=\ttfamily,
    columns=fullflexible,   
    breakatwhitespace=false,  
    captionpos=b, 
    commentstyle=\color{mygreen}, 
    extendedchars=true,              
    frame=single, 
    keepspaces=true,  
    keywordstyle=\color{blue},      
    numbers=none,                
    numbersep=5pt,
    numberstyle=\tiny\color{blue}, 
    rulecolor=\color{black},        
    showspaces=false,               
    showstringspaces=false,
    showtabs=false,                 
    stepnumber=5, 
    tabsize=3,                      
    title=\lstname  
    breaklines=true,
    emph={lis},                   % emphasize the function lis ...
    emphstyle={\color{purple}},   % ... and take purple as emph-color
    literate=                     % color single literates 0, -1 and 1e9
    {0}{{{\color{red}0}}}1
    {-1}{{{\color{red}-1}}}1
    {1e9}{{{\color{red}1e9}}}1,
}


\begin{document}
\begin{lstlisting}
int lis(vector<int> const& a) {
    int n = a.size();
    const int INF = 1e9;
    vector<int> d(n+1, INF);
    d[0] = -INF;

    for (int i = 0; i < n; i++) {
        int j = upper_bound(d.begin(), d.end(), a[i]) - d.begin();
        if (d[j-1] < a[i] && a[i] < d[j])
            d[j] = a[i];
    }

    int ans = 0;
    for (int i = 0; i <= n; i++) {
        if (d[i] < INF)
            ans = i;
    }
    return ans;
}
\end{lstlisting}
\end{document} 

在此处输入图片描述

相关内容