3

I use minted to highlight some javascript code.

I use

\documentclass{article}

\usepackage{minted}

\begin{document}

\begin{minted}{js}
const mySchema = new Schema({
  name: String,
  is_true: Boolean,
  age: Number,
  object_ids: [ObjectId],
});
\end{minted}

\end{document}

to produce

enter image description here

The problem is that it highlights String, Boolean and Number, but not ObjectId.

How can I add ObjectId as a keyword to javascript in minted?

3

Regarding minted, the answer is: You have to write an extension to the JavaScript lexer of pygmentize or modify a copy of the lexer. See the answers to the question How to add custom C++ keywords to be recognized by Minted? for details.

If you are not bound to use minted, use the package listings. Here is an example for defining JavaScript highlighting (adapted from this answer to the question language option supported in listings. You can easily define your own classes of keywords and their formatting.

enter image description here

\documentclass{article}
\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{js}{
  keywords={const, typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  keywords=[2]{boolean, string, number, objectid},
  keywordstyle=[2]\color{green}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstset{
   language=js,
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   tabsize=2,
   breaklines=true,
   showtabs=false
}

\begin{document}

\begin{lstlisting}
const mySchema = new Schema({
  name: String,
  is_true: Boolean,
  age: Number,
  object_ids: [ObjectId],
});
\end{lstlisting}

\end{document}
  • Thanks, but can I find the same syntax highlighting as in minted (i.e. same colors and keywords), and then just use it in listings and append the extra keyword? – Jamgreen Jan 21 '17 at 15:08
  • @Jamgreen If you look around on the net, you might find a language definition closer to the style of minted; I have no pointers since I don't use JavaScript highlighting. If you want to have exactly the style of minted, then you probably have to copy the code of the JavaScript lexer, add your keywords, and make it known to pygmentize (see the pointers above). – gernot Jan 21 '17 at 15:13
2

Since I only needed highlighting for one word and couldn't find any quick solution (and didn't want to rewrite the entire highlighting), I came up with this (ashamingly) dirty workaround:

Firstly, identify the RGB color needed for highlighting. You can get it through a color picker (e.g. the inbuilt ones from web browsers) or use this value as a basis to find the exact rgb value in the <yourMintedStyle>.pygstlye file (you can find it in the _minted folder once you compile your project.

Then define this color in your latex document and color the desired word through escape characters in the minted environment. Here's a minimum example for the trac style for golang:

\documentclass{article}
\usepackage{minted}
\usepackage{xcolor}

\begin{document}
\usemintedstyle{trac}
\definecolor{mblue}{rgb}{0.27,0.33,0.53}

\begin{minted}[escapeinside=||]{go}
type SomeType struct {
    Foo        uint64
    Standard   FooBar
    Tweaked    |\textcolor{mblue}{FooBar}|
}
\end{minted}
\end{document}

Output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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