个性化列表中的关键字

个性化列表中的关键字

在一份报告中,我介绍了一个新的 R 包,我想给新函数而不是“基本”函数上色。但是,我想保留字符串、注释颜色for以及if

我发现了几个类似的问题,使用我想要的代码但是不起作用......

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}

\usepackage{color}
\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{black}{rgb}{0.0, 0.0, 0.0}
\definecolor{beaver}{rgb}{0.62, 0.51, 0.44}

\lstset{frame=tb,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=left,
  numberstyle=\tiny\color{beaver},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  keywordstyle=\color{black},
  keywordstyle = [2]{\color{blue}},
  keywordstyle = [3]{\color{red}},
  otherkeywords = {fastqPairedFilter, for, if},
  morekeywords = [2]{fastqPairedFilter},
  morekeywords = [3]{for, if},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\begin{document}
\begin{lstlisting}[language=R]
#### Trim and Filter

if(!file_test("-d", filt_path)){
  dir.create(filt_path, recursive = TRUE)
  }

for(i in seq_along(fnFs)){
  fastqPairedFilter(c(fnFs[[i]], fnRs[[i]]),
                    c(filtFs[[i]], filtRs[[i]]),
                    trimLeft = 0, truncLen = 0,
                    maxN = 0, maxEE = c(2, 5), truncQ = 2,
                    compress=TRUE)
  }
\end{lstlisting}
\end{document}

如您所见,这里有一个小技巧,因为我将“base”关键字涂成了黑色。(请注意bluered已经定义)

输出如下: 在此处输入图片描述

实际上,我不知道是否可以覆盖for和,if因为它们仍然在“基本”关键字中。但至少,这应该适用于fastqPairedFilter,不是吗?

答案1

使用deletekeywords您想要更改的关键字(forif)并定义一种样式(我称之为Rlst):

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}

\usepackage{color}
\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{black}{rgb}{0.0, 0.0, 0.0}
\definecolor{beaver}{rgb}{0.62, 0.51, 0.44}

\lstdefinestyle{Rlst}{ 
    language=R,               
    frame=tb,
    aboveskip=3mm,
    belowskip=3mm,
    showstringspaces=false,
    columns=flexible,
    basicstyle={\small\ttfamily},
    numbers=left,
    numberstyle=\tiny\color{beaver},
    commentstyle=\color{dkgreen},
    stringstyle=\color{mauve},
    keywordstyle=\color{black},
    keywordstyle = [2]{\color{blue}},
    keywordstyle = [3]{\color{red}},
    deletekeywords = {for, if},
    morekeywords = [2]{fastqPairedFilter},
    morekeywords = [3]{for, if},
    breaklines=true,
    breakatwhitespace=true,
    tabsize=3
}

\begin{document}

    \begin{lstlisting}[style=Rlst]
    #### Trim and Filter

    if(!file_test("-d", filt_path)){
      dir.create(filt_path, recursive = TRUE)
      }

    for(i in seq_along(fnFs)){
      fastqPairedFilter(c(fnFs[[i]], fnRs[[i]]),
                      c(filtFs[[i]], filtRs[[i]]),
                      trimLeft = 0, truncLen = 0,
                      maxN = 0, maxEE = c(2, 5), truncQ = 2,
                      compress=TRUE)
      }
    \end{lstlisting}
\end{document}

在此处输入图片描述

相关内容