Skip to content

Latest commit

 

History

History
45 lines (39 loc) · 1.67 KB

File metadata and controls

45 lines (39 loc) · 1.67 KB

LaTeX listings style for Racket programs

LaTeX code for (simple) syntax highlighting of the Racket programming language using the listings package.

Usage

Download the only LaTeX file in this repository. In your preamble, include it with \input{racket-lang.tex}. You can now use \begin{lstlisting}[language=racket] to typeset Racket code. The LaTeX file also defines a command \ir{...} for inline Racket code.

Example

This is a screenshot of the PDF generated by the LaTeX/Racket code below it.

Example of Racket code with syntax highlighting by the LaTeX package listings

The Racket code defines an abstract datatype for lambda expressions, and a procedure to check whether a variable occurs free in one. This example is copied from p. 46 of the book Essentials of Programming Languages (3rd ed.) by Friedman & Wand.

\documentclass{article}
\input{racket-lang.tex}

\begin{document}
\begin{lstlisting}[language=racket]
	;; Lambda calculus expressions
	(define-datatype lc-exp lc-exp?
		(var-exp
			(var symbol?))
		(lambda-exp
			(bound-var symbol?)
			(body lc-exp?))
		(app-exp
			(rator lc-exp?)
			(rand lc-exp?)))
	
	;; occurs-free? : Symbol * Lc-exp -> Bool
	(define occurs-free?
		(lambda (search-var exp)
			(cases lc-exp exp
				(var-exp (var) (eqv? var search-var))
			(lambda-exp (bound-var body)
				(and
					(not (eqv? search-var bound-var))
					(occurs-free? search-var body)))
			(app-exp (rator rand)
				(or
					(occurs-free? search-var rator)
					(occurs-free? search-var rand))))))
\end{lstlisting}
\end{document}