Fixing :emphasized-lines: in PDF output

89 views
Skip to first unread message

David H

unread,
Dec 18, 2015, 8:49:04 AM12/18/15
to sphinx-dev
Hi all -

I posted this first on sphinx-users, but now realized I should have posted it here on sphinx-dev instead.

I am writing (actually re-writing) a beginning Python programming textbook using Sphinx. It is important to me to have highlighted lines of code in both HTML and PDF output format.

Apparently :emphasized-lines: has been broken in PDF output for quite some time, there is a ticket for it here: https://github.com/sphinx-doc/sphinx/issues/1238

This is important enough to me that I'd like to help fix it. I'm an experienced Python developer, new to Sphinx, and not comfortable with latex.

Questions:

Is anyone else working on this, or thinking of working on this?

What help or at least advice can you give me to get started on this?

Thanks,
David H

Bram Geron

unread,
Dec 20, 2015, 5:38:50 PM12/20/15
to sphinx-dev
On Friday, 18 December 2015 14:49:04 UTC+1, David H wrote:
This is important enough to me that I'd like to help fix it. I'm an experienced Python developer, new to Sphinx, and not comfortable with latex.

Great! 
 
Questions:

Is anyone else working on this, or thinking of working on this?

I expect not.
 
What help or at least advice can you give me to get started on this?

I've looked around a bit, and it seems that Sphinx calls[1][2] Pygments for both parsing and formatting to HTML and LaTeX. The Pygments HtmlFormatter checks[3] if the hl_lines attribute is set (in which case it should be a list of line numbers); if it is, the HTML output is postprocessed[4]. There is a similar writer for LaTeX but the highlight wrapper is missing, as is duly noted in the source[6]. Wikipedia gives an example of how to add a background color in LaTeX[5]. I have hand-patched the output of Sphinx using colorbox, and the result looks decent, although there is a bit of a shift to the right, which you can compensate for with a negative-width space (\!). I have given an example in [7]. 

Hope that helps; let us know if you get stuck!

Cheers, Bram

David H

unread,
Jan 6, 2016, 9:19:22 AM1/6/16
to sphinx-dev
Thank you Bram for the good advice to get me started.

Since this appears to be a pygments enhancement, I went ahead and filed pygments ticket for this: https://bitbucket.org/birkenfeld/pygments-main/issues/1187/add-line-highlighting-to-latex-formatter

If all goes well, I'll follow up with a pygments pull request.

Thanks again,
David H

Bram Geron

unread,
Jan 7, 2016, 12:21:17 PM1/7/16
to sphinx-dev
Thanks, and good luck :)

Cheers, Bram
Reply all
Reply to author
Forward
0 new messages