From: Juan L. C. R. <jua...@gm...> - 2012-05-05 18:00:33
|
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. Any help? [1]: http://docutils.sourceforge.net/docs/ref/rst/directives.html#code [2]: http://docutils.sourceforge.net/sandbox/code-block-directive/tools/pygments-enhanced-front-ends/ |
From: Guenter M. <mi...@us...> - 2012-05-06 19:42:07
|
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 |
From: <dcu...@re...> - 2013-03-26 12:41:32
|
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 |
From: Juan L. C. R. <jua...@gm...> - 2012-05-08 19:09:20
|
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! |
From: Guenter M. <mi...@us...> - 2012-05-09 10:44:41
|
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 |
From: Guenter M. <mi...@us...> - 2013-03-27 16:56:12
|
On 2013-03-26, dcu...@re... 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 |