pygments/pygments

inherance (of bold, italic, etc) not working as expected with LaTeX

Opened this issue · 10 comments

(Original issue 614 created by None on 2010-11-16T21:00:59.827635+00:00)

Hi, I'm using Pygmentize for LaTeX output but I've got an annoying bug, I can't get italic comments while getting non italic preprocessor directives like a #include, while it works on HTML output. The problem lies in some wrong handling of style inherance for LaTeX.
For example, in murphy style we have:

Comment: "#666 italic",
Comment.Preproc: "#579 noitalic",
Comment.Special: "#c00 bold",

and, even though we have noitalic for Preproc, it keeps italic in LaTeX output (I'm using pdfLatex on TeX Live 2010) while it outputs non italic on html.

Pygmentize is great thanks.

(Original comment by tshatch on 2010-12-21T08:35:00.132796+00:00)

Hi, can you provide full steps to reproduce?

(Original comment by None on 2010-12-21T12:36:18.155821+00:00)

Create a directory and inside it create a file named main.cpp with the following contents:

#include <iostream>

int main()
{
	// Saying hello to the world
	std::cout << "Hello World!" << std::endl;
	
	return 0;
}

Navigate to that directory through the command shell and execute the command

pygmentize -O full,style=murphy -l c++ -f html -o main.html main.cpp

then execute

pygmentize -O full,style=murphy -l c++ -f latex -o main.tex main.cpp

now execute

pdflatex main.tex

Now opening both pdf and html output you may see that the #include in the html file isn't italic, which is expected since the murphy style used here doens't inherent italics for the directives (directives are view as subsections of comments in styles), but it keeps italic in the pdf file, while in both, the comments are italic as expected.

Regards

(Original comment by None on 2011-03-02T16:41:29.829977+00:00)

Hello?

(Original comment by None on 2011-07-22T20:01:21.271185+00:00)

The issue is discussed here http://pbelmans.wordpress.com/2011/07/05/guest-blog-inheritance-in-pygments-latexformatter/ and a solution is presented.

Here's a patch against current pygments tip:

--- pygments/formatters/latex.py.orig	2011-07-22 22:55:29.000000000 +0300
+++ pygments/formatters/latex.py	2011-07-22 22:58:29.000000000 +0300
@@ -96,11 +96,9 @@
     \let\%(cp)s@ul=\relax \let\%(cp)s@tc=\relax%%
     \let\%(cp)s@bc=\relax \let\%(cp)s@ff=\relax}
 \def\%(cp)s@tok#1{\csname %(cp)s@tok@#1\endcsname}
-\def\%(cp)s@toks#1+{\ifx\relax#1\empty\else%%
-    \%(cp)s@tok{#1}\expandafter\%(cp)s@toks\fi}
 \def\%(cp)s@do#1{\%(cp)s@bc{\%(cp)s@tc{\%(cp)s@ul{%%
     \%(cp)s@it{\%(cp)s@bf{\%(cp)s@ff{#1}}}}}}}
-\def\%(cp)s#1#2{\%(cp)s@reset\%(cp)s@toks#1+\relax+\%(cp)s@do{#2}}
+\def\%(cp)s#1#2{\%(cp)s@reset\%(cp)s@tok{#1}\%(cp)s@do{#2}}
 
 %(styles)s
 
@@ -340,14 +338,11 @@
             else:
                 value = escape_tex(value, self.commandprefix)
             styles = []
-            while ttype is not Token:
-                try:
-                    styles.append(t2n[ttype])
-                except KeyError:
-                    # not in current style
-                    styles.append(_get_ttype_name(ttype))
-                ttype = ttype.parent
-            styleval = '+'.join(reversed(styles))
+            try:
+                styleval = t2n[ttype]
+            except KeyError:
+                # not in current style
+                styleval = _get_ttype_name(ttype)
             if styleval:
                 spl = value.split('\n')
                 for line in spl[:-1]:

Please, apply this patch.

(Original comment by None on 2011-07-22T20:02:56.424936+00:00)

The patch was corrupted by bitbucket intelligent comment formatter. Here it is: http://pastebin.com/4qREr2Tr

(Original comment by leliobrun on 2018-07-26T11:42:22.613993+00:00)

I ran across this bug, why is it still alive 8 years after?

Hello, I also noticed this bug.

Not quite sure what the status is here... Is there already a pull request to fix it in the next release ?

Not that I'm aware of -- any contribution to help resolve this is welcome!

Voilà.

Unless there is something I do not understand, this should fix the issue without side effects (other than perhaps slower LaTeX compilation).

Potential future optimization : This should also remove the needs for \PY@reset completely

jfbu commented

The #1989 has the same cause.

edit1: sorry, no. I went too quick to conclusion. In #1989, LaTeX formatter uses k+kt. The #1391 PR would indeed reset the \PYG@bf so the mismatch in output between LaTeX (bold is kept) and HTML (no bold) would disappear, but there is still the problem that the classes diverges k+kt in LaTeX and only kt in HTML.

edit2: my edit1 was perhaps too much influenced by #1391 PR, but indeed the #1989 is about a nobold without effect and the opening ticket here mentions a noitalic without effect. So yes, probably #1989 is another instance of same underlying phenomenon (the nobold should translate into latex into a reset of \$$@bf to \relax and the noitalic should give a reset of \$$@it to \relax).