TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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

I'm using listings and I want to extend a language with additional keywords for highlightning. I know there is an option morekeywords available, but that only works for a particular lstset declaration and I don't want to have to copy-paste all keywords into all listings. I've tried declaring the keywords as part of a command but that doesn't work (probably because the command is not expanded and I don't know how to fix that).

So is there any way of declaring a new language by copying an existing one and just adding more keywords?

share|improve this question
up vote 24 down vote accepted

You can define the keywords in a separate file and just include it in your preamble. Here is an example of adding for and downto as new keywords:

enter image description here


    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,%

% Add your keywords here, and have this in a separate file
% and include it in your preamble
    downto, for%

y = 0
for i = n downto 0
    y = a_i + x * y
share|improve this answer
I never thought about using a separate \lstset to define the keywords! This solved my problem! – gablin Sep 13 '11 at 6:51

It seems that morekeywords also works for extending a language. For instance the Python keyword super isn't in the list of default keywords. In my preamble, I can set it as a keyword by doing:


Of course, it would be ideal to contribute those missing keywords to the listings package.

share|improve this answer
Works like a charm... – mtsz Feb 28 '13 at 17:27

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.