Skip to content

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

Closed

Description

@jfbu

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

Activity

  1. added this to the 1.6.6 milestone on Nov 20, 2017
  2. jfbu commented on Nov 20, 2017

    @jfbu
    ContributorAuthor

    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).

  3. added 2 commits that reference this issue on Nov 20, 2017
    5f2e001
    c89a9e9
  4. added 2 commits that reference this issue on Dec 6, 2017
    6cb517d
    0febbc5
  5. jfbu commented on Dec 10, 2017

    @jfbu
    ContributorAuthor

    Fixed at 0febbc5

  6. locked as resolved and limited conversation to collaborators on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions