对于用非ASCII ISO-8859-1字符导出Matlab图形,在Windows上没有问题,但在具有UTF-8语言环境的Linux上有Matlab错误和workaround。这里的问题针对的是不在ISO-8859-1中的字符,这更加棘手。以下是我发布在相关question上的解决方案。
如果需要的字符数小于256(8位格式),理想地在标准的编码集,那么一种解决方案是:
- 转换八进制代码到Unicode字符;
- 将文件保存到目标编码标准(以8位格式);
- 添加目标编码集的编码矢量。
例如,如果要导出波兰语文本,则需要将该文件转换为ISO-8859-2。以下是Python(多平台)的实现:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,codecs
input = sys.argv[1]
fo = codecs.open(input[:-4]+'_latin2.eps','w','latin2')
with codecs.open(input,'r','string_escape') as fi:
data = fi.readlines()
with open('ISOLatin2Encoding.ps') as fenc:
for line in data:
fo.write(line.decode('utf-8').replace('ISOLatin1Encoding','MyEncoding'))
if line.startswith('%%EndPageSetup'):
fo.write(fenc.read())
fo.close()
另存为eps_lat2.py;然后运行命令python eps_lat2.py file.eps
,其中file.eps是由Matlab创建的eps,使用Latin-2编码创建file_latin2.eps。该文件ISOLatin2Encoding.ps包含encoding vector:
/MyEncoding
% The first 144 entries are the same as the ISO Latin-1 encoding.
ISOLatin1Encoding 0 144 getinterval aload pop
% \22x
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
/.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef
% \24x
/nbspace /Aogonek /breve /Lslash /currency /Lcaron /Sacute /section
/dieresis /Scaron /Scedilla /Tcaron /Zacute /hyphen /Zcaron /Zdotaccent
/degree /aogonek /ogonek /lslash /acute /lcaron /sacute /caron
/cedilla /scaron /scedilla /tcaron /zacute /hungarumlaut /zcaron /zdotaccent
% \30x
/Racute /Aacute /Acircumflex /Abreve /Adieresis /Lacute /Cacute /Ccedilla
/Ccaron /Eacute /Eogonek /Edieresis /Ecaron /Iacute /Icircumflex /Dcaron
/Dcroat /Nacute /Ncaron /Oacute /Ocircumflex /Ohungarumlaut /Odieresis /multiply
/Rcaron /Uring /Uacute /Uhungarumlaut /Udieresis /Yacute /Tcedilla /germandbls
% \34x
/racute /aacute /acircumflex /abreve /adieresis /lacute /cacute /ccedilla
/ccaron /eacute /eogonek /edieresis /ecaron /iacute /icircumflex /dcaron
/dcroat /nacute /ncaron /oacute /ocircumflex /ohungarumlaut /odieresis /divide
/rcaron /uring /uacute /uhungarumlaut /udieresis /yacute /tcedilla /dotaccent
256 packedarray def
这里是在Linux上另一种实现方式使用bash:
#!/bin/bash
name=$(basename "$1" .eps)
ascii2uni -a K "$1" > /tmp/eps_uni.eps
iconv -t ISO-8859-2 /tmp/eps_uni.eps -o "$name"_latin2.eps
sed -i -e '/%EndPageSetup/ r ISOLatin2Encoding.ps' -e 's/ISOLatin1Encoding/MyEncoding/' "$name"_latin2.eps
保存为eps_lat2;然后运行命令sh eps_lat2 file.eps
创建带有Latin-2编码的file_latin2.eps。
通过更改脚本中的编码矢量和iconv(或codecs.open)参数,可以轻松地将其适用于其他8位编码标准。
我以某种方式忽略了使用pdflatex的信息。对于不那么丑陋的解决方法你有什么想法吗? – triazotan 2011-02-10 16:06:14
Matlab可以直接在文本字段中解释乳胶。您是否曾尝试在matlab文本字段中放置像“\'c”这样的胶乳构造,并将解释器设置为“latex”而不是直接输入“ć”? – groovingandi 2011-02-10 18:05:49