diff --git a/source/11_chapter_3.md b/source/11_chapter_3.md index c3d41ac3..61fddea0 100644 --- a/source/11_chapter_3.md +++ b/source/11_chapter_3.md @@ -20,6 +20,11 @@ if mood == 'happy': print("I am a happy robot") ``` +Alternatively, you can also use LaTeX to create a code block as shown in the Java example below: +\lstinputlisting[style=javaCodeStyle, caption=Main.java]{source/code/HelloWorld.java} + +If you use `javaCodeStyle` as defined in the `preamble.tex`, it is best to keep the maximum line length in the source code at 80 characters. + ### Subsection 2 This is the second part of the methodology. Proin tincidunt odio non sem mollis tristique. Fusce pharetra accumsan volutpat. In nec mauris vel orci rutrum dapibus nec ac nibh. Praesent malesuada sagittis nulla, eget commodo mauris ultricies eget. Suspendisse iaculis finibus ligula. diff --git a/source/code/HelloWorld.java b/source/code/HelloWorld.java new file mode 100644 index 00000000..d66baa05 --- /dev/null +++ b/source/code/HelloWorld.java @@ -0,0 +1,9 @@ +/** + * Hello, world - example in Java. + */ +public class Main{ + // says hello to the world + public static void main(String[] args) { + System.out.println("Hello, world!"); + } +} \ No newline at end of file diff --git a/style/preamble.tex b/style/preamble.tex index aabca838..ec551dbc 100644 --- a/style/preamble.tex +++ b/style/preamble.tex @@ -40,6 +40,40 @@ %Following line controls size of figure legends % \setsansfont[Scale=1.2]{Optima Regular} +% CODE BLOCKS +\usepackage[utf8]{inputenc} +\usepackage{listings} +\usepackage{color} + +% JAVA CODE BLOCKS +\definecolor{backcolour}{RGB}{242,242,242} +\definecolor{javared}{rgb}{0.6,0,0} +\definecolor{javagreen}{rgb}{0.25,0.5,0.35} +\definecolor{javapurple}{rgb}{0.5,0,0.35} +\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} + +\lstdefinestyle{javaCodeStyle}{ + language=Java, % the language of the code + backgroundcolor=\color{backcolour}, % choose the background color; you must add \usepackage{color} or \usepackage{xcolor} + basicstyle=\fontsize{10}{8}\sffamily, + breakatwhitespace=false, + breaklines=true, + keywordstyle=\color{javapurple}\bfseries, + stringstyle=\color{javared}, + commentstyle=\color{javagreen}, + morecomment=[s][\color{javadocblue}]{/**}{*/}, + captionpos=t, % sets the caption-position to bottom + frame=single, % adds a frame around the code + numbers=left, + numbersep=10pt, % margin between number and code block + keepspaces=true, % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible) + columns=fullflexible, + showspaces=false, % show spaces everywhere adding particular underscores; it overrides 'showstringspaces' + showstringspaces=false, % underline spaces within strings only + showtabs=false, % show tabs within strings adding particular underscores + tabsize=2 % sets default tabsize to 2 spaces +} + %Attempt to set math size %First size must match the text size in the document or command will not work %\DeclareMathSizes{display size}{text size}{script size}{scriptscript size}.