# Personalize keywords in listings

In a report, I present a new R package and I want to color the new functions and not the "base" ones. However, I want to keep the strings, comment color and also for and if.

I find several similar questions and one with the code I want but that doesn't work...

\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},
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}


As you can see, there is a little trick because I color the "base" keywords in black. (Note that blue and red are already defined)

Here's the output:

Actually, I don't know if I can overwrite for and if because they're still in the "base" keywords. But at least, this should work for fastqPairedFilter, no?

Use deletekeywords for the keywords you want to change (for and if) and define a style (I've called it 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},
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}


• @MrSnake You're welcome, thank you for accepting my answer! – CarLaTeX Oct 1 '17 at 7:47