Closed kilasuelika closed 1 year ago
If you check the log file, you'll see (on Windows)
runsystem(pygmentize -l "gams.py:GAMSLexer -x" ...)...executed.
Thus the culprit is the quotes around gams.py:GAMSLexer -x
. The quotes are added in https://github.com/gpoore/minted/commit/25e1f300159c3e7848026688415141508248d113 and shipped with minted
v2.7 to resolve other problems. Overleaf's texlive 2022 still uses minted
v2.5 thus the different behaviors.
In example below a new boolean option custom lexer
is provided (which supersedes #176 in part). If set, it will pass cli option -x
to pygmentize
. Now you can use custom lexer through \begin{minted}[custom lexer]{gams.py:GAMSLexer}
. I don't speak GAMS so a custom Python lexer is used as an example.
% based on https://github.com/gpoore/minted/issues/176#issuecomment-695344998
\begin{filecontents}[noheader]{my_lexer.py}
from pygments.lexers.markup import TexLexer
from pygments.token import Keyword
class TexLexer2(TexLexer):
"""
Improved lexer for the TeX and LaTeX typesetting languages.
Character "@" is treated part of command names.
"""
TexLexer.tokens['root'][4] = (r'\\([a-zA-Z@]+|.)', Keyword, 'command')
\end{filecontents}
\documentclass{article}
\usepackage{minted}
\makeatletter
\ifwindows
\renewcommand{\minted@optlistcl@quote}[2]{%
\ifstrempty{#2}{\detokenize{#1}}{\detokenize{#1="#2"}}}
\else
\renewcommand{\minted@optlistcl@quote}[2]{%
\ifstrempty{#2}{\detokenize{#1}}{\detokenize{#1='#2'}}}
\fi
% similar to \minted@def@optcl@switch
\newcommand{\minted@def@optcl@novalue}[2]{%
\define@booleankey{minted@opt@g}{#1}%
{\minted@addto@optlistcl{\minted@optlistcl@g}{#2}{}%
\@namedef{minted@opt@g:#1}{true}}
{\@namedef{minted@opt@g:#1}{false}}
\define@booleankey{minted@opt@g@i}{#1}%
{\minted@addto@optlistcl{\minted@optlistcl@g@i}{#2}{}%
\@namedef{minted@opt@g@i:#1}{true}}
{\@namedef{minted@opt@g@i:#1}{false}}
\define@booleankey{minted@opt@lang}{#1}%
{\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang}{#2}{}%
\@namedef{minted@opt@lang\minted@lang:#1}{true}}
{\@namedef{minted@opt@lang\minted@lang:#1}{false}}
\define@booleankey{minted@opt@lang@i}{#1}%
{\minted@addto@optlistcl@lang{minted@optlistcl@lang\minted@lang @i}{#2}{}%
\@namedef{minted@opt@lang\minted@lang @i:#1}{true}}
{\@namedef{minted@opt@lang\minted@lang @i:#1}{false}}
\define@booleankey{minted@opt@cmd}{#1}%
{\minted@addto@optlistcl{\minted@optlistcl@cmd}{#2}{}%
\@namedef{minted@opt@cmd:#1}{true}}
{\@namedef{minted@opt@cmd:#1}{false}}
}
\minted@def@optcl@novalue{custom lexer}{-x}
\makeatother
\begin{document}
% runsystem(pygmentize -l 'tex' ...)
\begin{minted}[custom lexer=false]{tex}
% built-in tex lexer
\@namedef{cmd}{$a + \alpha$} x
\end{minted}
% runsystem(pygmentize -l 'my_lexer.py:TexLexer2' ... -x ...)
\begin{minted}[custom lexer]{my_lexer.py:TexLexer2}
% custom tex lexer which treats "@" as letter
\@namedef{cmd}{$a + \alpha$} y
\end{minted}
\end{document}
I encountered the same issue. @muzimuzhi, would it make sense to contribute your custom lexer
option to minted as a pull request?
@tobiasgrosser Hi Tobias, I'm really happy my patch helps people with their documents, but first I think it has to be decided how minted
will support custom lexers. I mean, the once-working \begin{minted}{gams.py:GAMSLexer -x}
is never an officially supported feature (thus its deprecation is not a bug).
In a more general way, I feel sth like \begin{minted}[custom lexer=gams.py:GAMSLexer]{fallback lang}
(with some python script to provide fallback logic) or \begin{minted}[custom lexer=gams.py]{GAMSLexer}
is better.
Sure. Now, admittedly the fact that there are uses of this in the wild makes the discussion if the loss of the other feature is a bug or not more academic 🙂. In an optimal world the -x syntax would just continue to work.
I'm having the same issue right now. A proper custom lexer
option would be great!
minted
version 3.0 is now under development, thanks to a grant from the TeX Users Group. It will include official support for custom lexers. It will also be able to be extended using Python, not just LaTeX macro programming, which will make possible many new lexer-related features. Progress on custom lexers will be tracked in #372. Initial beta releases of minted
version 3.0 are expected by early 2024.
I use a gams lexer (gams.py) in my project. Some months ago I found that my lexer no longer worked and it works before. At that time I thought it was a bug and maybe fixed. But recently I found that the same code can work on Overleaf so I think the problem doesn't lie in lexer. The simplest latex example would be
Error information:
I have updated all to the latest including
pygments
. I test installedpygments
withAnd it works perfectly.