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

[show-&-tell] ad-hoc adoptations from a different style to another one. #380

Open
goyalyashpal opened this issue Dec 5, 2023 · 2 comments

Comments

@goyalyashpal
Copy link

goyalyashpal commented Dec 5, 2023

Prelude

I am using

  • a language for interactive terminal sessions (like console or psql)
  • style=bw
  • and the output is tokenized as Generic.Output

i wanted to make the output tokens from style=bw to appear just like they do in default style i.e. light gray, since:

  • in style=bw, the output is not differentiated from the command line (prompt + command) in any way,
  • unlike its distinct light grey color in default style.
  • this makes it very hard to read.

If you really want gray comments, you'll have to customize the comment token macro. You'll need something like this in the preamble:

\makeatletter
\appto{\minted@patch@PYGZhy}{%
  \def\PYG@tok@c{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.5,0.5,0.5}{##1}}}}
\makeatother

Originally posted by @ gpoore in #366 (comment)

Details

So,

  1. i inspected the _minted-* directory of my existing project
    • I saw these terms at line start of .pygtex files
      \PYG{g+gp} for prompt Generic.Prompt, and
      \PYG{g+go} before output lines Generic.Output, or
      sometimes even this \PYG{l+s+s1} before Bye lol Literal + String + ?
    • i noticed that \PYGZ*{} is just like x/html character entities, so, gt >, hy -, bs \, sq ' (single quotes)
  2. i created a Minimal working example
    actually, it was for asking the question, but it immediately led myself to discover the hack
  3. i inspected the same _minted-* dir of this MWE and
    • noticed the files bw.pygstyle default.pygstyle *.pygtex
  4. i immediately opened both style files and searched for go and found this following line in default.pygstyle defining the grayscale color (all 3 arguments to color of same value)
  5. I guarded this inside \makeat* and pasted in my preamble. and voilla, it worked :)
\@namedef{PYG@tok@go}{\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}

Cons and Alternates

  • i reckon that this will define & overwrite this for all the styles, which may not be desirable in general usecases.
  • fortunately, it ain't a problem for me at this very moment.
  • i thought about pasting this line verbatim in the bw.pygstyle but it would be very volatile and not very "setup once & forget" method
  • i tried makeat inside the \newmintedfile's body, but it didn't compile

but anyhow, What better way of doing this would you suggest? 😃

MWE source

\begin{filecontents}[noheader, overwrite]{./test.mysql}
    mysql-> select * from firstLine;
    +--------------------+
    | Database           |
    +--------------------+
    mysql-> \help contents
    mysql-> \h contents
    You asked for help about help category: "Contents"
\end{filecontents}

\documentclass{article}
\usepackage{minted}

% \makeatletter
% \@namedef{PYG@tok@go}{\def\PYG@tc##1{\textcolor[rgb]{0.44,0.44,0.44}{##1}}}
% \makeatother


\begin{document}

\inputminted[autogobble]{psql}{test.mysql}
\inputminted[autogobble, style=bw]{psql}{test.mysql}

\end{document}

Screenshots

original screenshot

screenshot after modification:


References:

@muzimuzhi
Copy link
Contributor

Why not do this on Python side, by defining your own Pygments style, probably based on a builtin style?

@samcarter
Copy link

samcarter commented Sep 30, 2024

@goyalyashpal Thanks for the nice idea to make style modifications from within the latex file! That's indeed very helpful if one would like other people to be able to compile the document, e.g. a package docu to be submitted to ctan or a journal article.

I think you were lucky that the bw style doesn't change \PYG@tok@go. Otherwise the style might have overwritten your changes. You can avoid this problem by smuggling your changes in later:

\documentclass{article}
\usepackage{minted}

\usemintedstyle{default}

\makeatletter
\AddToHook{env/MintedVerbatim/before}{%
  % optional arguments
  \@namedef{PYG@tok@na}{\def\PYG@tc##1{\textcolor{black}{##1}}}%
  % macros
  \@namedef{PYG@tok@k}{\def\PYG@tc##1{\textcolor{red}{\bfseries ##1}}}%
  % curly brakets
  \@namedef{PYG@tok@nb}{\def\PYG@tc##1{\textcolor{red}{\bfseries ##1}}}%
  % comments
  \@namedef{PYG@tok@c}{\def\PYG@tc##1{\textcolor{gray}{##1}}}%
}
\makeatother

\begin{document}

\begin{minted}{latex}
% a duck
\begin{tikzpicture}
  \duck[cake]
\end{tikzpicture}
\end{minted}

\end{document}

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

3 participants