7

In minted, how do I highlight a particular set of keywords by making them bold. Say for example consider the MWE,

\documentclass[a4paper , 12pt]{article}
\usepackage{minted}
\usemintedstyle{manni}
\begin{document}
\begin{minted}{java}
class Test
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
    }
}
\end{minted}
\end{document} 

The above generates this:
enter image description here
Now, whenever I use the keyword class, it should be in bold. How do I do that?

| improve this question | | | | |
  • 1
    This is out of control for minted, which delegates the job to Pygmentize and its lexers. A Python forum would be better, I guess. – egreg Jun 1 '14 at 7:36
  • 1
    On the other hand, class is already printed boldface; try adding \usepackage[lighttt]{lmodern} to realize it. The fact is that the usual Computer Modern Typewriter fonts don't have a boldface variant. – egreg Jun 1 '14 at 8:49
  • See tex.stackexchange.com/questions/33039/… for the font problem – egreg Jun 1 '14 at 8:53
3

AFAIK, there is no such option with minted. But with listings it is easy. you can use

escapechar=!,   %% use any character as you wish

or

moredelim=[is][\bfseries\color{magenta}]{`}{`},   %% same as above

Or

escapeinside={*@}{@*}     %% Use as *@ \bfseries class @*

etc.

Here is some sample:

\documentclass[a4paper,11pt]{article}
\usepackage{xcolor}
\usepackage{courier}
\usepackage{listings} % for code snippets

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=single,
  framerule=0pt,
  backgroundcolor=\color{olive!10},
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=fullflexible,
  basicstyle={\footnotesize\ttfamily},
  numbers=left,
  numbersep=10pt,
  numberstyle=\scriptsize\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  escapechar=!,
  moredelim=[is][\bfseries\color{magenta}]{`}{`},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=2
}

\begin{document}
\begin{lstlisting}[escapechar=!,]
!\bfseries\color{blue} class! Test
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
    }
}
`class`
\end{lstlisting}
\end{document}

enter image description here

| improve this answer | | | | |
  • 1
    In fact you can do the same for minted, you just have to adapt either the stylesheet or the lexer (both are written in Python). – Konrad Rudolph Jun 1 '14 at 10:28
  • Minted now supports escapeinside, although it is fragile in some cases. – Daniel H May 10 '17 at 23:41
11

The class keyword is already printed boldface. The problem is that the default Computer Modern Typewriter doesn't have a boldface variant, see Using \ttfamily with \bfseries (or how to enable bold in fixed-width font) for details.

Indeed, if we follow one of the suggestions, the example file

\documentclass[a4paper , 12pt]{article}
\usepackage[lighttt]{lmodern}
\usepackage{minted}
\usemintedstyle{manni}
\begin{document}
\begin{minted}{java}
class Test
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
    }
}
\end{minted}
\end{document}

will print

enter image description here

However, here's a set of macros for overriding the style for particular keywords (thanks to Marco Daniel for suggesting the idea).

\documentclass[a4paper , 12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage[lighttt]{lmodern}
\usepackage{minted}
\usemintedstyle{manni}

\ExplSyntaxOn
\NewDocumentCommand \AddWordToMinted { m m }
 {
  \prop_gput:Nnn \g_xminted_highlight_prop { #1 } { #2 }
 }
\prop_new:N \g_xminted_highlight_prop

\AtBeginDocument{
  \cs_set_eq:NN \xminted_pydo:nn \PY
  \cs_new_protected:Npn \xminted_newpydo:nn #1 #2
   {
    \xminted_pydo:nn { #1 } { \prop_get:Nn \g_xminted_highlight_prop { #2 } #2 }
   }
  \cs_set_eq:NN \PY \xminted_newpydo:nn
}

\ExplSyntaxOff

%% Just to experiment
\AddWordToMinted{class}{\sffamily}
\AddWordToMinted{void}{\itshape}

\begin{document}

\begin{minted}{java}
class Test
{
    public static void main(String args[])
    {
        System.out.println("Hello World");
    }
}
\end{minted}
\end{document}

enter image description here

| improve this answer | | | | |
  • Note that as of September 2014, \prop_get:Nn has been renamed to \prop_item:Nn – egreg Sep 17 '14 at 17:56
4

Try using escapeinside option available in the latest minted release v2.1 from 09/09/2015. See the documentation here http://ctan.mirrorcatalogs.com/macros/latex/contrib/minted/minted.pdf. The documentation of escapeinside says the following.

Escape to LATEX between the two characters specified in (string). All code between the two characters will be interpreted as LATEX and typeset accordingly. This allows for additional formatting. The escape characters need not be identical. Special LATEX characters must be escaped when they are used as the escape characters (for example, escapeinside=#\%). Requires Pygments 2.0+.

\begin{minted}[escapeinside=||]{py}
  def f(x):
     y = x|\colorbox{green}{**}|2
     return y
\end{minted}

resulted in enter image description here

| improve this answer | | | | |
  • Welcome to TeX.SX! Posting full examples instead of some fragment of code is most times more useful – user31729 Oct 23 '15 at 15:54

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.