# 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:** 2

<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, 1:52pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/21 "2020-09-24T13:52:56Z")

</div>

Thanks a lot. Yes, increasing the font size in the tex preamble helps a little bit. But it is not enough for slides. Let’s see if the guys that developed the original tex package have this point in mind.

---

<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, 5:52pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/22 "2020-09-24T17:52:15Z")

</div>

Found how to do it: You need to change the preamble to (assuming you are using the example I provided):

```julia
\def\jlbasicfont{\ttfamily\large\selectfont} % Add this line
\lstdefinestyle{code}{
  columns=fullflexible, % Add this to the lstdefinestyle
  keepspaces=true, % Add this line also
  language=Julia,
  xleftmargin=1.5cm,
  showstringspaces=false,
  % Comment the following three lines to remove colors
  keywordstyle=\color{blue},
  commentstyle=\color{gray},
  identifierstyle=\color[RGB]{0,102,0}
}
\lstnewenvironment{code}{\lstset{style=code}}{} % I also updated this, which is simpler now
\newcommand{\codefile}[1]{\lstinputlisting[style=code]{#1}}
\newcommand{\codelink}[1]{\lstinputlisting[style=code]{#1}
            \begin{center}
            \href{http://m3g.iqm.unicamp.br/main/didatico/simulacoes2/#1}
                 {\textcolor{blue}{~\hspace{6.5cm}[Clique aqui para baixar o código]}}
            \end{center}}
\newcommand{\inline}{\jlinl}
\newcommand{\il}{\jlinl}

```

I will update the example in the github repository to include this (edit: done).

---

<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, 8:10pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/23 "2020-09-24T20:10:51Z")

</div>

Great job. It works perfectly well.

But the funny thing is that there must be something wrong with the jlcode.sty package, because the fonts in default mode are smaller than using the footnotesize in your new commands. If those fonts were the “normalsize”, then they appear to be of limited use. In your file, “large” is really large, and we can play with different sizes very easily.

Thanks a lot.

---

<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, 9:24pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/24 "2020-09-24T21:24:32Z")

</div>

That is true, the font sizes seem to be shifted. I am using `\footnotesize` to get fonts of good size in a document with overall 12pt fonts.

Edit: the default size of the package is `\scriptsize`.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 5, 2021, 9:58am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/25 "2021-03-05T09:58:11Z")

</div>

We are also using `jlcode` in our paper and noticed that spacing is sometimes off, especially when having `<:` (with a space before and one space after) in the code, the second space (i.e. the one after `<:`) is sometimes gobbled/missing. Anyone expieriencing similar spacing issues?

_edit:_ seems like `keepspaces=true` fixed my issue (set with `\lstset{...}` globally).

---

<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:** [March 5, 2021, 12:11pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/26 "2021-03-05T12:11:01Z")

</div>

