Inserting a unicode text into pyx canvas

2019-06-04 01:55发布

问题:

I have a set of UTF-8 characters that I would like to insert into a PyX generated pdf file. I have included # -*- coding: utf-8 -*- to top of the file. The code is somewhat similar to the following:

# -*- coding: utf-8 -*-
c = canvas.canvas()
txt = "u'aあä'"
c.text(2, 2, "ID: %s"%txt)
c.writeEPSfile("filename.eps")

But I still can't get my head around this.

Error:

'ascii' codec can't encode character u'\xae' in position 47: ordinal not in range(128)

回答1:

Try this:

# -*- coding: utf-8 -*-
c = canvas.canvas()
txt = u'aあä'.encode('utf-8')
c.text(1, 4, "UID: %s"%(txt))
c.writeEPSfile("filename.eps")


回答2:

You can setup PyX to pass unicode characters to (La)TeX. Then it all becomes a problem to produce the characters in question within TeX/LaTeX. Here is a rather minimal solution to produce the output in question:

from pyx import *

text.set(cls=text.LatexRunner, texenc='utf-8')
text.preamble(r'\usepackage[utf8]{inputenc}')
text.preamble(r'\usepackage{newunicodechar}')
text.preamble(r"\newunicodechar{あ}{{\usefont{U}{min}{m}{n}\symbol{'102}}}") 
text.preamble(r'\DeclareFontFamily{U}{min}{}')
text.preamble(r'\DeclareFontShape{U}{min}{m}{n}{<-> udmj30}{}')

c = canvas.canvas()
c.text(0, 0, 'UID: aあä')
c.writeGSfile('utf8.png')

This directly results in the output (as PNG as uploaded here):

Note that this was done using PyX 0.13 on Python 3 and a rather standard LaTeX installation. Also, I used some information from https://tex.stackexchange.com/questions/171611/how-to-write-a-single-hiragana-character-in-latex about creating those characters in LaTeX. There seem to be solutions like CJKutf8 to setup all kind of characters for direct use as unicode characters within LaTeX, but this is way out of my experience. Anyway, it should all work fine from within PyX like it does from LaTeX itself if all the setup has been done properly. Good luck!



回答3:

Maybe you can find an according set from the babel package I ran into the same error when I tried to insert the german ä (a umlaut). I simply added the german babel package:

text.preamble(r"\usepackage[ngerman]{babel}")

After that, this was possible without errors:

c.text(12, 34, "äöüßß")

I also used an utf8 input encoding, I think this it is necessary as well.

Further reading:
https://en.wikibooks.org/wiki/LaTeX/Internationalization
https://en.wikibooks.org/wiki/LaTeX/Fonts