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

LaTeX: better workaround for a Pygments LaTeXFormatter issue (with Pygments up to 2.7.4) #8879

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Bugs fixed
change) with late TeXLive 2019
* #8253: LaTeX: Figures with no size defined get overscaled (compared to images
with size explicitly set in pixels) (fixed for ``'pdflatex'/'lualatex'`` only)
* #8874: LaTeX: the fix to two minor Pygments LaTeXFormatter output issues ignore
Pygments style

Testing
--------
Expand Down
3 changes: 2 additions & 1 deletion sphinx/builders/latex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ def write_stylesheet(self) -> None:
with open(stylesheet, 'w') as f:
f.write('\\NeedsTeXFormat{LaTeX2e}[1995/12/01]\n')
f.write('\\ProvidesPackage{sphinxhighlight}'
'[2016/05/29 stylesheet for highlighting with pygments]\n\n')
'[2016/05/29 stylesheet for highlighting with pygments]\n')
f.write('% Its contents depend on pygments_style configuration variable.\n\n')
f.write(highlighter.get_stylesheet())

def write(self, *ignored: Any) -> None:
Expand Down
16 changes: 16 additions & 0 deletions sphinx/highlighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
:license: BSD, see LICENSE for details.
"""

from distutils.version import LooseVersion
from functools import partial
from importlib import import_module
from typing import Any, Dict

from pygments import __version__ as pygmentsversion
from pygments import highlight
from pygments.filters import ErrorToken
from pygments.formatter import Formatter
Expand Down Expand Up @@ -50,6 +52,20 @@
_LATEX_ADD_STYLES = r'''
\renewcommand\PYGZsq{\textquotesingle}
'''
# fix extra space between lines when Pygments highlighting uses \fcolorbox
# add a {..} to limit \fboxsep scope, and force \fcolorbox use correct value
# cf pygments #1708 which makes this unneeded for Pygments > 2.7.4
_LATEX_ADD_STYLES_FIXPYG = r'''
\makeatletter
% fix for Pygments <= 2.7.4
\let\spx@original@fcolorbox\fcolorbox
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means \fcolorbox (from color/xcolor package) must exist when sphinxhighlight.sty is loaded by sphinx.sty. But this is the case as color/xcolor is loaded very early by sphinx.sty, before handling options. So we are safe here. (this is true also on 3.x even though sphinxhighlight.sty is loaded earlier, but I feel now latex changes must only go to master). One could add some \AtBeginDocument to be extra safe if some package modifies \fcolorbox but I feel this is unnecessary. Besides in future, this patch will not be used when people have updated their Pygments version.

\def\spx@fixpyg@fcolorbox{\fboxsep-\fboxrule\spx@original@fcolorbox}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have to insert here a \string -\fboxrule in place of -\fboxrule as was done at Pygments 2.8.1 to work-around a minted (latex package using Pygments) compatibility issue

\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+%
{\let\fcolorbox\spx@fixpyg@fcolorbox\PYG@do{#2}}}
\makeatother
'''
if tuple(LooseVersion(pygmentsversion).version) <= (2, 7, 4):
_LATEX_ADD_STYLES += _LATEX_ADD_STYLES_FIXPYG


class PygmentsBridge:
Expand Down
11 changes: 0 additions & 11 deletions sphinx/texinputs/sphinx.sty
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,6 @@
%% PYGMENTS
% stylesheet for highlighting with pygments
\RequirePackage{sphinxhighlight}
% fix baseline increase from Pygments latex formatter in case of error tokens
% and keep \fboxsep's scope local via added braces
\def\PYG@tok@err{%
\def\PYG@bc##1{{\setlength{\fboxsep}{-\fboxrule}%
\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}%
}
\def\PYG@tok@cs{%
\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}%
\def\PYG@bc##1{{\setlength{\fboxsep}{0pt}%
\colorbox[rgb]{1.00,0.94,0.94}{\strut ##1}}}%
}%


%% TABLES
Expand Down