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?

subham soni
  • 9,673
  • 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 01 '14 at 07: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 01 '14 at 08:49
  • See http://tex.stackexchange.com/questions/33039/using-ttfamily-with-bfseries-or-how-to-enable-bold-in-fixed-width-font for the font problem – egreg Jun 01 '14 at 08:53

3 Answers3

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

egreg
  • 1,121,712
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

Khawar
  • 41
  • Welcome to TeX.SX! Posting full examples instead of some fragment of code is most times more useful –  Oct 23 '15 at 15:54
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