Juan Luis Cano Rodríguez | 5 May 2012 20:00
Picon
Gravatar

Unable to get syntax highlight with new code directive in LaTeX

Hello everyone, I just read the other day the release notes of the 0.9 version and decided to test the new code directive and role [1].


My problem is that, though I have Pygments installed and the code seems to be well parsed, I can't get it colored when I export to LaTeX using rst2latex.

If I export to HTML, I can then do

    pygmentize -f html -S trac > style.css

add the stylesheet to the generated .html file and there I have the colors. On the other hand, I cannot figure out how to do this with LaTeX.

I stumbled upon an old document on the sandbox mentioning rst2html-pygments and rst2latex-pygments [2], which seems to be exactly what I want, but these scripts are not included in the main distribution.

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
Guenter Milde | 6 May 2012 21:41

Re: Unable to get syntax highlight with new code directive in LaTeX

On 2012-05-05, Juan Luis Cano Rodríguez wrote:

> [-- Type: text/plain, Encoding:  --]

> Hello everyone, I just read the other day the release notes of the 0.9
> version and decided to test the new code directive and role [1].

> My problem is that, though I have Pygments installed and the code seems to
> be well parsed, I can't get it colored when I export to LaTeX using
> rst2latex.

As with the HTML export, you need a stylesheet (but you may know this
already).

(Remember that for syntax highlight of a "code" role, you need to define
a custom text role based on "code". See the test input file
"standard.txt" for an example.)

> If I export to HTML, I can then do

>     pygmentize -f html -S trac > style.css

> add the stylesheet to the generated .html file and there I have the colors.
> On the other hand, I cannot figure out how to do this with LaTeX.

Docutils does syntax highlight via text roles. This means that in all output
variants, styling is possible with rules similar to styling custom text roles.

For LaTeX, you have two options:

* Raphael 'kena' Poss contributed a CSS->TeX converter pygments
  stylesheets. I jut put it on the sandbox, together with an example
  stylesheet:
  http://docutils.svn.sourceforge.net/viewvc/docutils/trunk/sandbox/code-block-directive/tools/

  Use with, e.g. ::

   pygmentize -S default -f html | python makesty.py >pygments-DUroles.sty

  A proper solution would be to provide a new output format for pygmentize,
  volunteers?  

* Alternatively, you can create a stylesheet "by hand". 

  For this, I recommend to use the option --syntax-highlight=long, which
  generates output with a hierarchy of intelligable class names, so that
  you can e.g. style all keywords with
  ``\newcommand{\DUrolekeyword}{\textbf}``

  For PDF, I prefer syntax highlight in black and white with bold,
  italic, and small caps. Remember, that you need a monospaced font with
  a these variants (e.g. txtt) in order to see an effect.

> I stumbled upon an old document on the sandbox mentioning rst2html-pygments
> and rst2latex-pygments [2], which seems to be exactly what I want, but
> these scripts are not included in the main distribution.

These scripts are superseded by the native support for code directive and
role. They also require stylesheets.

Günter

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Docutils-users mailing list
Docutils-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.
dcutil_guard.bloodnok | 26 Mar 2013 13:41
Favicon

Re: Unable to get syntax highlight with new code directive in LaTeX

On 05/07/2012 03:41 AM, Guenter Milde wrote:
> On 2012-05-05, Juan Luis Cano Rodríguez wrote:
...
>> If I export to HTML, I can then do
>
>>      pygmentize -f html -S trac > style.css
>
>> add the stylesheet to the generated .html file and there I have the colors.
>> On the other hand, I cannot figure out how to do this with LaTeX.
>
> Docutils does syntax highlight via text roles. This means that in all output
> variants, styling is possible with rules similar to styling custom text roles.
>
> For LaTeX, you have two options:
>
> * Raphael 'kena' Poss contributed a CSS->TeX converter pygments
>    stylesheets. I jut put it on the sandbox, together with an example
>    stylesheet:
>    http://docutils.svn.sourceforge.net/viewvc/docutils/trunk/sandbox/code-block-directive/tools/
>
>    Use with, e.g. ::
>
>     pygmentize -S default -f html | python makesty.py >pygments-DUroles.sty
>
>    A proper solution would be to provide a new output format for pygmentize,
>    volunteers?
>

Hi,

The makesty.py script referred to[1] seems to give me errors when I use 
it with rst2latex and pdflatex - along the lines of:

   ! LaTeX Error: Missing \begin{document}.

   See the LaTeX manual or LaTeX Companion for explanation.
   Type  H <return>  for immediate help.
   ...

   l.21 \providecommand\csname DU
                                 rolec1\endcsname[1]

The information I've found from Googling for this error seems to suggest 
this is a problem with the LaTeX code generated by the script:

   \providecommand\csname 
