Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
This example works fine for me. Included only listings and xcolor in the document preamble. –  Werner Aug 12 '11 at 16:19
add comment

migrated from stackoverflow.com Aug 12 '11 at 16:09

This question came from our site for professional and enthusiast programmers.

1 Answer

up vote 3 down vote accepted

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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