gpoore/minted

#include directives are in italics

Closed this issue · 8 comments

First of all, awesome package, thank you very much for developing and maintaining it!

I'm having an issue with the following code:

\begin{minted}{c++}
#include <iostream>
int main(void) {}
\end{minted}

image

The first line with the #include is all displayed in italics. The rest of the code is nicely in code-font. Is there a way to change that?

That's the standard behavior for the default highlighting style. You could try a different style. See http://pygments.org/demo to try a quick demo online and get a list of styles (but notice that some details in LaTeX may be a little different, due to how Pygments outputs LaTeX). To change the style in the document, \setminted{style=<style>}.

Another option is to disable italics altogether within minted environments. You could use something like \AtBeginEnvironment{minted}{\let\itshape\relax}.

Ah, thank you very much for the info. That demo page is great as well.

The behaviour is still a bit odd though: I don't think the behavior of default is italics - on the demo page, that code is in code-font as well. So is it for e.g. native. But in my latex document, both get displayed in italics. If I choose another style like vs, I get non-italic behaviour on both the demo site and in my document. So sometimes the output of the two is consistent, sometimes it differs.

Edit: That demo page seems to be a bit odd: the style bw (assuming it means black-white) displays some code with colors. If I choose bw in my latex document, it's (correctly?) black & white only.

You're correct, something is strange with the demo page, and bw isn't working properly there. Looks like a bug in the site. If you do a web search, you can probably find some (correct) pre-renderd Pygments style galleries.

Yep, there's a lot more wrong with that demo page than only the bw style.

Anyway, so there's no way to disable italics in the default style only for stuff with #? (I only care about C++ codeblocks - in other languages, # may often indicate a comment, but in C++ it's regular code, so I personally would like that in non-italic).

Unfortunately, the Pygments lexer treats preprocessor directives as a subclass of comments, which makes this tricky.

There are a couple ways to deal with this. You could create a custom environment for C++ with \newminted, and then use the \AtBeginEnvironment{<new_env>}{\let\itshape\relax} trick to eliminate italics in the new environment.

Or you could eliminate italics for preprocessor directives directly. Inspired by this TeX.SE question, the following code should do what you want. You can put it in the preamble after loading minted (it assumes minted 2.0). It allows you to treat preprocessor directives as a different type of token. Currently, this will treat c+cp (comment:comment.preprocessor) as s+o (string:string.other). A list of Pygments tokens is here. Generally, they are abbreviated to some combination of the first letters in the LaTeX style definitions. I'm not aware of any existing reference; the best way to get the "code" for a particular token is probably just to highlight an example of it and then look at the cached output. Note that if you want to use this for styles other than default, you would need to replace \PYGdefault with \PYG<other_style>.

\usemintedstyle{default}
\AtBeginEnvironment{minted}{%
  \let\originalPYGdefault\PYGdefault
  \renewcommand{\PYGdefault}[2]{%
    \ifstrequal{#1}{c+cp}%
     {\originalPYGdefault{s+o}{#2}}%
     {\originalPYGdefault{#1}{#2}}%
  }%
}

Aah, wicked. Thank you very much! This works well enough.

From my point of view, the issue can be closed.

zhayu commented

yes, I have the same problem, but now I solved by using gpoore's approach.
Adding the following to the preemble produces what you desired:

\usepackage{minted}
\usepackage{etoolbox}
\usepackage{newunicodechar}
\newtoggle{inminted}
\AtBeginEnvironment{minted}{\let\itshape\relax}

Hope this may help you!

This issue should be fixed with pygments/pygments#1391.