I use the listings package to display code snippets in my documents. To add syntax highlighting to my javascript listings, I have defined javascript as language for the listings package. The keywords I defined will be highlighted even if they appear within comments. How can I prevent this?

Minimum working example for my language definition:



Example listing:

// if 



Desired output:

Desired Output

  • Did you figure out the solution to this problem?
    – aaragon
    Aug 10, 2020 at 18:31
  • No, not really. As a workaround I removed the if from the keywords list and added it as literate: literate={if}{{{\color{blue}if}}}{3}
    – HerthaBSC
    Aug 11, 2020 at 19:58

1 Answer 1


I tried this in Overleaf, and it works. For solution see: https://www.overleaf.com/read/zrxjfjvvbghj. Please note that I added the curly brackets, i.e.: morecomment=[l]{//}. However, I also tried this without them and it works fine.

Update: The package seems to figure out how to overcome this error. Unfortunately, I cannot determine what the cause was.

  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Miyase
    Jan 16, 2023 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .