📄 teximage.m
字号:
'" -D ' int2str(res) ' -E -o "' ps_file ...
'" "' dvi_file '"']);
if ~exist(ps_file)
ind1 = findstr('!',r);
if ~isempty(ind1)
r = r(ind1:end);
end
error(['Error running DVIPS: ' r]);
end
function bits = ghostscript(file,res,exe_ext);
origpsfile = [file '.ps'];
psfile = [file '2.ps'];
%some postscript hacking to translate the equation to 0,0
origps_fid = fopen (origpsfile, 'r');
F=fread(origps_fid); % read the whole thing in
fclose(origps_fid);
F=char(F');
bbox=findstr('BoundingBox',F) + 13; % skip to start of numbers
bboxstr = F(bbox:bbox+50);
bbox = sscanf(bboxstr,'%d',4); % read in bounding box
endsetup=findstr('EndSetup',F)+8;
while isspace(F(endsetup))
endsetup = endsetup + 1;
end
F=[F(1:endsetup-1) int2str(-bbox(1)) ...
' ' int2str(-bbox(2)) ' translate ' F(endsetup:end)];
ps_fid = fopen (psfile, 'w');
fwrite(ps_fid,F);
fclose(ps_fid);
% now go and rasterize the translated file
pcx_file = [file '.pcx'];
rsp_file = [file '.rsp'];
rsp_fid = fopen (rsp_file, 'w');
ghostDir = fullfile( matlabroot, 'sys', 'ghostscript' );
if ~exist(ghostDir)
error( ['Can not find the directory for Ghostscript in ' matlabroot] )
end
fprintf(rsp_fid, '-dNOPAUSE -q \n');
v = version;
if v(1) >= '6'
fprintf(rsp_fid, '-I"%s"\n', fullfile( ghostDir, 'ps_files'));
fprintf(rsp_fid, '-I"%s"\n', fullfile( ghostDir, 'fonts'));
fprintf(rsp_fid, '-sOutputFile="%s"\n', pcx_file );
else
fprintf(rsp_fid, '-I%s\n', fullfile( ghostDir, 'ps_files',' '));
fprintf(rsp_fid, '-I%s\n', fullfile( ghostDir, 'fonts',' '));
fprintf(rsp_fid, '-sOutputFile=%s\n', pcx_file );
end
fprintf(rsp_fid, '-sDEVICE=pcxmono\n');
width = ceil(bbox(3)-bbox(1))*res/72;
height = ceil(bbox(4)-bbox(2))*res/72;
fprintf( rsp_fid, ['-g' int2str(width) 'x' int2str(height) '\n'] );
fprintf( rsp_fid, ['-r' int2str(res) '\n'] );
fclose(rsp_fid);
if isunix
gsPath = fullfile(ghostDir,'bin',getenv('ARCH'));
else
gsPath = fullfile(ghostDir,'bin','win32');
if ~exist(gsPath,'dir')
gsPath = fullfile(ghostDir,'bin','nt');
end
end
[s, r] = run_system_cmd( ['echo quit | "' fullfile(gsPath, ['gs' exe_ext])...
'" "@' rsp_file '" "' psfile '"']);
if ~exist(pcx_file)
error(['Error running Ghostscript: ' r]);
end
bits = double(imread(pcx_file,'pcx'));
function im = makeimage(p,color,antialias,csize,angle)
p = rot90(p,angle);
mask = p == 1;
F = repmat(1/(csize*csize),[csize csize]);
if isa(color,'char')
switch lower(color)
case 'y'
color = [1 1 0];
case 'm'
color = [1 0 1];
case 'c'
color = [0 1 1];
case 'r'
color = [1 0 0];
case 'g'
color = [0 1 0];
case 'b'
color = [0 0 1];
case 'w'
color = [1 1 1];
case 'k'
color = [0 0 0];
case 'none'
color = [1 1 1];
end
end
if (color(1) ~= color(2)) | (color(2) ~= color(3))
p1 = p; p1(mask) = color(1);
p2 = p; p2(mask) = color(2);
p3 = p; p3(mask) = color(3);
if isequal(antialias,'on')
p1 = doblur(p1,F);
p2 = doblur(p2,F);
p3 = doblur(p3,F);
end
im = cat(3,p1,p2,p3);
else
p(mask) = color(1);
if isequal(antialias,'on')
p = doblur(p,F);
end
im = repmat(p,[1 1 3]);
end
function out = doblur(p,F)
out = conv2(p,F,'valid'); % reduces p
out(out>1) = 1;out(out<0) = 0;
function menu = makecontextmenu(h)
menu = uicontextmenu;
item = uimenu('parent',menu,'label','Increase Size','callback',...
'teximage(''increasesize'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','Decrease Size','callback',...
'teximage(''decreasesize'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','Sharpen','callback',...
'teximage(''sharpentex'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','Blur','callback',...
'teximage(''blurtex'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','AntiAlias','callback',...
'teximage(''togglealias'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','Background');
sub = uimenu('parent',item,'label','White','callback',...
'teximage(''whitecolor'',gcbo)','userdata',h);
sub = uimenu('parent',item,'label','Figure Color','callback',...
'teximage(''figurecolor'',gcbo)','userdata',h);
sub = uimenu('parent',item,'label','Transparent','callback',...
'teximage(''transparent'',gcbo)','userdata',h);
item = uimenu('parent',menu,'label','Angle');
sub = uimenu('parent',item,'label','Rotate Counterclockwise','callback',...
'teximage(''rotcounterclock'',gcbo)','userdata',h);
sub = uimenu('parent',item,'label','Rotate Clockwise','callback',...
'teximage(''rotclockwise'',gcbo)','userdata',h);
function setantialiasmenu(h,antialias)
menu = get(h,'uicontextmenu');
ch = get(menu,'children');
set(ch(3),'checked',antialias);
function startmove(h)
ax=get(h,'parent');
fig=get(ax,'parent');
setappdata(fig,'teximagemotion',h);
setappdata(h,'mousemove',get(fig,'windowbuttonmotionfcn'));
setappdata(h,'mouseup',get(fig,'windowbuttonupfcn'));
fpos = convertUnits(get(fig,'units'),get(fig,'position'),'inches');
cp = convertUnits(get(fig,'units'),[get(fig,'currentpoint') 10 10],...
'inches',fpos,'inches');
setappdata(h,'currentpos',cp(1:2));
set(fig,'windowbuttonmotionfcn','teximage(''mousemove'',gcbo)');
set(fig,'windowbuttonupfcn','teximage(''endmove'',gcbo)');
function mousemove(fig)
h = getappdata(fig,'teximagemotion');
ax=get(h,'parent');
apos = get(ax,'position');
oldpos = getappdata(h,'currentpos');
funits = get(fig,'units');
fpos = convertUnits(funits,get(fig,'position'),'inches');
cp = convertUnits(funits,[get(fig,'currentpoint') 10 10],'inches',...
fpos,'inches');
newpos = cp(1:2);
diffs = newpos-oldpos;
apos(1:2) = apos(1:2) + diffs;
set(ax,'position',apos);
setappdata(h,'currentpos',newpos);
oldcenter = getappdata(h,'position');
cunits = getappdata(h,'units');
cp = convertUnits(cunits,[oldcenter 10 10],'inches',fpos,'inches');
cp(1:2) = cp(1:2) + diffs;
cp = convertUnits('inches',cp,cunits,fpos,'inches');
setappdata(h,'position',cp(1:2));
function endmove(fig)
h = getappdata(fig,'teximagemotion');
set(fig,'windowbuttonmotionfcn',getappdata(h,'mousemove'));
set(fig,'windowbuttonupfcn',getappdata(h,'mouseup'));
function docallback(s,menuh)
h = get(menuh,'userdata');
fig = get(get(h,'parent'),'parent');
op=get(fig,'pointer');
set(fig,'pointer','watch');
switch (s)
case 'increasesize'
teximage(h,'scale',getappdata(h,'scale') * 1.25);
case 'decreasesize'
teximage(h,'scale',getappdata(h,'scale') / 1.25);
case 'sharpentex'
teximage(h,'convolution',getappdata(h,'convolution') - 2);
case 'blurtex'
teximage(h,'convolution',getappdata(h,'convolution') + 2);
case 'whitecolor'
teximage(h,'background','w');
case 'figurecolor'
teximage(h,'background',get(fig,'color'));
case 'transparent'
teximage(h,'background','none');
case 'togglealias'
a = getappdata(h,'antialias');
if isequal(a,'on')
teximage(h,'antialias','off');
else
teximage(h,'antialias','on');
end
case 'rotcounterclock'
teximage(h,'rotation',getappdata(h,'rotation') + 90);
case 'rotclockwise'
teximage(h,'rotation',getappdata(h,'rotation') - 90);
end
set(fig,'pointer',op);
function figuremoved(fig);
fpos = convertUnits(get(fig,'units'),get(fig,'position'),'inches');
H = findall(fig,'type','image','tag','teximage');
H=H(:)';
for h=H
ax = get(h,'parent');
hunits = getappdata(h,'units');
if hunits(1) == 'n'
apos = get(ax,'pos');
pos = getappdata(h,'position');
center = convertUnits(hunits,[pos 10 10],'inches',fpos,'inches');
apos(1:2) = center(1:2) - .5*apos(3:4);
set(ax,'pos',apos);
end
end
function texbin = locateTeX
texbin = 0;
if isunix
[s,path] = unix('which tex');
if s == 0 % tex is on the system path so
texbin = ''; % we don't need to include an explicit path
end
else
% first try looking for MiKTeX
try
texbin = winqueryreg('HKEY_LOCAL_MACHINE',...
'SOFTWARE\MiK\MiKTeX\CurrentVersion\MiKTeX','Install Root');
catch
return
end
texbin = fullfile(texbin,'miktex','bin');
end
function texbin = promptForTeX(exe_ext)
oldcd = pwd;
cd(matlabroot); % so that the user doesn't start navigating
% from the TEMP directory.
[file,texbin] = uigetfile(['*' exe_ext],'Locate TeX Executable');
cd(oldcd);
function val = getdefault(ishndl,s,str,default)
if ishndl
val = getappdata(s,str);
elseif Lispref('teximage',str)
val = Lgetpref('teximage',str);
else
val = default;
end
function [s,r] = run_system_cmd(cmd);
if isunix
[s, r] = unix(cmd);
else
[s, r] = dos(cmd);
end
function val2 = convertUnits(units1,val1,units2,ref,refunits)
%Persistent figure so that we can get the figure position
% in inches instead of its units. This way we don't change
% the original figure. Also used to convert any units.
persistent teximage_figure_for_units;
persistent teximage_axes_for_units;
if isempty(teximage_figure_for_units)
teximage_figure_for_units = figure('visible','off',...
'handlevis','off',...
'integerhandle','off');
teximage_axes_for_units=axes('parent',teximage_figure_for_units);
end
set(teximage_figure_for_units,'units','inches');
if nargin == 3
set(teximage_figure_for_units,'units',get(0,'Units'));
set(teximage_figure_for_units,'position',get(0,'ScreenSize'));
else
set(teximage_figure_for_units,'units',refunits);
set(teximage_figure_for_units,'position',ref);
end
set(teximage_axes_for_units,'units',units1);
set(teximage_axes_for_units,'position',val1);
set(teximage_axes_for_units,'units',units2);
val2 = get(teximage_axes_for_units,'position');
function out = canreusebits(h,resolution,displaymode,s)
res = getappdata(h,'resolution');
disp = getappdata(h,'displaymode');
str = getappdata(h,'texstring');
out = isequal(res,resolution) & isequal(disp,displaymode) & ...
isequal(str,s);
% preferences API that works on both R11 and R12
function out = Lispref(s1,s2)
oldwarn = warning;
warning('off');
out = logical(0);
try
out = ispref(s1,s2);
catch
if isappdata(0,s1)
h = getappdata(0,s1);
out = isfield(h,s2);
end
end
warning(oldwarn);
function out = Lgetpref(s1,s2)
oldwarn = warning;
warning('off');
try
out = getpref(s1,s2);
catch
if isappdata(0,s1)
h = getappdata(0,s1);
out = getfield(h,s2);
else
error(['No preference for ' s2]);
end
end
warning(oldwarn);
function Lsetpref(s1,s2,val)
oldwarn = warning;
warning('off');
try
setpref(s1,s2,val);
catch
setappdata(0,'teximage',struct(s2,val));
disp('Copy the following line into your startup.m:');
if isa(val,'char')
disp(['setappdata(0,''teximage'',struct(''' s2 ''', ''' val '''));']);
end
end
warning(oldwarn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -