What do you use to embed Julia code in LaTeX?

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.

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

\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).

1 Like

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.

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.

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).

Indeed, this is one of the options I have added to the macro commands here. There are other options there that solve other small issues that have appeared.

4 Likes

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

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.

1 Like

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:

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.

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.

@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.

2 Likes

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

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.

8 Likes

@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.

1 Like

Is pygments in your PATH? Minted expects that it is.
And maybe this will help pygments and Minted

1 Like

@JeffreySarnoff yes I do. There is actually an issue on Stack 6 years ago about this issue here, 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.

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… :sweat_smile:

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

3 Select LaTeX rather than LuaLaTeX from the drop down:

texshop-2

4 Start a document like this

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

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

\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:

\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. :joy_cat:

8 Likes

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

I have updated the files here:

Thanks @cormullion !

Now it looks like this:

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 :frowning:

10 Likes

@cormullion your sense of humor looks as good as your JuliaMono package. Your description of LaTeXers is the best I have ever seen. :laughing: 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?!

1 Like

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

LaTeX is challenging, but Microsoft Word is frustrating… :joy:

1 Like

This is precisely what I’m working on. Still a work in progress though: Books.jl. See the homepage for a link to the PDF.

6 Likes

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

1 Like