Skip to content

Commit

Permalink
LaTeX: fix #10506 (make Pygments \PYG macro \protected)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbu committed Jun 30, 2022
1 parent 5a24fec commit a7ef63a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Bugs fixed
* #10594: HTML Theme: field term colons are doubled if using Docutils 0.18+
* #10596: Build failure if Docutils version is 0.18 (not 0.18.1) due
to missing ``Node.findall()``
* #10506: LaTeX: build error when using ``:cpp:stuff`` syntax highlighting in
figure caption

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

Expand Down
9 changes: 7 additions & 2 deletions sphinx/highlighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@
ord('}'): '\\PYGZcb{}'}

# used if Pygments is available
# use textcomp quote to get a true single quote
_LATEX_ADD_STYLES = r'''
% Sphinx additions
% use textcomp quote to get a true single quote
\renewcommand\PYGZsq{\textquotesingle}
\makeatletter
% use \protected to allow \PYG in \caption
\protected\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+{\PYG@do{#2}}}
\makeatother
'''
# fix extra space between lines when Pygments highlighting uses \fcolorbox
# add a {..} to limit \fboxsep scope, and force \fcolorbox use correct value
Expand All @@ -52,7 +57,7 @@
% fix for Pygments <= 2.7.4
\let\spx@original@fcolorbox\fcolorbox
\def\spx@fixpyg@fcolorbox{\fboxsep-\fboxrule\spx@original@fcolorbox}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+%
\protected\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+%
{\let\fcolorbox\spx@fixpyg@fcolorbox\PYG@do{#2}}}
\makeatother
'''
Expand Down

0 comments on commit a7ef63a

Please sign in to comment.