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

PDF output: Pygments error highlighting increases line spacing in code blocks #4249

Closed
jfbu opened this issue Nov 20, 2017 · 2 comments
Closed

Comments

@jfbu
Copy link
Contributor

jfbu commented Nov 20, 2017

Procedure to reproduce the problem

Apply following patch to silence Pygments exception on lexer error token:

diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py
index eb309d82a..acaca89ad 100644
--- a/sphinx/highlighting.py
+++ b/sphinx/highlighting.py
@@ -44,8 +44,8 @@ lexers = dict(
     rest = RstLexer(stripnl=False),
     c = CLexer(stripnl=False),
 )  # type: Dict[unicode, Lexer]
-for _lexer in lexers.values():
-    _lexer.add_filter('raiseonerror')
+# for _lexer in lexers.values():
+#     _lexer.add_filter('raiseonerror')
 
 
 escape_hl_chars = {ord(u'\\'): u'\\PYGZbs{}',
@@ -136,7 +136,8 @@ class PygmentsBridge(object):
                                    location=location)
                     lexer = lexers['none']
                 else:
-                    lexer.add_filter('raiseonerror')
+                    pass
+                    # lexer.add_filter('raiseonerror')
 
         # trim doctest options if wanted
         if isinstance(lexer, PythonConsoleLexer) and self.trim_doctest_flags:

Then do make latexpdf with index.rst being

Welcome to FOO's documentation!
===============================

.. highlight:: coq

.. raw:: latex

   \begin{multicols}2

::

   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).


.. raw:: latex

   \makeatletter
   \def\PYG@tok@err {\def\PYG@bc##1{\strut ##1}}
   \makeatother

::

   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).
   Definition logeq_both_false {X Y : UU} : ¬X -> ¬Y -> (X <-> Y).

.. raw:: latex

   \end{multicols}

and conf.py containing

latex_elements = {
    'preamble': r'\usepackage{multicol}',
}

Error logs / results

This produces this

capture d ecran 2017-11-20 a 16 16 35

Expected results

The two sides have the same height.

The problem is in the produced sphinxhighlight.sty file in the latex build repertory which contains

\expandafter\def\csname PYG@tok@err\endcsname{\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}

It should be \setlength\fboxsep{-\fboxrule}.

Besides there is another bug, which is that braces should be used else the \fboxsep settting is not local. This does not show easily because each line in code-block is in its own TeX box, so the change does not propagate to other lines, and besides a priori, nothing uses again \fboxsep setting. But it is a bug nevertheless.

There is also

\expandafter\def\csname PYG@tok@cs\endcsname{\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}}}

which has same unbraced \setlength{\fboxsep}{0pt} issue. (here 0pt is correct choice, though).

Environment info

  • OS: Mac
  • Python version: 3.5.4
  • Sphinx version: stable at 0c98d04
  • TeXLive 2017

Relates

#4225

@jfbu jfbu added this to the 1.6.6 milestone Nov 20, 2017
@jfbu
Copy link
Contributor Author

jfbu commented Nov 20, 2017

This appears to a be a bug (or feature) of the Pygments latex formatter, because as far as I understand the sphinxhighlight.sty is generated (from sphinx/builders/latex.py) via a call to Pygments via a PygmentsBridge object (the class is defined in highlighting.py).

jfbu added a commit to jfbu/sphinx that referenced this issue Nov 20, 2017
jfbu added a commit to jfbu/sphinx that referenced this issue Nov 20, 2017
jfbu added a commit to jfbu/sphinx that referenced this issue Dec 6, 2017
jfbu added a commit that referenced this issue Dec 10, 2017
Fix #4249 by overriding Pygments latex formatter error highlighting
@jfbu
Copy link
Contributor Author

jfbu commented Dec 10, 2017

Fixed at 0febbc5

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant