# Highlight language keywords in a paragraph?

While it is possible to highlight specific language keywords (such as public in C++) by using lstinline command from listing package as in:

Highlighting a C++ keyword \lstinline|public| in a sentence.


Is it possible to provide a list of keywords or better still specify the language and let a macro or other option search-and-augment keywords with lstinline in the paragraph?

There appear to be a related question on StackOverflow but I am not sure how to source keywords.

-
What exactly is it you want to achieve? Automatic highlighting of inline keywords? How should the code know if something is meant to be code, or normal language? Think of words like if, else, or, and, for ... these are all used in natural language. –  Michel Apr 12 '11 at 17:08
Related Question: Possible CSS type features?. Although it may not be useful for entire documents, or large blocks of text. –  Peter Grill Oct 11 '12 at 19:49

## 1 Answer

Is this what you're looking for?

\documentclass{article}

\usepackage{listings}

\lstset{language=C++,morekeywords={foo}}

\begin{document}
Highlighting a C++ keyword \lstinline|public| in a sentence.
Not highlighting (correctly) an unknown word \lstinline|blarg| in a sentence.
Highlighting (correctly) a lstinline-scoped C++ keyword
\lstinline[morekeywords={blarg}]|blarg| in a sentence.
Highlighting another document-scoped C++ keyword \lstinline|foo| in a sentence.

\lstMakeShortInline=

Highlighting a C++ keyword =public= in a sentence.
Not highlighting (correctly) an unknown word =blarg= in a sentence.

\lstDeleteShortInline=
\end{document}

-
Thanks Mike, I am indeed looking for the output that you have provided but was hoping for a method in which I do not have tag every occurrence of keywords. It appears to me from your answer as well from other searches on the internet that this may not be possible. –  R2D2 Aug 8 '11 at 15:19
I tried to make a macro that would take one argument, add it to a \lstMakeShortInline[morekeywords={foo}]= command, and then run =foo= to insert the inline, but that failed. Since user-defined keywords will be indistinguishable from variables, you really ought to just define the unusual ones in one place in the preamble. –  Mike Renfro Aug 8 '11 at 20:23
Forgot to add, if you just want random words to be formatted like keywords, you can just replicate the visual formatting, similar to \newcommand{\mykw}[1]{{\bfseries #1}} -- then just use \mykw{blarg} as needed. –  Mike Renfro Aug 8 '11 at 21:13