Skip to content

Commit

Permalink
Fix #10506 (again): replacements to Pygments escapes
Browse files Browse the repository at this point in the history
  • Loading branch information
jfbu committed Jul 2, 2022
1 parent a7ef63a commit 213c29b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CHANGES
Expand Up @@ -19,8 +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
* #10506: LaTeX: build error if highlighting inline code role in figure caption
(refs: #10251)

Testing
--------
Expand Down
35 changes: 31 additions & 4 deletions sphinx/highlighting.py
Expand Up @@ -40,12 +40,39 @@
ord('}'): '\\PYGZcb{}'}

# used if Pygments is available
# MEMO: no use of \protected here to avoid having to do hyperref extras,
# (if in future code highlighting in sectioning titles is activated):
# the definitions here use only robust, protected or chardef tokens,
# which are all known to the hyperref re-encoding for bookmarks.
# The " is troublesome because we would like to use \text\textquotedbl
# but \textquotedbl is *defined to raise an error* (!) if the font
# encoding is OT1. This however could happen from 'fontenc' key.
# MEMO: the Pygments escapes with \char`\<char> syntax, if the document
# uses old OT1 font encoding, work correctly only in monospace font.
# MEMO: the Pygmentize output mark-up is always with a {} after.
_LATEX_ADD_STYLES = r'''
% Sphinx additions
% use textcomp quote to get a true single quote
\renewcommand\PYGZsq{\textquotesingle}
% Sphinx redefinitions
% Originally to obtain a straight single quote via package textcomp, then
% to fix problems for the 5.0.0 inline code highlighting (captions!).
% The \text is from amstext, a dependency of sphinx.sty. It is here only
% to avoid build errors if for some reason expansion is in math mode.
\def\PYGZbs{\text\textbackslash}
\def\PYGZus{\_}
\def\PYGZob{\{}
\def\PYGZcb{\}}
\def\PYGZca{\text\textasciicircum}
\def\PYGZam{\&}
\def\PYGZlt{\text\textless}
\def\PYGZgt{\text\textgreater}
\def\PYGZsh{\#}
\def\PYGZpc{\%}
\def\PYGZdl{\$}
\def\PYGZhy{\sphinxhyphen}% defined in sphinxlatexstyletext.sty
\def\PYGZsq{\text\textquotesingle}
\def\PYGZdq{"}
\def\PYGZti{\text\textasciitilde}
\makeatletter
% use \protected to allow \PYG in \caption
% use \protected to allow syntax highlighting in captions
\protected\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+{\PYG@do{#2}}}
\makeatother
'''
Expand Down
3 changes: 2 additions & 1 deletion sphinx/texinputs/sphinxlatexstyletext.sty
@@ -1,7 +1,7 @@
%% TEXT STYLING
%
% change this info string if making any custom modification
\ProvidesFile{sphinxlatexstyletext.sty}[2021/12/06 text styling]
\ProvidesFile{sphinxlatexstyletext.sty}[2022/07/02 text styling]

% Basically everything here consists of macros which are part of the latex
% markup produced by the Sphinx latex writer
Expand Down Expand Up @@ -68,6 +68,7 @@
\let\sphinxcrossref \@firstofone
\let\sphinxtermref \@firstofone
\let\sphinxhyphen\sphinxhyphenforbookmarks
\def\PYG#1#2{#2}% (can not yet appear in section titles, but perhaps in future)
}}

% Special characters
Expand Down

0 comments on commit 213c29b

Please sign in to comment.