# What do you use to embed Julia code in LaTeX?

**URL:** https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992
**Category:** General Usage
**Created:** [May 8, 2019, 10:28am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992 "2019-05-08T10:28:08Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 8, 2019, 10:28am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/1 "2019-05-08T10:28:08Z")

</div>

I wonder if anyone here has interesting tips or tricks for embedding Julia code in LaTeX, e.g. fancy color schemes, running code inline for presentations, automatic display of figures with captions, etc.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 8, 2019, 10:51am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/2 "2019-05-08T10:51:21Z")

</div>

For julia in latex I use minted.  
To run julia in presentations I use the “export to reveal.js slides” option in Jupyter

Here is a short wiki I created for my coworkers to include julia in latex

# Minted

It is possible to include julia code using the package minted [GitHub - gpoore/minted: minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.](https://github.com/gpoore/minted)

## Prerequisites

pygmentize is required, if it is not installed, try running _sudo pip install pygments_

Get latest version of _minted.sty_ from the github repo above. Put it in you texlive package folder or in the current working directory. Minted has other prerequisites, but those should be installed already.

## A small example

- \begin{minted}[breaklines,escapeinside=||,mathescape=true, linenos, numbersep=3pt, gobble=2, frame=lines, fontsize=\small, framesep=2mm]{julia}
  - Your awesome julia code here\end{minted}

[minted.tex](https://www.control.lth.se/media/Staff/FredrikBaggeCarlson/minted.tex) [minted.pdf](https://www.control.lth.se/media/Staff/FredrikBaggeCarlson/minted.pdf)

## Inclusion of unicode characters

With julia, one has the option of including most unicode characters in the code. For a copy-paste-kind of strategy to work, I have found the following two packages useful

\usepackage[mathletters]{ucs} \usepackage[utf8x]{inputenc}

With these packages, unicode characters in the input are handled quite well. At least if one builds with latexmk and pdflatex.

With luatex, use the following instead \usepackage[T1]{fontenc} \usepackage{unicode-math}

See [https://tex.stackexchange.com/questions/251446/problem-with-math-symbols-unicode-math](https://tex.stackexchange.com/questions/251446/problem-with-math-symbols-unicode-math) for info regarding setting a math font. The following selects the font that is default in Atom

\usepackage{fontspec}\setmonofont{[DejaVu](https://wiki.control.lth.se/DejaVu) Sans Mono}[Scale=0.8]

Note, there might be some conflicts between unicode-math and the department fonts. If you get errors, try removing all other font choices, such as schoolbook, newtxtext, newtxmath,inconsolata,fourier, and see if the erros go away.

## Issues

### Beamer

Frames must be declared fragile for minted to work

\begin{frame}[fragile]

### Other issues

Minted has issues if the build output is stored in a subdirectory and not in the working directory, use \usepackage[outputdir=build]{minted}

If _pygments_ is installed but _pygmentize_ can not be found, reinstall _pygments_ .

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 8, 2019, 10:57am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/3 "2019-05-08T10:57:05Z")

</div>

I also created a minted style to resemble atomone syntax highlighting in atom

```python
# -*- coding: utf-8 -*-
"""
Command to clear build directory, copy this file to the correct folder and then build a test document
rm /tmp/test.pdf; rm -rf /tmp/build/*; sudo cp ~/atomone.py /usr/lib/python3.6/site-packages/pygments/styles/.;lualatex --shell-escape --output-directory=build test.tex; okular build/test.pdf
"""

from pygments.style import Style
from pygments.token import Keyword, Name, Comment, String, Error, \
     Number, Operator, Generic, Whitespace

class AtomoneStyle(Style):
    """
    Bagge's style
    """

    background_color = '#f0f3f3'

    styles = {
        Whitespace: '#bbbbbb',
        Comment: 'italic #A2A2A2',
        Comment.Preproc: 'noitalic #A2A2A2',
        Comment.Special: 'bold',

        Keyword: 'bold #AD00AC', # function, for, if
        Keyword.Pseudo: 'nobold',
        Keyword.Type: '#008491',

        Operator: '#555555',
        Operator.Word: 'bold #FF0000',# Does not seem to be used

        Name.Builtin: '#008491', # Both these should be set to same
        Name.Function: '#008491', # Both these should be set to same
        Name.Class: 'bold #000000',
        Name.Namespace: 'bold #FF0000',# Does not seem to be used
        Name.Exception: 'bold #CC0000',
        Name.Variable: '#000000',
        Name.Constant: '#000000',
        Name.Label: '#FF0000',
        Name.Entity: 'bold #999999',
        Name.Attribute: '#330000',
        Name.Tag: 'bold #FF0000',
        Name.Decorator: '#9999FF',

        String: '#97C417', # This is used for strings
        String.Doc: 'italic',
        String.Interpol: '#AA0000', # Used for itnerpolations
        String.Escape: 'bold #CC3300',
        String.Regex: '#33AAAA',
        String.Symbol: '#FFCC33',
        String.Other: '#CC3300',

        Number: '#FF6600',

        Generic.Heading: 'bold #1A3300',
        Generic.Subheading: 'bold #003300',
        Generic.Deleted: 'border:#CC0000 bg:#FFCCCC',
        Generic.Inserted: 'border:#00CC00 bg:#CCFFCC',
        Generic.Error: '#D60000',
        Generic.Emph: 'italic',
        Generic.Strong: 'bold',
        Generic.Prompt: 'bold #078600',
        Generic.Output: '#787878',
        Generic.Traceback: '#787878',

        Error: 'bg:#FFAAAA #AA0000'
    }

```

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 8, 2019, 11:01am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/4 "2019-05-08T11:01:06Z")

</div>

Very cool! Thank your for response. I will play around with minted some more and try your syntax highlighting.

---

<div class="post-metadata">

### Author: ![JonasIsensee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonasisensee/32/4704_2.png) [@JonasIsensee](https://discourse.julialang.org/u/JonasIsensee)
#### Post date: [May 8, 2019, 12:01pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/5 "2019-05-08T12:01:52Z")

</div>

Also see

> [@Typesetting Julia Code in Latex Documents (Unicode)](https://discourse.julialang.org/t/typesetting-julia-code-in-latex-documents-unicode/10176):
>
> Hey there, I love how I can use Unicode in Julia programming. When trying to write my report about my work I encountered a problem, though. Regular LateX is not compatible with Unicode characters. This is not a problem in equations as you can use \alpha and such but this doesn’t work properly with code embedded through listings or minted. Luckily there is a fix for people who don’t want to give up and stop using Unicode characters while coding. This is a report of what I did to make it wor…

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 8, 2019, 5:00pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/6 "2019-05-08T17:00:48Z")

</div>

@baggepinnen in your atomone style, what would be the way to make `=` and function names at call site differently colored from black?

Edit: that’s probably a language support issue?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 8, 2019, 5:10pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/7 "2019-05-08T17:10:07Z")

</div>

I have honestly no idea, it was very much a trial and error endeavor for me. I know I managed to install another julia syntax ruleset that works better, I’ll look around and see if I can remember where I got that from.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 8, 2019, 5:24pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/8 "2019-05-08T17:24:50Z")

</div>

You need to find a new _lexer_  
ref: [Available lexers — Pygments](http://pygments.org/docs/lexers/#lexers-for-the-julia-language)  
This is the one I use  
[https://github.com/sisl/pygments-julia](https://github.com/sisl/pygments-julia)  
After that, use `julia1` as language for the minted environment

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [May 8, 2019, 5:33pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/9 "2019-05-08T17:33:10Z")

</div>

Awesome, works like a charm! Thank you.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [May 8, 2019, 5:35pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/10 "2019-05-08T17:35:38Z")

</div>

You can also use `jlcon1` as language for code copy-pasted from the REPL, to make output render nicely etc.

---

<div class="post-metadata">

### Author: ![david\_m](https://avatars.discourse-cdn.com/v4/letter/d/c37758/32.png) [@david\_m](https://discourse.julialang.org/u/david_m)
#### Post date: [August 29, 2020, 11:55am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/11 "2020-08-29T11:55:03Z")

</div>

Pygments version 2.6.1 calls this language _jlcon_ .

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [August 29, 2020, 12:00pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/12 "2020-08-29T12:00:21Z")

</div>

So does older versions, but with the custom lexer I linked in [the post above](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/8), you need to append the `1` on the end to make user of the new lexer.

[Reference](https://github.com/sisl/pygments-julia/blob/16d1423d8e1b2dc13d27ad875deb27fb5f91b79f/pygments_julia/ __init__.py#L225)

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [August 29, 2020, 3:41pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/13 "2020-08-29T15:41:14Z")

</div>

I’m having good luck with this

[https://github.com/wg030/jlcode](https://github.com/wg030/jlcode)

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 30, 2020, 1:19am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/14 "2020-08-30T01:19:49Z")

</div>

I too use the jlcode package, and I have added some newcommands. Works like this:

```julia
\begin{code}
using DelimitedFiles
data = readdlm("file.dat")
x = data[:,1]
y = data[:,2]
\end{code}

```

which displays something like:

![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/39fef1dd4448f0b62506fbd7dc52760efb2f62ee.png)

or

```julia
\codelink{./julia/kineticmodel.jl}

```

to display:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/7/e7744925218dcc50c608f025e120cc57fcd20e33.png)

where “[Clique aqui para baixar o código]” means “[Click here to download the code]”.

Examples files are available at: [GitHub - m3g/jlcode\_example: Example of the use of jlcode and the JuliaMono font to write Julia code in LaTeX](https://github.com/m3g/jlcode_example)

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [August 30, 2020, 10:02am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/15 "2020-08-30T10:02:20Z")

</div>

Nice work! Thanks @lmiq

If I want to use \begin{code} … without the colors (print book where colors are expensive) can I just delete

keywordstyle=\color{blue},  
commentstyle=\color{gray},  
identifierstyle=\color[RGB]{0,102,0}\textbf,

or do I need to changed the colors? Right now I use

\usepackage[nocolors]{jlcode}

Would that do the job?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 30, 2020, 2:00pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/16 "2020-08-30T14:00:46Z")

</div>

You have to both add the [nocolors] option and comment those lines to get a completely grayscale output.

I have added a minimal working example (with some fixes to the code above), here:

[https://github.com/m3g/jlcode\_example](https://github.com/m3g/jlcode_example)

---

<div class="post-metadata">

### Author: ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)
#### Post date: [August 30, 2020, 4:20pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/17 "2020-08-30T16:20:14Z")

</div>

You sir, just saved me some trouble over the next six months. Thanks again.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 30, 2020, 5:04pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/18 "2020-08-30T17:04:06Z")

</div>

All the merits go to the wg030 github user, what I provide there is just a very simple example of what is in the user manual.

---

<div class="post-metadata">

### Author: ![VivMendes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vivmendes/32/16477_2.png) [@VivMendes](https://discourse.julialang.org/u/VivMendes)
#### Post date: [September 24, 2020, 11:08am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/19 "2020-09-24T11:08:50Z")

</div>

Wonderful stuff. I checked in the jlcode.sty file if there was an easy way to increase the size of the fonts. In some circumstances, we may need larger fonts for slides. But I did not figure out how to do it. Do you have a clue? Help would be very appreciated.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [September 24, 2020, 1:13pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/20 "2020-09-24T13:13:19Z")

</div>

Not really, I tried I few things but I can only change the size with proper results if changing the size of the fonts of the complete document in `\documentclass[12pt]{article}`, for example.

[Next page](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992.md?page=2)
