First, some thoughts on minted
: this great package relies on Pygments and AFAIK most of the available styles does support Java annotations. Sadly, as you pointed out, the bw
style doesn't highlight annotations.
One possibility is to create an extended version of this style (in order words, create a new style with bw
as base) and then include the instruction that sets the formatting for annotations (I just took a quick look at some styles, but failed to determine which instruction is used by Pygments to refer to an annotation).
If you want to try listings
instead, I'd go with Pouya's suggestion on using custom delimiters. The reason is that, in my humble opinion, since annotations are syntactic metadata, they cannot be treated as keywords or comments, hence a different style to represent them would be a wiser choice.
I know you are using a black and white theme, but I decided to create a colored output just for us to see which style is being applied to each identifier. The concept would be the same, just replace color
occurrences by the font series you want.
In my code, I'd create two delimiters:
moredelim=[il][\textcolor{pgrey}]{$$},
moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
The first, $$
, is applied to the whole line and it can be used for annotations that are in lines of their own (think of a single line comment here). The second one, %% ... %%
, behaves like a multiline comment, which encloses everything that is inside these delimiters, and it's useful for annotations that are applied inline to methods or fields.
A sample code is as follows (the Java example here is a courtesy of Project Lombok):
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{inconsolata}
\usepackage{color}
\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}
\definecolor{pred}{rgb}{0.9,0,0}
\definecolor{pgrey}{rgb}{0.46,0.45,0.48}
\usepackage{listings}
\lstset{language=Java,
showspaces=false,
showtabs=false,
breaklines=true,
showstringspaces=false,
breakatwhitespace=true,
commentstyle=\color{pgreen},
keywordstyle=\color{pblue},
stringstyle=\color{pred},
basicstyle=\ttfamily,
moredelim=[il][\textcolor{pgrey}]{$$},
moredelim=[is][\textcolor{pgrey}]{\%\%}{\%\%}
}
\begin{document}
\begin{lstlisting}
/**
* This is a doc comment.
*/
package com.ociweb.jnb.lombok;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
$$@Data
$$@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
enum Gender { Male, Female }
// another comment
%%@NonNull%% private String firstName;
%%@NonNull%% private String lastName;
%%@NonNull%% private final Gender gender;
%%@NonNull%% private final Date dateOfBirth;
private String ssn;
private String address;
private String city;
private String state;
private String zip;
}
\end{lstlisting}
\end{document}
The output:
Hope it helps. :)
I had some trouble setting up minted
with pygments on Windows, but now it seems to run. So, this is my first minted
text - so, please, forgive me, if I'm doing some nonsense here. Nevertheless, I have found something which may be a ground to build on. The following code uses not exactly your syntax, but creates a myshellbox
instead.
Basically, I use the listings
features of tcolorbox
and trick minted
in at some point:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{minted}
\makeatletter
\tcbset{minted language/.store in=\kvtcb@minted@language}
\def\tcbuselistinglisting{%
\toks@=\expandafter{\kvtcb@listingoptions}%
\edef\tcb@temp{\noexpand\inputminted[\the\toks@]}%
\tcb@temp{\kvtcb@minted@language}{\kvtcb@listingfile}%
}%
\makeatother
\begin{document}
\section{Test}
\newtcblisting[auto counter,number within=section,
list inside=mypyg]{myshellbox}[2][]{%
title={Listing \thetcbcounter: #2},
list entry={\protect\numberline{\thetcbcounter}#2},
minted language=shell-session,
listing options={tabsize=2,fontsize=\footnotesize},
listing only,
enhanced,colframe=red!50!black,drop fuzzy shadow,
#1}
\begin{myshellbox}{Load network configuration with vi}
[root@localhost ~]# vi /etc/sysconfig/network
\end{myshellbox}
\begin{myshellbox}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{myshellbox}
\tcblistof{mypyg}{My Listings}
\end{document}
EDIT: It should me mentioned that the created myshellbox
environment takes an optional parameter, where further options can be put in, e.g. minted language=<something>
or listing option=<something>
.
EDIT: The second solution is a combination of my first solution with Marco Daniel's answer:
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{minted}
\newminted[mycsharp]{csharp}{tabsize=2,fontsize=\footnotesize}
\newminted[myjson]{js}{tabsize=2,fontsize=\footnotesize}
\newminted[myxml]{xml}{tabsize=2,fontsize=\footnotesize}
\newminted[myshell]{shell-session}{tabsize=2,fontsize=\footnotesize}
\newminted[mycode]{text}{tabsize=2,fontsize=\footnotesize}
\newtcolorbox[auto counter,number within=section,
list inside=mypyg]{mintedbox}[2][]{%
title={Listing \thetcbcounter: #2},
list entry={\protect\numberline{\thetcbcounter}#2},
enhanced,colframe=red!50!black,drop fuzzy shadow,#1}
\newenvironment{listingsbox}[3][]
{%
\def\listingsboxenvironment{#2}%save the environments
\VerbatimEnvironment%
\begin{mintedbox}[#1]{#3}%
\begin{\listingsboxenvironment}}%
{%
\end{\listingsboxenvironment}%
\end{mintedbox}%
}
\begin{document}
\section{Test}
\begin{listingsbox}{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}
\begin{listingsbox}{myshell}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{listingsbox}
\begin{listingsbox}{myxml}{XML box}
<hello>World</hello>
\end{listingsbox}
\tcblistof{mypyg}{My Listings}
\end{document}
Update: Newer versions of tcolorbox
have integrated support for minted
. Therefore, a more elegant solution with the same output is possible now. I also added a macro \mynewminted
which creates a style for tcolorbox
plus an equally named minted
environment, if this is needed somewhere else.
\documentclass{article}
\usepackage[many,minted]{tcolorbox}% version 3.03 or better
\newcommand{\mynewminted}[3]{%
\newminted[#1]{#2}{#3}%
\tcbset{myminted/#1/.style={minted language=#2,minted options={#3}}}}
\mynewminted{mycsharp}{csharp}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myjson}{js}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myxml}{xml}{tabsize=2,fontsize=\footnotesize}
\mynewminted{myshell}{shell-session}{tabsize=2,fontsize=\footnotesize}
\mynewminted{mycode}{text}{tabsize=2,fontsize=\footnotesize}
\newtcblisting[auto counter,number within=section,
list inside=mypyg]{listingsbox}[3][]{%
listing only,title={Listing \thetcbcounter: #3},
list entry={\protect\numberline{\thetcbcounter}#3},
enhanced,colframe=red!50!black,drop fuzzy shadow,myminted/#2,#1}
\begin{document}
\section{Test}
\begin{listingsbox}{myshell}{A nice title}
[root@localhost ~]# vi /etc/sysconfig/network
\end{listingsbox}
\begin{listingsbox}{myshell}{Something else}
[root@localhost ~]# vi /etc/sysconfig/something
\end{listingsbox}
\begin{listingsbox}{myxml}{XML box}
<hello>World</hello>
\end{listingsbox}
\tcblistof{mypyg}{My Listings}
\end{document}
Best Answer
Since, AFAICS, no one has done this already, you need to write a JVM lexer for Pygments. That involves some not quite trivial Python programming: see the official instructions for several approaches to constructing a lexer, but you will likely find that the RegexLexer suffices for your needs.