2

I'm using the listings-package for LaTeX to format source-codes within my document. I can emphasize keywords using the emph-option of the listings package:

\lstset{
    emph={nerv},
    emphstyle={\color{red}\textbf}
}

\begin{lstlisting}[
    frame=trBL,
    caption={R-Code (test).},
    label={code:R_allee}
]
# comment
square <- function(x) {
    x^2
}

nerv = "Str ing "

x <- c(1:100)
y <- square(x)
\end{lstlisting}

This makes the "nerv" withing my source code bold and red. But, I want to highlight the "ing" withing the String "Str ing " using:

\lstset{
    emph={ing},
    emphstyle={\color{red}\textbf}
}

Unfortunately that does not work. I couldn't figure out what might solve the problem.

Does anyone of you have an idea?

1
  • This example works fine for me. Included only listings and xcolor in the document preamble.
    – Werner
    Aug 12, 2011 at 16:19

2 Answers 2

5

I understand that this may be a moot point, since you only provide a minimal example. However, I also assume that highlighting the string ing in "Str ing " may be limited to only that specific instance in your code. For that, there is no need to define a style that is global to the entire listing. You could momentarily escape the listings environment (producing a local change), and typeset something using standard LaTeX by setting the mathescape=true flag:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\begin{document}

\lstset{
    emph={ing},
    emphstyle={\color{red}\textit}
}

\begin{lstlisting}[
    frame=trBL,mathescape=true,
    caption={R-Code (test).},
    label={code:R_allee}
]
# comment
square <- function(x) {
    x^2
}

nerv = "Str $\color{blue}\textbf{ing}$ "
burn ing = hot

x <- c(1:100)
y <- square(x)
\end{lstlisting}

​\end{document}​

mathescape from within lstlisting

Another good example of such an illustration: Lstlistings: Getting pretty equations in display mode

0

I stumbled upon a similar problem today and found another solution to this rather old question. The listings package allows us to define delimiters inside of which the package will still process keywords and other stuff. From the listings documentation:

You can even let the package detect keywords, comments, strings, and other de- limiters inside the contents.

\lstset{moredelim=*[s][\itshape]{/*}{*/}}

The * modifier also works on string definitions so we can define our own strings that still allow keywords inside of them:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}
\lstset{
    emph={ing},
    emphstyle={\color{red}\textit},
    morestring=*[b]"  % the quote may be escaped by a _b_ackslash
}

\begin{lstlisting}[
    frame=trBL,mathescape=true,
    caption={R-Code (test).},
    label={code:R_allee}
]
# comment
square <- function(x) {
    x^2
}

nerv = "Str ing"
burn ing = hot

x <- c(1:100)
y <- square(x)
\end{lstlisting}
​\end{document}

Note that the "ing" still has to be its own identifier, though. enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.