> [@kellertuer](#):
>
> seems like `keepspaces=true` fixed my issue

Indeed, this is one of the options I have added to the macro commands [here](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/16). There are other options there that solve other small issues that have appeared.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [March 5, 2021, 12:27pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/27 "2021-03-05T12:27:06Z")

</div>

…I might just have taken a look at your settings and used that one 😉

Also, a while back, I contributed a few more utf8 symbols to `jlcode.sty` (calligraphic and bold math symbols), I think the developer is always happy to have the list of utf8 symbols extended in case you’re missing some.

---

<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:** [March 17, 2021, 7:54pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/28 "2021-03-17T19:54:10Z")

</div>

The last time (font size), @lmiq saved my skin. Thanks for that.

The problem I am having now is that I have to code using a lot of Unicode characters. I have noticed that in the jlcode site, it says that a lot of Julia Unicode characters are already supported by the package. However, if I try something like this:

```julia
mutable struct FictitiousPlay
    𝒫 # simple game
    i # agent index
    N # array of action count dictionaries
    πi # current policy
end

function FictitiousPlay(𝒫::SimpleGame, i)
    N = [Dict(aj => 1 for aj in 𝒫.𝒜[j]) for j in 𝒫.ℐ]
    πi = SimpleGamePolicy(ai => 1.0 for ai in 𝒫.𝒜[i])
    return FictitiousPlay(𝒫, i, N, πi)
end

```

the LaTeX compilation stops at line two of the code with the error:

`Package inputenc Error: Invalid UTF-8 byte sequence. 𝒫`

In fact, none of the script characters can be displayed by my text editor (TexStudio). Everything works perfectly well with jlcode; the only problem I have is with the script Unicode characters. Does anybody know how can I overcome this problem?

Thanks.

---

<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:** [March 17, 2021, 7:58pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/29 "2021-03-17T19:58:21Z")

</div>

> [@VivMendes](#):
>
> know how can I overcome this problem?

Unfortunately I cannot help with that. And I have tried quite a bit… there is some support for some unicode characters in some latex packages, but I could not find any reliable solution. I finally ended not using unicode in the material I was producing.

---

<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:** [March 17, 2021, 8:04pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/30 "2021-03-17T20:04:42Z")

</div>

@lmiq Thanks anyway. Probably that’s what I have to do as well. But Julia is so great on this side as well, that it’s a pity if many of the Unicode characters cannot be passed on to the LaTeX world.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 18, 2021, 2:17pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/31 "2021-03-18T14:17:59Z")

</div>

> [@VivMendes](#):
>
> …it’s a pity if many of the Unicode characters cannot be passed on to the LaTeX world

I think getting all the glories of Unicode into a code listing in LaTeX isn’t impossible. I managed this:

 ![Screenshot 2021-03-18 at 14.13.35](https://global.discourse-cdn.com/julialang/original/3X/3/2/32ebd4354ed60e90d6e5355eae7b35f1247c82c6.png)

However, as a non-LaTeX-er I found it quite difficult to adjust all the settings and options in order to get it to work correctly. Adjusting the engines, the encodings, the packages, the command-line options, the font specifications - I think it’s all a lot harder than it should be.

---

<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:** [March 18, 2021, 10:43pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/32 "2021-03-18T22:43:19Z")

</div>

@cormullion, by the way, I was aware of your wonderful package JuliaMono. Congrats to you and your collaborators on such a nice piece of software. This package is the factor that made me raise the question above. After @cormullion’s comments, I decided to take a second shot at “minted.sty”.

I had previously tried “minted,” but my text editor (TexStudio) kept boycotting my efforts. Kept saying that I had to use the “–shell escape flag”, which I did, but with no luck. I do have pygments.py 2.8.1 installed, also an updated minted.sty, and tried both ways:

(i) in the config of TexStudio:  
pdflatex -synctex=1 -interaction=nonstopmode --shell-escape %.tex  
(ii) and with the top document in line “magic” comment:  
%!TeX TXS-program:compile = txs:///pdflatex/[–shell-escape]

I did not understand what was going on, and so I opted for the jlcode.sty. It looks great but unfortunately is not very versatile with Unicode characters.

Today I spent some time figuring out the problem with the error coming out from “minted”. I checked the minted documentation and found out that it requires another package: shellesc.sty, which is not part of the MiKTeX distribution anymore, apparently, for security reasons. There are many entries about this problem in Stack forums, so the problem is not about my particular LaTeX editor. JuliaMono is such a great package, I will not give up.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 19, 2021, 3:39am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/33 "2021-03-19T03:39:53Z")

</div>

Is pygments in your PATH? Minted expects that it is.  
And _maybe_ this will help [pygments and Minted](https://tex.stackexchange.com/questions/494711/minted-error-missing-pygments-error)

---

<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:** [March 19, 2021, 12:03pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/34 "2021-03-19T12:03:22Z")

</div>

@JeffreySarnoff yes I do. There is actually an issue on Stack 6 years ago about this issue [here](https://superuser.com/questions/816340/minted-cannot-find-pygmentize-in-texstudio-on-windows-7/1113138), in which one of the contributors clarified the PATH problem extremely well. There are a large number of similar questions on Stack, so I am not the only one with this pain on Windows 10.

By the way, I raised the point in another Stack question yesterday, and David Carlisle explained that my last paragraph above does not make sense: the shellesc.sty is part of the latex-tools.sty package, which is indeed included in the standard distribution of MiKTeX.

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 19, 2021, 12:23pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/35 "2021-03-19T12:23:26Z")

</div>

Thanks for the kind words!

My very limited experience of LaTeX has filled me with admiration for people who can actually make it work and then rely on it for their livelihoods… To me it all seems like a sadistic adventure game where you have to overcome many bizarre obstacles before getting your reward… 😅

Anyway, to get my examples working I do the following:

1 Use TeXShop, which comes with TeXLive. Assume UTF8.  
2 Use `lualatex` by adding this command to the preferences.

![texshop-1](https://global.discourse-cdn.com/julialang/original/3X/1/c/1cca14e1c9f3d87b1299ea102776168102160d61.png)

3 Select LaTeX rather than LuaLaTeX from the drop down:

![texshop-2](https://global.discourse-cdn.com/julialang/original/3X/4/6/46155f540432d960cc537dfa0143a354792bab7f.png)

4 Start a document like this

```julia
\documentclass[onecolumn]{article}
\usepackage{fontspec}
\usepackage{minted}
\usepackage{amsmath}
\usepackage{graphicx}

```

5 Specify a monospaced font and ‘ligatures’ like this:

```julia
\newfontfamily \JuliaMono {JuliaMono-Light.ttf}[
    Path = /Users/me/Library/Fonts/,
    Extension = .ttf
    ]
\newfontface \JuliaMonoLight{JuliaMono-Light}
\setmonofont{JuliaMonoLight}[
    Contextuals=Alternate
]

```

6 Insert code like this:

```julia
\begin{minted}[
	label = code example,
	linenos,
	breaklines,
	style = paraiso,
	fontsize=\small,
	frame = single
	]{julia}

$ some code
mutable struct FictitiousPlay
    𝒫 # simple game
    i # agent index
    N # array of action count dictionaries
    πi # current policy
end

```

7 And then click Typeset, invoking the names of various luck-bringing spirits.

Hopefully I get the result shown above. Well, it works today. 😹

---

<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:** [March 19, 2021, 1:57pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/36 "2021-03-19T13:57:35Z")

</div>

That is great. Using JuliaMono and `lualatex` seems to get a lot of unicode characters right.

I have updated the files here:

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

Thanks @cormullion !

Now it looks like this:

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

Unfortunately, there seem to be quite a few situations in which the unicode characters get messed up. Not the characters themselves, but their position in the text. Needs improvement ☹

---

<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:** [March 19, 2021, 7:39pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/37 "2021-03-19T19:39:25Z")

</div>

@cormullion your sense of humor looks as good as your JuliaMono package. Your description of LaTeXers is the best I have ever seen. 😆 I will use it to convince my students.

I will have a go and try to replicate your output. I suppose your ligatures come before the `\begin{document}` declaration, right?!

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [March 19, 2021, 7:59pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/38 "2021-03-19T19:59:12Z")

</div>

I put the font declarations after `\begin{document}` , but it appears to work…

LaTeX is challenging, but Microsoft Word is frustrating… 😂

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [March 19, 2021, 9:07pm UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/39 "2021-03-19T21:07:43Z")

</div>

> [@mohamed82008](#):
>
> automatic display of figures with captions, etc.

This is precisely what I’m working on. Still a work in progress though: [Books.jl](https://github.com/rikhuijzer/Books.jl). See the [homepage](https://books.huijzer.xyz/) for a link to the PDF.

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [March 21, 2021, 10:56am UTC](https://discourse.julialang.org/t/what-do-you-use-to-embed-julia-code-in-latex/23992/40 "2021-03-21T10:56:39Z")

</div>

> [@cormullion](#):
>
> invoking the names of various luck-bringing spirits.

I’m going to need the names of those spirits!

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

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