Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change highlighting color for numbers #275

Open
muzimuzhi opened this issue Aug 4, 2020 · 0 comments
Open

Change highlighting color for numbers #275

muzimuzhi opened this issue Aug 4, 2020 · 0 comments

Comments

@muzimuzhi
Copy link
Contributor

muzimuzhi commented Aug 4, 2020

Just for the record, to help others with similar requirements in future.

I saw a question on CTAN's google group, in which OP wanted to change the highlighting color for numbers when pygments style friendly is used.

Under that post, I gave following workaround

\documentclass{article}
\usepackage{minted}
\usemintedstyle{friendly}
\newenvironment{myspec}
  {\VerbatimEnvironment\begin{minted}{haskell}}
  {\end{minted}}


\makeatletter
\newif\ifminted@color@patched

\def\PYGfriendly@change@color{%
  % test if style `friendly` is loaded
  \ifdefined\PYGfriendly@tok@m
    % patch only once since any pygments style is loaded only once
    \ifminted@color@patched
    \else
      \minted@color@patchedtrue
      % redefine highlighting style for token type `mi` (Number.Integer)
      \def\PYGfriendly@tok@mi{%
        \def\PYGfriendly@tc####1{\textcolor{green}{####1}}%
      }%
      % redefine highlighting style for token type `mf` (Number.Float)
      \def\PYGfriendly@tok@mf{%
        \def\PYGfriendly@tc####1{\textcolor{green}{####1}}%
      }%
    \fi
  \fi
}

% append \PYGfriendly@change@color into beginning of `Verbatim` environment
\fvset{codes*={\PYGfriendly@change@color}}
\makeatother

\begin{document}
\begin{myspec}
menuSlideIn = linearTo (menu . width) (For 0.5) (To 75)
appFadeOut = linearTo (obscuringBox . alpha) (For 0.5) (To 0.65)
\end{myspec}
\end{document}

and words

Default color scheme of "friendly" pygments style is hardcoded in
https://github.com/pygments/pygments/blob/master/pygments/styles/friendly.py
and in converted friendly.pygsty located in minted's cache directory.

Here is an example changing color for token types Number.Integer and Number.Float to green.

[example]

  • minted use python library pygments to do syntax highlighting. pygments firstly splits code snippet into tokens, then apply a style to highlight it.
  • For LaTeX use, every style has a corresponding <style name>.pygsty stored in cache directory used by minted.
  • In the friendly.pygsty file, you will find macro definitions for every token type. For example, token type Number.Integer has short name mi, hence its macro definition begins with \expandafter\def\csname PYGfriendly@tok@mi\endcsname .... For a complete list of token types and their short names, see
    https://github.com/pygments/pygments/blob/master/pygments/token.py
  • For example, in definition of \PYGfriendly@tok@mi in file friendly.pygsty, you will find the color used is [rgb]{0.25,0.63,0.44}, which corresponds to Number: "#40a070" in source code of friendly style (see its link at the beginning of my reply). You can think Number.Integer is a subtype of Number, hence inherits highlighting style from Number.
  • Therefore in LaTeX, to change the color is to redefine the corresponding macro (this is done in macro \PYGfriendly@change@color in my above example), and apply that redefinition after xxx.pygsty is loaded. That's why I use a test \ifdefined\PYGfriendly@tok@m and sets \fvset{codes*={\PYGfriendly@change@color}}.

If you want to change other token (sub)types of Number, you can add redefinitions into \PYGfriendly@change@color by yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant