My solution
(Optional) Define custom colors
I first created some custom colors in mysettings.sty
.
% annotation background colors
\definecolor{blueannoback}{RGB}{234,242,250}
\definecolor{greenannoback}{RGB}{230,244,214}
My main.tex
file includes the following packages:
\usepackage{xcolor}
\usepackage{mysettings}
Custom listing style
Next I define a custom style for each programming language of my choice. In this example I define a custom language which is based on Java.
The custom Java language allows me to color the font of my annotations automatically:
% Define custom Java language
\lstdefinelanguage{custom-java}{
...,
morecomment=[l][\textcolor{BurntOrange}]{@}
}
The custom listing style provides a custom way to specify escapeinside
in order to use LateX commands inside my listing, which I will need:
\lstdefinestyle{my-java-style}{
language=custom-java,
...,
escapeinside={\%*}{*)}
}
The listing
I then use a colorbox
to wrap the text in a background with the color of my choosing. The text needs to be escaped so that LateX code may be executed inside the listing. Note that this overrides the BurntOrange font color I had defined for lines that start with @
.
\begin{lstlisting}[style=my-java-style, otherkeywords={this}]
%*\colorbox{blueannoback}{@Entity}*)
%*\colorbox{blueannoback}{@Table(name="CourseCredits")}*)
%*\colorbox{greenannoback}{@Function}*)
public class CourseCredit %*\colorbox{blueannoback}{implements Serializable}*) {
%*\colorbox{blueannoback}{@Id}*)
%*\colorbox{blueannoback}{@Column(name="Course")}*)
%*\colorbox{greenannoback}{@ArgumentType(value=1, typeName="Course")}*)
private String course;
%*\colorbox{blueannoback}{@Column(name="Credits")}*)
%*\colorbox{greenannoback}{@FunctionValue("Credits")}*)
private int credits;
public %*\colorbox{blueannoback}{CourseCredit()}*){
}
public %*\colorbox{greenannoback}{CourseCredit(String course, int credits)}*){
this.course = course;
this.credits = credits;
}
}\end{lstlisting}
Result

Full code for minimal working example
Here's the full code to reproduce the above image. I tried to keep it compact.
mysettings.sty
% Colors
\definecolor{javapurple}{rgb}{0.5,0,0.35}
\definecolor{blueannoback}{RGB}{234,242,250}
\definecolor{greenannoback}{RGB}{230,244,214}
% Define language
\lstdefinelanguage{my-java}{
keywords={private,public,class,this}
}
% define style
\lstdefinestyle{my-java-style}{
language=my-java,
basicstyle=\footnotesize,
captionpos=b,
alsoletter={.},
escapeinside={\%*}{*)},
extendedchars=true,
frame=tb,
keepspaces=true,
keywordstyle=\color{javapurple}\bfseries,
rulecolor=\color{black},
tabsize=4
}
main.tex
\documentclass[12pt]{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{mysettings} % my .sty file
\begin{document}
\begin{lstlisting}[style=my-java-style,otherkeywords={this}]
%*\colorbox{blueannoback}{@Entity}*)
%*\colorbox{blueannoback}{@Table(name="CourseCredits")}*)
%*\colorbox{greenannoback}{@Function}*)
public class CourseCredit %*\colorbox{blueannoback}{implements Serializable}*) {
%*\colorbox{blueannoback}{@Id}*)
%*\colorbox{blueannoback}{@Column(name="Course")}*)
%*\colorbox{greenannoback}{@ArgumentType(value=1, typeName="Course")}*)
private String course;
%*\colorbox{blueannoback}{@Column(name="Credits")}*)
%*\colorbox{greenannoback}{@FunctionValue("Credits")}*)
private int credits;
public %*\colorbox{blueannoback}{CourseCredit()}*){
}
public %*\colorbox{greenannoback}{CourseCredit(String course, int credits)}*){
this.course = course;
this.credits = credits;
}
}\end{lstlisting}
\end{document}
listings
block?