31

I'd like to typeset some Julia code, syntax highlighting and all, with the listings package. For information, the Julia language is a strong contender for MATLAB, and is free and open-source.

However, listings doesn't come with a language definition for Julia. I've found a Colorer specification for the language here. How can I adapt it in order to properly highlight Julia code with the listings package?

For illustration purposes, here is an MWE with some a Julia code sample (adapted from http://julialang.org) embedded in an lstlisting environment:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{listings}

\lstset{basicstyle=\ttfamily}

\begin{document}
\begin{lstlisting}
#= This is a code sample for the Julia language
(adapted from http://julialang.org) =#
function mandel(z)
    c = z
    maxiter = 80
    for n = 1:maxiter
        if abs(z) > 2
            return n-1
        end
        z = z^2 + c
    end
    return maxiter
end

function helloworld()
    println("Hello, World!") # Bye bye, MATLAB!
end

function randmatstat(t)
    n = 5
    v = zeros(t)
    w = zeros(t)
    for i = 1:t
        a = randn(n,n)
        b = randn(n,n)
        c = randn(n,n)
        d = randn(n,n)
        P = [a b c d]
        Q = [a b; c d]
        v[i] = trace((P.'*P)^4)
        w[i] = trace((Q.'*Q)^4)
    end
    std(v)/mean(v), std(w)/mean(w)
end
\end{lstlisting}
\end{document}

2 Answers 2

30

You'll find a listings language definition for the Julia language in the code below.

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}

%%
%% Julia definition (c) 2014 Jubobs
%%
\lstdefinelanguage{Julia}%
  {morekeywords={abstract,break,case,catch,const,continue,do,else,elseif,%
      end,export,false,for,function,immutable,import,importall,if,in,%
      macro,module,otherwise,quote,return,switch,true,try,type,typealias,%
      using,while},%
   sensitive=true,%
   alsoother={$},%
   morecomment=[l]\#,%
   morecomment=[n]{\#=}{=\#},%
   morestring=[s]{"}{"},%
   morestring=[m]{'}{'},%
}[keywords,comments,strings]%

\lstset{%
    language         = Julia,
    basicstyle       = \ttfamily,
    keywordstyle     = \bfseries\color{blue},
    stringstyle      = \color{magenta},
    commentstyle     = \color{ForestGreen},
    showstringspaces = false,
}

\begin{document}
\begin{lstlisting}
#= This is a code sample for the Julia language
(adapted from http://julialang.org) =#
function mandel(z)
    c = z
    maxiter = 80
    for n = 1:maxiter
        if abs(z) > 2
            return n-1
        end
        z = z^2 + c
    end
    return maxiter
end

function helloworld()
    println("Hello, World!") # Bye bye, MATLAB!
end

function randmatstat(t)
    n = 5
    v = zeros(t)
    w = zeros(t)
    for i = 1:t
        a = randn(n,n)
        b = randn(n,n)
        c = randn(n,n)
        d = randn(n,n)
        P = [a b c d]
        Q = [a b; c d]
        v[i] = trace((P.'*P)^4)
        w[i] = trace((Q.'*Q)^4)
    end
    std(v)/mean(v), std(w)/mean(w)
end
\end{lstlisting}
\end{document}
1
  • 2
    "begin" should be included in the keywords. Jan 4, 2015 at 14:41
14

For those of you coming from google and having found this page:

Inspired by Jubobs three year old post I have recently created a julia language definition for the listings package that tries to display julia code the same way as in the official online documentation: https://docs.julialang.org/en/stable/

So if you are intrested, go and check it out: https://github.com/wg030/jlcode

1
  • 6
    Great! Have you asked the author of listings if he wants to include the language definition directly into the code or is it intended that it is separate? Jan 28, 2018 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.