Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

Code in LaTex

Published on

Updated on

2 minute reading time

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

I am currently working on a paper in LaTex and wanted to include some source code in it. I didn’t want to use the default verbatim environment since I wanted to include syntax highlighting as well. Luckily, the listings package is an easy and extensible way to include source code inside LaTex documents. To speak first of its extensibility, here is a subset of the arguments that it can possibly take:

Argument Description
aboveskip Amount of space to include above code.
backgroundcolor Background color
basicstyle Font-style of code (Color & Size)
belowskip Amount of space to include below code.
breakatwhitespace Only break at whitespace (boolean)
breaklines Automatic Line Breaking (boolean)
commentstyle Font-style of comments (Color & Size)
frame Type of frame: l for left, r for right, t for top, b for bottom; can use combination of letters or single.
keywordstyle Font-style of keywords (Color & Size)
language Language of code (for highlighting purposes)
numbers Where to put the numbers: none, left, or right.
numbersep How far the line-numbers are from the code.
numberstyle Font-style of numbers (Color & Size)
stringstyle Font-style of strings in code (Color & Size)
tabsize Default tab size in terms of spaces.
title Show filename included in \lstinputlistings or caption.

Here is an example of code that I used in a paper

\definecolor{keyblue}{rgb}{0.1, 0.1, 0.6}


def greeting():
	return "Hello!"

# Printing the greeting to the screen


Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :