Add line highlighting to latex formatter

Issue #1187 new
David Handy
created an issue

I am writing a book using Sphinx, and would like highlighting of individual lines of Python code samples to work in PDF format like it does in HTML format.

Bram Geron on the Sphinx-dev list was kind enough to advise me that adding line highlighting support to the latex formatter in pygments was the way to solve this problem:!topic/sphinx-dev/GSLzhsYsdyU

I'm proposing to add hl_lines and hl_color options to LatexFormatter, similar to what was done for ImageFormatter in this ticket:

Help and advice welcome!

Comments (7)

  1. Tim Hatch

    Yes, that sounds great. How to properly highlight lines in latex is the missing part that I don't know.

    (Moving to Sprint milestone since it's bite-sized, and we may end up with a contirbutor that knows as well to help.)

  2. Geoffrey Poore

    As far as the LaTeX side is concerned, my fvextra package for LaTeX extends the fancyvrb package that is currently used for Pygments LaTeX output. fvextra adds highlightlines and highlightcolor options to the Verbatim environment.

  3. jfbu

    Sphinx 1.6.6 (github ) will add support for highlighted lines. As Pygments LaTeXFormatter currently ignores hl_lines, the Sphinx LaTeX writer handles directly the data and malaxes it into suitable LaTeX mark-up.

    We did not benefit from very nice fvextra LaTeX package by @Geoffrey Poore, because we already have in place some wrapper of fancyverb's Verbatim and extended that.

    If Pygments LaTeXFormatter at some point takes into account hl_lines, presumably by adding suitable fvextra enabled option to Verbatim environment, please ping the Sphinx then current LaTeX maintainer before release so that he/she can adapt to that (even without loading full of fvextra.sty.)

    Side note @Geoffrey Poore: I copied over your choice of default highlight colour ;-)

  4. Dietmar Winkler

    This is great news . I just tested it with today's 1.6.6+ stable. Only problem is that there is an offset in the highlighted line. But I guess that this is something that ought to be reported on the sphinx issue tracker.

  5. Log in to comment