A possible manual approach is with the use of moredelim=**[is][\bfseries]{@}{@},
Can I also suggest a style? Below I have defined \lstdefinelanguage
customRthree
in conjunction with package courier
. You can also try beramono
. See for instance, Latex listings R: need mono-spaced font with bold keywords and correct caret (^).
\lstdefinelanguage{customRthree}{
keywords={t},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@}, % every character inside @ @ will be in bold face
basicstyle=\ttfamily,
commentstyle=\textsl,
}
Code
\documentclass[preview,border=5,convert]{standalone}
\usepackage{courier}
\usepackage{listings}
\lstdefinelanguage{customR}{ % defined a new R entry
keywords={t},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}
\lstdefinelanguage{customRtwo}{
otherkeywords={<-,<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
}
\lstdefinelanguage{customRthree}{
keywords={t,R},
otherkeywords={<<-,\%*\%}, %% cannot add <-
keywordstyle=\bfseries,
moredelim=**[is][\bfseries]{@}{@},
basicstyle=\ttfamily,
commentstyle=\textsl,
}
\begin{document}
\lstset{language=R,caption={Standard R}}
\begin{lstlisting}[frame=tb]
S <- H %*% t(pprd) %*% t(H) + R
xest <<- xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\lstset{language=customR,caption={Custom R}}
\begin{lstlisting}[frame=tb]
S @<-@ H %*% t(pprd) %*% t(H) + R
xest @<<-@ xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\lstset{language=customRtwo,caption={Custom R (alt.)}}
\begin{lstlisting}[frame=tb]
S <- H %*% t(pprd) %*% t(H) + R
xest @<@<- xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\begin{lstlisting}[frame=tb,language=customRthree,caption={Custom R 3}]
S @<-@ H %*% t(pprd) %*% t(H) + R
xest @<<-@ xprd + kalmangain %*% (z - H %*% xprd)
}
\end{lstlisting}
\end{document}
Output
As @jubobs commented, Listings treats language and style separately.
- By defining a language, you tell Listings how it should analyze the code; such as
"string in quotation marks"
and //comment after double slashes
.
- By defining a style, you tell Listings how it should typeset the code; such as "strings goes italic" and //comments shrinks.
Go back to your question, a stylish setting is permanent unless you issue another stylish setting or the current group terminates. Similarly, a language assignment is permanent unless you assign another language, the current group terminates, or you issue a stylish setting that contains a language assignment.
In conclusion, perhaps the most systematic way to manage both language and style is to define an exhaustive style that contains a language assignment together with its associated stylish setting. (Just like a language-IDE pair.) And now you can switch between IDEs styles and the no-style-at-all style.
\documentclass{minimal}
\usepackage{listings,xcolor}
\begin{document}
\lstdefinestyle{my IDE setting}{
language=Asymptote,
identifierstyle=\color{purple}
}
\lstdefinelanguage{Asymptote}{
keywords={draw,fill},
morecomment=[l]{//}
}
\lstset{style=my IDE setting}
\lstinline{123 hello world 456}
\lstset{style=}
\lstinline{123 hello world 456}
\end{document}
Best Answer
The best way to show the output along with the LaTeX code is to use the
showexpl
package:Notes:
pos=
option:t
op,b
ottom,r
ight,l
eft,o
uter, andi
nner.Code: