2011-02-10 122 views
9

我正在使用包括Matlab绘制的数字到LaTeX中。我平时的工作流程如下:如何在Matlab图中使用非ASCII字符(用于LaTeX doc)?

  1. 脚本在MATLAB中创建人物(S),
  2. 我调整我觉得需要在可视化图形编辑器进行调整,
  3. 图保存为.FIG(用于未来的修改)和.eps(包括在LaTeX中),
  4. 我将.eps文件转换为.pdf,
  5. PDF文件在LaTeX源代码中被引用。

的一点是:当我尝试在轴标签使用,传说,标题等非ASCII字符,(准确的说:波兰国家字符如“A”,“E”, 'ś','ć')在Matlab图形编辑器中编码很好,字符显示正确。在输出到.eps后,它们都是错误的(例如:“Głębokość”变成“G³êbokoœæ”)。

有没有一种方法可以正确地做到这一点,无论是通过调整Matlab选项或改变我的工作流程?

注:我发现,出口png格式或其他非矢量格式正确处理字符编码,但我想,以避免做 - 我要求的方式来“养它矢量“。直接导出到.pdf产生与.eps相同的效果,例如。它产生了错误的结果。

PS。 Matlab是R2008a,.latex文件是用pdfText,.eps文件和MikTeX 2.9(全部在Win7下)的epstopdf编译的。

回答

8

你可以看看psfrag,这就是我在LaTeX中尝试使用Matlab图形时通常使用的。你基本上只是把标签放在Matlab的图中,然后用LaTeX文本替换这些标签。最大的好处是可以让你在文字和数字中拥有相同的符号。

编辑:当寻找psfrag-URL时,我找到了一个Matlab脚本来简化它: LaPrint

+0

我以某种方式忽略了使用pdflatex的信息。对于不那么丑陋的解决方法你有什么想法吗? – triazotan 2011-02-10 16:06:14

+0

Matlab可以直接在文本字段中解释乳胶。您是否曾尝试在matlab文本字段中放置像“\'c”这样的胶乳构造,并将解释器设置为“latex”而不是直接输入“ć”? – groovingandi 2011-02-10 18:05:49

4

另一种可能的解决方案是使用matlab2tikz。它会创建一个tikz/pgfplot源文件,该文件可能由您的胶乳源直接包含。这意味着它使用LaTeX的设施进行字体渲染。您可以直接编辑生成的文件来调整标签等。不幸的是,它不适用于所有的MATLAB数字。

1
char(2048) will be shown by `print -depsc` as 'à ', 
char(5064) as 'á', 
char(28808) as 'ç', 
char(37000) as 'é', 
char(32904) as 'è', ... 

对于latin1的字符集的其他字符,看:

for j=0:4*64;clf;subplot(1,1,1);plot(eye(2));leg='';for i=4*(j+1)-1:-1:max(1,4*j); 
str=['  ',num2str(i*64)];leg(i,:)=[str(end-4:end),':',char(64*i+(0:63))]; 
end; 
title(leg,'interpreter','none');print('-depsc',['ascii',num2str(j),'.ps']); 
end; 

我使用pdflatex,所以psfrag是不是一种选择,并pdfrack似乎被打破。

0

对于用非ASCII ISO-8859-1字符导出Matlab图形,在Windows上没有问题,但在具有UTF-8语言环境的Linux上有Matlab错误和workaround。这里的问题针对的是不在ISO-8859-1中的字符,这更加棘手。以下是我发布在相关question上的解决方案。

如果需要的字符数小于256(8位格式),理想地在标准的编码集,那么一种解决方案是:

  1. 转换八进制代码到Unicode字符;
  2. 将文件保存到目标编码标准(以8位格式);
  3. 添加目标编码集的编码矢量。

例如,如果要导出波兰语文本,则需要将该文件转换为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位编码标准。