# Highlighting Java Code like Bitcoin Whitepaper

In Bitcoin Whitepaper ( https://bitcoin.org/bitcoin.pdf ) on page 7, there is a C code snippet. The style, font and the size is very neat and proper. How can I apply this kind of style for my java code in TexStudio ( I am using TexLive)?

This is my java code:

import java.math.*;

public class successAttackerProbability {

public double calculateSuccessProbability(double q, int z) {
int i, k;
double p = 1.0 - q;
double lambda = z * (q / p);
double sum = 1.0;
for (k = 0; k <= z; k++) {
double poisson = Math.pow(Math.E, -lambda);

for (i = 1; i <= k; i++)
poisson *= lambda / i;
sum -= poisson * (1 - Math.pow(q / p, z - k));

}
System.out.println(sum);
return sum;

}

public static void main(String[] args) {
successAttackerProbability successAttackerProbability = new successAttackerProbability();
successAttackerProbability.calculateSuccessProbability(0.1, 1); // q =0.1 and z = 1
successAttackerProbability.calculateSuccessProbability(0.1, 2);
// ..
successAttackerProbability.calculateSuccessProbability(0.1, 10);

}

}

• Why not? Could you add some code and descripe your problem with it? A small minimal working example (MWE), that illustrates your problem would be great. Aug 21 '18 at 12:40
• Well, I just did it with $\lstinputlisting{successAttackerProbability.java}$ , that's it, but I want the style like in the whitepaper. Aug 21 '18 at 12:54
• (*describe. ). - Does \lstset{basicstyle=\ttfamily} see daleifs answer solves your style problem? - Question without working code are harder to solve... compilable minimal code, starting with \documentclass and ending with \end{document}, helps a lot. Aug 21 '18 at 13:09
• It solves my problem regarding to font color. Now, I have to change the font. Do you have any examples? If not, I will google :) Aug 21 '18 at 13:12
• ttfamily is the font or i'm wrong? - RelatedSetting up specific font for use in listings Aug 21 '18 at 13:16

That looks like plain verbatim, or the listings package with the basicstyle set to contain \ttfamily.

Example

\documentclass[a4paper]{article}
\usepackage[margin=3cm]{geometry}
\usepackage{listings}
\lstset{
language=JAVA,
basicstyle=\ttfamily\small,
%  columns=flexible,
breaklines,
breakatwhitespace,
}
\begin{document}

\begin{lstlisting}
import java.math.*;

public class successAttackerProbability {

public double calculateSuccessProbability(double q, int z) {
int i, k;
double p = 1.0 - q;
double lambda = z * (q / p);
double sum = 1.0;
for (k = 0; k <= z; k++) {
double poisson = Math.pow(Math.E, -lambda);

for (i = 1; i <= k; i++)
poisson *= lambda / i;
sum -= poisson * (1 - Math.pow(q / p, z - k));

}
System.out.println(sum);
return sum;

}

public static void main(String[] args) {
successAttackerProbability successAttackerProbability = new successAttackerProbability();
successAttackerProbability.calculateSuccessProbability(0.1, 1); // q =0.1 and z = 1
successAttackerProbability.calculateSuccessProbability(0.1, 2);
// ..
successAttackerProbability.calculateSuccessProbability(0.1, 10);

}
\end{lstlisting}

\end{document}


See the listings package manual for details.