12

What is the best way to highlight code when using ConTeXt? I am aware of support for specific languages, as described on the ConTeXt wiki, but it seems rather limited at present. I know that we could also rely on vim, through the t-vim module.

Now, I wonder if there exists more automated ways to deal with other languages like R, Python, Lisp, Asymptote? Also, I would like to be able to combine verbatim and mathematical notations, as it is currently available in Minted, for example.

A working example would be very appreciated.

chl
  • 8,890
  • 2
    t-vim can deal with any language for which there is syntax highlighting support in vim. – Aditya Dec 26 '10 at 20:40
  • @Aditya Yes, thanks. I like vim colors although I use Emacs :) In fact, I was rather looking for a 'pure' ConTeXt solution, in the spirit of the minted LaTeX package. – chl Dec 26 '10 at 20:55
  • 1
    @chl: minted isn't a pure Latex solution, since it relies on Pygments, the Python syntax highlighter. – Charles Stewart Dec 26 '10 at 22:44
  • @Charles You're damned right! I guess I have to write custom pretty printer then?! I was hoping that something along Lua/MkIV already existed... – chl Dec 26 '10 at 23:45
  • @chl: I've wondered about how tricky it would be to translate Pygments' Latex formatter into a Context formatter, and I think it would be easy. You'd then have coverage of all the languages that Pygments covers. Porting listings, which is pure Latex, I think would be hard. – Charles Stewart Dec 27 '10 at 10:06
  • 1
    MkIV has a new parser/pretty-printer. See the ConTeXt mailing list for a few discussions and look at v-*.(mkiv|lua) files for examples. – Aditya Dec 27 '10 at 15:57
  • @Aditya Cool, thanks for this. I will look at ntg-context. If no answer comes within few days, I think you could simply drop this comment as an answer so that it can be accepted. – chl Dec 27 '10 at 16:28
  • 1
    http://wiki.contextgarden.net/Custom_pretty_printer – Mica Dec 28 '10 at 04:59
  • 1
    @Aditya: Can you link to the discussion of this pretty-printer? – Charles Stewart Jan 11 '11 at 11:46
  • You can try using one of Pygments ImageFormatters through the filter module https://github.com/adityam/filter. – Martin Heller Mar 01 '11 at 00:03
  • @Mica perhaps you could add an answer about the custom pretty printer so we can get this question off the "unanswered list" – Seamus Jun 23 '11 at 13:50
  • 1
    Starting a bounty, with the hope of closing this question :-) /cc @Seamus – chl Jun 26 '11 at 21:27

1 Answers1

14

If you are willing to use an external tool, then t-vim provides highlighting for many languages. You can use it as follows: define a typing

\usemodule[vim]
\definevimtyping [RUBY]  [syntax=ruby]

and then use it either as an evnironment

    \startRUBY
      ...
    \stopRUBY

or inline

    \inlineRUBY{...}

This module does not support math escape, but with luatex, it is easy to support. Here is a complete example.


\newcatcodetable\minimalmathtable

\startcatcodetable \minimalmathtable
    \catcode\backslashasciicode  = \escapecatcode
    \catcode\leftbraceasciicode  = \begingroupcatcode  
    \catcode\rightbraceasciicode = \endgroupcatcode
    \catcode\endoflineasciicode  = \activecatcode
    \catcode\formfeedasciicode   = \activecatcode
    \catcode\spaceasciicode      = \activecatcode
    \catcode\dollarasciicode     = \mathshiftcatcode
\stopcatcodetable 

\unprotect
\starttexdefinition mathescaped #1
  \pushcatcodetable
  \setcatcodetable \minimalmathtable
  \ctxcommand{parsemath(\!!bs #1 \!!es)}
  \popcatcodetable
\stoptexdefinition
\protect

\startluacode
  local function unescape(content)
      local s = string.gsub(content, '\\\\', '\\')
      s = string.gsub(s, '\\{', '{')
      s = string.gsub(s, '\\}', '}')
      return s
  end

  local dollar = lpeg.P("$")
  local nodollar = (1 - dollar)
  local math = lpeg.Cs(dollar * nodollar^0 * dollar) / unescape
  local any = lpeg.P(1)
  local match = lpeg.Cs( (math + any)^0 )

  function commands.parsemath(content)
    local s = lpeg.match(match, content)
    tex.sprint(s)
  end

\stopluacode

\usemodule[vim]

\startcolorscheme[pscolor]
    \definesyntaxgroup 
        [Comment]
        [command=\mathescaped]
\stopcolorscheme

\definevimtyping[python][syntax=python]

\starttext

\startpython
# Returns $\sum_{i=1}^{n}i$
def sum_upto(n)
    r = range(1, n+1)
    return sum(r)
\stoppython

\stoptext

The same restrictions as the listings package apply, i.e., spaces are active, so you should avoid spaces in math mode.

EDIT: The new (2012.12.17) version of t-vim module supports math escape in the Comment region. To active it you need to pass escape=on to \definevimtyping and use \m{...} or \math{...} for math mode. Thus, instead of the above kludge, you can use

\definevimtyping[python][syntax=python, escape=on]

\startpython
# Returns \m{\sum_{i=1}^{n}i}
def sum_upto(n)
    r = range(1, n+1)
    return sum(r)
\stoppython
Dave Jarvis
  • 11,809
Aditya
  • 62,301
  • (+1) Thanks for the working example about math support. It looks great. I'll try this ASAP. – chl Jun 27 '11 at 15:03
  • Just tried it, but cannot get it to work. I encountered errors with Module Catcodes (! Undefined control sequence. <argument> \catcode \tabasciicode) when trying to compile a working example (the above one, or any of those available in your Github repos using context). I don't know why because I succeeded in compiling some files in the past... I shall return to this later. – chl Jun 27 '11 at 19:43
  • Perhaps you are using an old version of ConTeXt. I think that \tabsciicode etc were introduced around April 2011. – Aditya Jun 27 '11 at 22:21
  • Yes, that's what I realized afterwards (I have the minimals beta version on my other computer, but I tested your example on TeXLive 2010 :-) Everything works fine now. Thanks again. – chl Jun 28 '11 at 06:24
  • Is there a way to use Pygments similar to what minted does? – user32882 Dec 21 '22 at 16:33
  • Not out of the box. I had looked into pygments when initially writing t-vim, but could not find any documentation on what the different codes mean. If you understand the output of pygments (or are willing to spend the time to learn it), then it should be a relatively easy thing to support it as part of the t-vim module. – Aditya Dec 23 '22 at 05:17
  • Also see https://adityam.github.io/context-blog/post/clean-tex-output/ for other syntax highlighters that I tested. – Aditya Dec 23 '22 at 05:18