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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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

Is this what you're looking for?

enter image description here




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.


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

share|improve this answer
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

Your Answer


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.