Ask Your Question
3

Disable syntax highlighting in sws2tex

asked 2011-09-03 10:07:46 +0100

Kabi gravatar image

updated 2011-09-05 09:42:32 +0100

kcrisman gravatar image

Hi

I am using sws2tex to convert sage notebooks to latex. I am wondering if it is possible to disable syntax highlighting?

The reason I want this is that sometimes I use the Maple and Matlab interfaces directly in the notebook, and the syntax highlighting looks wrong here. I know I can remove it manually after creating the tex file, but I would like to be able to disable it instead.

Another small question: Is it possible to disable the "attachfile"? Of course this is not so important since it requires removing 1 or 2 lines, but it could be nice though...

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2011-09-05 09:42:06 +0100

kcrisman gravatar image

updated 2011-09-06 09:44:29 +0100

Kabi, this is all possible, but for now you have to do it all yourself by modifying sws2tex.py. I have done this myself at times.

As you noted, removing the "attachfile" is very simple. I usually also do

shutil.copyfile(temp_dir + os.sep + output_filename + ".tex", output_filename + ".tex")

so I have the original TeX source to look at (or modify).

The easiest way to remove the syntax highlighting is to comment out all the stuff about colorizing, like so (I also do this):

#try:
#    from pygments import highlight
#    from pygments.lexers import PythonLexer, TexLexer, HtmlLexer
#    from pygments.formatters import LatexFormatter
<snip>
#except ImportError:
#    print "Warning: Unable to load module pygments"
#    print "Warning: Syntax highlighting will be disabled"
#
#    def colorize(s, **kwds):
#        return "\\begin{Verbatim}[commandchars=@\\[\\]]\n" + s + "\n\\end{Verbatim}\n"

def colorize(s, **kwds):
    return "\\begin{Verbatim}\n" + s + "\n\\end{Verbatim}\n"

Or you can do your own colorizing. It's also possible to remove this from the place where the stuff happens in the methods for cells.


It's great to hear you use this, by the way; sws2tex is not part of Sage yet, but the hope is that it would eventually be ready for inclusion, and comments like this for options are great to hear.

edit flag offensive delete link more
1

answered 2011-09-06 06:04:56 +0100

Kabi gravatar image

updated 2011-09-06 06:30:13 +0100

Thanks!

I have now changed several things in the sws2tex.py file including disabling syntax highlighting.

It is practical in the notebook to be able to use %hide and then %latex in the second line or only %latex, and then only have the pure latex code included in the tex file without adding the input cell with latex code in a verbatim environment. I achieve this with sws2tex by changing the following in the, class InputCell(Cell):

def _detect_language(self):
    first_line = self.text.splitlines()[0]
    try:
        second_line = self.text.splitlines()[1]    
    except: 
        second_line = ""
    if first_line == '%latex':
        self.language = 'latex'
    elif second_line == '%latex':
        self.language = 'latex'
    ...

and

def latex(self):
    if options.hide:
        first_line = self.text.splitlines()[0]
        if first_line == '%hide':
            return ""
    if self.language == 'latex':
        return ""
    ...

Another thing for people having problems with special letters like æ, ø or å in danish: One solution is to add the following as the first line in html2latex.py:

# -*- coding: utf-8 -*-

and the following to convert the letters from the html to utf-8:

entities = {
    ...
    "aelig": 'æ',
    "oslash": 'ø',
    "aring": 'å',
    "AElig": 'Æ',
    "Oslash": 'Ø',
    "Aring": 'Å'

With these changes the sage notebook becomes a near complete math/report environment for me. sws2tex makes it possible to produce very good paper print of your work done in the notebook, which (in my opinion) is not the case with the existing print function with jsMath directly from the notebook.

So thanks for sws2tex and keep up the good work! :-)

edit flag offensive delete link more

Comments

Well, I'm definitely not responsible for more than one character of the actual code, but I have used it a fair amount and love seeing people use it. Glad it's helping!

kcrisman gravatar imagekcrisman ( 2011-09-06 09:57:09 +0100 )edit

@Kabi: By the way, I assume you *are* aware of the TinyMCE editor, which uses jsmath and which does render properly in sws2tex? I rarely need %latex in the notebook for this reason, though I occasionally use the latex() method of some Sage object to put it in a longer computational cell.

kcrisman gravatar imagekcrisman ( 2011-09-06 09:58:31 +0100 )edit

I have problems with the TinyMCE editor when wanting to use the "align" environment. In the editor you need to enclose latex math in $$ or \\[\\], but in the converted latex code you will get, \\[ \begin{align} ..., which will fail when compiling the document. So I use the TinyMCE for normal text and simple math stuff and then the %latex cell for more complicated math expressions.

Kabi gravatar imageKabi ( 2011-09-06 19:07:46 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-09-03 10:07:46 +0100

Seen: 944 times

Last updated: Sep 06 '11