Zulip Chat Archive
Stream: new members
Topic: LaTeX Syntax rendering
Simon Daniel (Apr 05 2024 at 13:44):
Hello,
i try to render Lean code in LaTex.
i followed the instructions from the Manual but both options did not work for me.
When using listings some none ASCII Characters are rendered displaced for some reason.
And my Minted version is 2.7, i copied the first answer from this issue but lualatex fails to build it.
Simon Daniel (Apr 05 2024 at 13:46):
lean_syntax.png
this how a listings render the follwing code for me
\begin{lstlisting}[language=lean]
variable {μ: Type}
\end{lstlisting}
Eric Wieser (Apr 05 2024 at 14:41):
Based on https://github.com/pygments/pygments/milestones, I think if you wait a few days then lean4 support might be native to pygments
Eric Wieser (Apr 05 2024 at 14:41):
Of course, I thought this in january which is when their previous release was scheduled for, which never happened...
Eric Wieser (Apr 05 2024 at 14:42):
i copied the first answer from this issue but lualatex fails to build it.
I was able to get @Tobias Grosser's approach here to work with LuaLatex for my thesis
Eric Wieser (Apr 05 2024 at 14:44):
What I had there is
Simon Daniel (Apr 05 2024 at 15:02):
im confused to how even use this code. should i add it to my preamble after the minted import?
My thesis is due to this month so i probably cant wait for the native Lean support.
Eric Wieser (May 05 2024 at 21:58):
Sorry, I'm afraid I missed your message @Simon Daniel. If it's not too late, pygments just released a version with native lean4 support.
Simon Daniel (May 05 2024 at 22:06):
no worries, i worked out a half decent looking solution. Ill have a look into the pygments release if I need latex support in the future
Utensil Song (May 06 2024 at 03:43):
I have just given minted
and pygments
another try.
My preamble:
% I have the following to adjust outputdir and cachedir but in general they are not needed
% \usepackage[outputdir=../print,cachedir=_minted-cache]{minted}
\usepackage{minted}
\setminted{
fontsize=\footnotesize,
frame=single,
breaklines
}
Code block:
\begin{minted}[autogobble,mathescape]{lean4}
/--
Let $K$ be a division ring, a module $V$ over $K$ is a vector space
where being a module requires $V$ to be a commutative group over $+$.
-/
variable [DivisionRing K] [AddCommGroup V] [Module K V]
\end{minted}
Inline code:
\mintinline{lean4}{Algebra.toRingHom r}
It seems ok, except that mathescape
is not working like lstlisting
as claimed in minted
documentation.
Here is lstlisting
output:
While minted
gives:
but mathescape
actually works for Python:
Utensil Song (May 06 2024 at 03:50):
Another side note is that usually when publishing, one has no control over the Python environment or the pygments
version of the publisher, released or not. One could just use the master version of pygments
locally, then submit using \usepackage[frozencache,cachedir=_minted-cache]{minted}
.
Jireh Loreaux (May 06 2024 at 04:18):
@Utensil Song , I haven't tested it, but I think you want the option texcomments
instead of mathescape
. The former is for escaping to LaTeX within the code itself, the latter is for escaping to LaTeX in comments.
Utensil Song (May 06 2024 at 04:59):
Thanks, they are for different purposes, mathescape
has less side-effect IIUC.
I've pushed a fix following the C/C++ lexer. When using pygments @ git+https://github.com/utensil/pygments.git@lean4_mathescape
, mathescape
works as expected.
Eric Wieser (May 06 2024 at 08:27):
I thought mathescape was the more aggressive option, and will mess up $ symbols in syntax antiquotations?
Eric Wieser (May 06 2024 at 08:28):
(https://github.com/utensil/pygments/tree/lean4_mathescape for reference)
Eric Wieser (May 06 2024 at 08:32):
That patch looks strange to me; and it breaks nested comments
Utensil Song (May 06 2024 at 09:05):
Actually, the patch is mostly unnecessary, no need to change the lexer nor add __init__
it seems, they are byproducts of figuring out the problem.
The only actual effect is to treat doc strings as comments so that these options about comments will work. Alternatively, I can just use /-
comments instead of /--
.
And thanks for the heads-up and explanation, it seems that I should change to texcomments
after all.
Last updated: May 02 2025 at 03:31 UTC