DUrolec1\endcsname[1]{\textit{\textcolor[rgb]{0.25,0.50,0.50}{#1}}}

However, I found a fix for this: adding "\expandafter" before the 
"\providecommand" seems to fix this.

i.e.:

   \providecommand\csname 
DUrolec1\endcsname[1]{\textit{\textcolor[rgb]{0.25,0.50,0.50}{#1}}}

I hope this is of use for anyone else trying to get the LaTeX writer to 
work with Pygments.

Perhaps also the docutils documentation should be updated to make it 
clear that a user has to generate their own style sheet?

The current text at 
http://docutils.sourceforge.net/docs/ref/rst/directives.html#code says 
that "The actual highlighting *can be customized* with a style-sheet 
(e.g. one generated by Pygments)". [my emphasis]

But it would seem more accurate to say that highlighting will not work 
at all *unless* you create your own style sheet. (At least for LaTeX, 
anyway.)

cheers

BW

[1] which I believe has now been re-named pygments_css2sty.py, and is 
now located at 
http://docutils.sourceforge.net/sandbox/stylesheets/pygments_css2sty.py

------------------------------------------------------------------------------
Own the Future-Intel&reg; Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest.
Compete for recognition, cash, and the chance to get your game 
on Steam. $5K grand prize plus 10 genre and skill prizes. 
Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
_______________________________________________
Docutils-users mailing list
Docutils-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.
Guenter Milde | 27 Mar 2013 17:55

Re: Unable to get syntax highlight with new code directive in LaTeX

On 2013-03-26, dcutil_guard.bloodnok <at> recursor.net wrote:
> On 05/07/2012 03:41 AM, Guenter Milde wrote:
>> On 2012-05-05, Juan Luis Cano Rodríguez wrote:
> ...
>>> If I export to HTML, I can then do

>>>      pygmentize -f html -S trac > style.css

>>> add the stylesheet to the generated .html file and there I have the colors.
>>> On the other hand, I cannot figure out how to do this with LaTeX.

>> Docutils does syntax highlight via text roles. This means that in all
>> output variants, styling is possible with rules similar to styling
>> custom text roles.

>> For LaTeX, you have two options:

a) use the style files from the sandbox/stylesheets. Available via SVN or
   http://docutils.sourceforge.net/sandbox/stylesheets/pygments-default.sty
   http://docutils.sourceforge.net/sandbox/stylesheets/pygments-long.sty
   (I just found and fixed bugs, so you need a fresh checkout.)

b)

>> * Raphael 'kena' Poss contributed a CSS->TeX converter pygments
>>    stylesheets. I jut put it on the sandbox

>>    A proper solution would be to provide a new output format for pygmentize,
>>    volunteers?

> The makesty.py script referred to[1] seems to give me errors
...

> However, I found a fix for this: adding "\expandafter" before the 
> "\providecommand" seems to fix this.

Thanks for the report and fix. 
As you already found out, the script is available under
http://docutils.sourceforge.net/sandbox/stylesheets/pygments_css2sty.py

> Perhaps also the docutils documentation should be updated to make it 
> clear that a user has to generate their own style sheet?

> The current text at 
> http://docutils.sourceforge.net/docs/ref/rst/directives.html#code says 
> that "The actual highlighting *can be customized* with a style-sheet 
> (e.g. one generated by Pygments)". [my emphasis]

> But it would seem more accurate to say that highlighting will not work 
> at all *unless* you create your own style sheet. (At least for LaTeX, 
> anyway.)

Fixed the documentation.

In the long run, there should be basic stylesheets at default locations
(stylesheet-dirs) and maybe even auto-activated.

Günter

------------------------------------------------------------------------------
Own the Future-Intel&reg; Level Up Game Demo Contest 2013
Rise to greatness in Intel's independent game demo contest.
Compete for recognition, cash, and the chance to get your game 
on Steam. $5K grand prize plus 10 genre and skill prizes. 
Submit your demo by 6/6/13. http://p.sf.net/sfu/intel_levelupd2d
_______________________________________________
Docutils-users mailing list
Docutils-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.
Juan Luis Cano Rodríguez | 8 May 2012 21:08
Picon
Gravatar

Re: Unable to get syntax highlight with new code directive in LaTeX

Thank you for your answer Günter (sorry for not answering myself, but it seems I wasn't receiving emails from the list correctly). I tried the script you uploaded to the sandbox, and it worked but it has a little bug: it expects all the lines in the CSS stylesheet to have a comment:

    print "% " + l.split('*')[1]

but if it isn't the case, it just breaks. Apart from this detail I got a nice coloured PDF. Thank you again!
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
Guenter Milde | 9 May 2012 12:44

Re: Unable to get syntax highlight with new code directive in LaTeX

On 2012-05-08, Juan Luis Cano Rodríguez wrote:

> Thank you for your answer Günter (sorry for not answering myself, but it
> seems I wasn't receiving emails from the list correctly). I tried the
> script you uploaded to the sandbox, and it worked but it has a little bug:
> it expects all the lines in the CSS stylesheet to have a comment:

>     print "% " + l.split('*')[1]

> but if it isn't the case, it just breaks. Apart from this detail I got a
> nice coloured PDF. Thank you again!

Thanks for the report. This is fixed in the sandbox SVN now. I also put
example stylesheets in the styles repository ``sandbox/stylesheets/``.

Günter

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Docutils-users mailing list
Docutils-users <at> lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/docutils-users

Please use "Reply All" to reply to the list.

Gmane