gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.73k stars 125 forks source link

clearing `\verbatim@nolig@list` does not allow for code ligatures #367

Closed mbertucci47 closed 1 year ago

mbertucci47 commented 1 year ago

If using fontspec and a monospaced font with code ligatures such as JetBrains Mono or Fira Code, then redefining \verbatim@nolig@list to be empty allows for the code ligatures in a verbatim or Verbatim (fancyvrb) environment. However this is not true for minted environments (or commands, etc.). Compile the following with xelatex or lualatex with Renderer=Harfbuzz (see this issue) to see the problem:

\documentclass{article}
\usepackage{fontspec,minted}
\setmonofont{JetBrains Mono}%[Renderer=HarfBuzz] % for lualatex

\makeatletter
\def\verbatim@nolig@list{}
\makeatother

\begin{document}

\begin{itemize}
\item \verb|\begin{verbatim}|
\begin{verbatim}
<> |=> <=| ++ -> <-
<-> <!-- ~> <~ ~~>
<~~ ->> <<- --> <--
>-> <-< /= -> <= <=>
=>> <<= ==> <== <==>
>=> <=< ## ### |> <|
</ /> #? ]#
\end{verbatim}

\item \verb|\begin{Verbatim}| (fancyvrb)
\begin{Verbatim}
<> |=> <=| ++ -> <-
<-> <!-- ~> <~ ~~>
<~~ ->> <<- --> <--
>-> <-< /= -> <= <=>
=>> <<= ==> <== <==>
>=> <=< ## ### |> <|
</ /> #? ]#
\end{Verbatim}

\item \verb|\begin{minted}{bash}|
\begin{minted}{bash}
<> |=> <=| ++ -> <-
<-> <!-- ~> <~ ~~>
<~~ ->> <<- --> <--
>-> <-< /= -> <= <=>
=>> <<= ==> <== <==>
>=> <=< ## ### |> <|
</ /> #? ]#
\end{minted}

\item \verb|\begin{minted}{python}|
\begin{minted}{python}
<> |=> <=| ++ -> <-
<-> <!-- ~> <~ ~~>
<~~ ->> <<- --> <--
>-> <-< /= -> <= <=>
=>> <<= ==> <== <==>
>=> <=< ## ### |> <|
</ /> #? ]#
\end{minted}
\end{itemize}

\end{document}

So some ligatures are enabled, others are disabled, and it depends on the language chosen.

I thought minted used fancyvrb environments "under the hood", so I'm not sure why some ligatures are disabled in the minted environments. I'm also not sure why it depends on the language. Assumedly it has something to do with the Pygments settings for each language. Is this expected behavior or a bug?

Related SE question: https://tex.stackexchange.com/questions/683184/jetbrains-arrow-code-ligatures-do-not-apply-consistently-in-different

gpoore commented 1 year ago

This is due to how Pygments does tokenization. For Bash, |=> becomes \PYG{p}{|}\PYG{o}{=}\PYGZgt{}. For Python, it becomes \PYG{o}{|=\PYGZgt{}}. (\PYGZgt{} is the Pygments translation of >.) Because the characters are split between different tokens for Bash (\PYG{<token_type>}{<token_text>}), ligatures won't work.

mbertucci47 commented 1 year ago

Thanks for the explanation