Conversion to LaTeX of unicode symbols in math roles

184 views
Skip to first unread message

Roberto Bonvallet

unread,
Oct 28, 2010, 8:50:44 PM10/28/10
to sphin...@googlegroups.com
Dear all,
I've been using Sphinx for creating online handouts for university programming courses. I like to type unicode characters directly in the source code; for example:

    The suit is ♠. The floor of `x` is `⌊x⌋`.

(I set :math: as the default role in conf.py). In the HTML output, this works beautifully for both regular text and jsmath equations.

Today I tried to generate a LaTeX version of the handout, and some symbols (like card suits) weren't properly converted to their LaTeX counterparts. I wrote this little extension that does the mapping: http://gist.github.com/652626

Unfortunately, this does not work for symbols within math roles. In the example above, the suit is converted, but the floor delimiters aren't. The workaround would be to start using `\lfloor x\rfloor` in my .rst source file, but I really want to keep the cleaner version above. Typing `Δω` by using vim digraphs is much more convenient than `\Delta\omega`.

Is there a simple way to map unicode symbols within math roles to their LaTeX versions?

Best regards.
--
Roberto Bonvallet

Guenter Milde

unread,
Nov 1, 2010, 10:03:48 AM11/1/10
to sphin...@googlegroups.com
On 2010-10-29, Roberto Bonvallet wrote:

...


> I like to type unicode characters directly in the source code; for
> example:

> The suit is =E2=99=A0. The floor of `x` is `=E2=8C=8Ax=E2=8C=8B`.

> (I set :math: as the default role in conf.py). In the HTML output, this
> works beautifully for both regular text and jsmath equations.

> Today I tried to generate a LaTeX version of the handout, and some symbols
> (like card suits) weren't properly converted to their LaTeX counterparts. I
> wrote this little extension that does the mapping:
> http://gist.github.com/652626

> Unfortunately, this does not work for symbols within math roles. In the
> example above, the suit is converted, but the floor delimiters aren't. The
> workaround would be to start using `\lfloor x\rfloor` in my .rst source

> file, but I really want to keep the cleaner version above. Typing `=CE=94=
>=CF=89` by


> using vim digraphs is much more convenient than `\Delta\omega`.

> Is there a simple way to map unicode symbols within math roles to their
> LaTeX versions?

I suppose that the UnicodeChar -> LaTeX conversion only works for
text. To get a similar setup for math, you will need to
configure/extend/modify the math extension (mathbase.py in shinx/ext).

(This way you can also set up separate conversions for text- and
math-mode avoiding a lot of \ensuremath.)

Günter

Marcin Wojdyr

unread,
Nov 12, 2010, 6:59:00 AM11/12/10
to sphinx-dev, Roberto Bonvallet
On Oct 29, 1:50 am, Roberto Bonvallet <rbon...@gmail.com> wrote:
>
> Is there a simple way to map unicode symbols within math roles to their
> LaTeX versions?

You can use latex_elements['utf8extra'] in conf.py, e.g.:

latex_elements = {
'utf8extra': r"""
\DeclareUnicodeCharacter{00A0}{\nobreakspace}
\DeclareUnicodeCharacter{03C3}{\ensuremath{\sigma}}
\DeclareUnicodeCharacter{2212}{\ensuremath{-}}
"""
}

03C3 is σ.

Marcin

Roberto Bonvallet

unread,
Nov 13, 2010, 11:39:50 PM11/13/10
to Marcin Wojdyr, sphinx-dev
Marcin Wojdyr wrote:
You can use latex_elements['utf8extra'] in conf.py, e.g.:

Dear Marcin,
thanks for the idea. Instead of declaring the characters myself, I took advantage of the ucs package:

latex_elements = {
    'inputenc': r'''
        \usepackage{ucs}
        \usepackage[utf8x]{inputenc}''',
    'utf8extra': '',
}

Best regards, 
--
Roberto Bonvallet
Reply all
Reply to author
Forward
0 new messages