Hello, I'm writing a project report with my Julia code in appendix. Since julia understand latex and uses mathematical symbol, I'd like to be able to enter maths mode in the lstlisting.
<= and => seem to work but not for \in etc...
Does anyone have an idea ?
Font is cascadia code, same as vscode's default so I assumed there was no problem of enconding
Compiler is lualatex
Thank you
#Maths mode in lstlisting
1 messages · Page 1 of 1 (latest)
I'm not sure I understand the issue. Is it that, in the second image, >= and <= look like ≥ and ≤, but in doesn't look like ∈?
Correct
Thing is in Julia you can add maths symbols e.g. you type \in then press tab and it will convert to the math symbol
Which one is easily understood by the language
But I cannot write it this way in the latex document since I can't be in maths mode inside lstlisting
Since you are using lualatex, maybe your font contains U+2208 (∈) in text mode?
(just checked, looks like it doesn't, but maybe your could borrow the symbol from elsewhere...)
that conversion is an editor feature. It's actually just putting a unicode character there, and then when lua runs the code it can properly interpret unicode
On the image on the right it's not doing any conversions, including <= and >=. It just looks like that because you have a font with ligatures I think
(you can tell because those glyphs are wider than in the image on the left)
you might have some luck with this https://tex.stackexchange.com/a/378606
basically you can "teach" lstlisting how to deal with the unicode characters (by entering math mode and just using it there)
Indeed, good spot. It just occurs to me now that the minted package might be more suited for the task: I'm pretty sure it supports unicode input out of the box, contrarily to lstlistings. It might also allow syntax highlighting in the final document since pygmentize ships relevant lexers.
Minimal examples with minted
\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{unicode-math}
\setmainfont{Fira Sans}
\setmonofont{Fira Code}
\setmathfont{Fira Math}
\usepackage{minted} % Requires shell escape
\begin{document}
\begin{minted}{julia}
s(t) ≥ π;
\end{minted}
\end{document}
or just a plain verbatim,
\documentclass{article}
\usepackage[a6paper]{geometry}
\usepackage{unicode-math}
\setmainfont{Fira Sans}
\setmonofont{Fira Code}
\setmathfont{Fira Math}
\begin{document}
\begin{verbatim}
s(t) ≥ π;
\end{verbatim}
\end{document}
Examples are compiled with latexmk -pdflua and optionally -shell-escape when using minted.
As you can see, both environments support unicode out of the box with lualatex.
I'm not sure. I've had issues with it displaying greek characters before
Might be a font issue though, since I was just using the default