function texWarning=renderTex2Emf(equationText,fontSize,fileName)
% This function converts a Tex input to a windows vector emf vector file.
%
% texWarning=renderTex2Emf(equationText,fontSize,fileName)
%
% It is a slighty modified version of renderTex in publish.m
% It allows having nice equations in word documents.
%
% How to use it:
%
% equationText is the input string.
%
% fontSize optional font size in pts (default 12)
%
% fileName optional file where to save the file.
%
% texWarning output of the tex Parser
%
% renderTex2Emf('$$ \frac{2}{x} +\int_0^2 x^2 dx$$');
% generate a 12 pts equation and save it into the cilpboard.
% for instance, you can paste it into a Word document.
%
% renderTex2Emf('$$ \frac{2}{x} +\int_0^2 x^2 dx$$',22);
% same with a 22 pts eqution.
%
% renderTex2Emf('$$ \frac{2}{x} +\int_0^2 x^2 dx$$',22,'myEquation.emf');
% save it to the 'myEquation.emf' file instead of the clipboard.
%
% NOTE ON FONTS: in order to export Tex equations to printers, you need to
% install the ams fonts on your system :
% for PC systems : control panel -> fonts and 'file - >add'
% select the fonts located in
% <matlabroot>\sys\fonts\ttf\cm
% <matlabroot>\sys\fonts\type1\cm
% and once the fonts are included, reboot.
%
% Mathias Ortner, 2007.
% default parameter
if(~exist('fontSize'))
fontSize=12
end
% Create a figure for rendering the equation.
hFigure = figure('handlevisibility','off', ...
'integerhandle','off', ...
'visible','off', ...
'paperpositionmode', 'auto', ...
'color','w');
hAxes = axes('position',[0 0 1 1], ...
'parent',hFigure, ...
'xtick',[],'ytick',[], ...
'xlim',[0 1],'ylim',[0 1], ...
'visible','off');
hText = text('parent',hAxes,'position',[.5 .5], ...
'horizontalalignment','center','fontSize',fontSize, ...
'interpreter','latex');
% Render and snap!
[lastMsg,lastId] = lastwarn('');
set(hText,'string',equationText);
if ispc
% The font metrics are not set properly unless the figure is visible.
set(hFigure,'Visible','on');
end
% We adapt the figure size to the equation size, in order to crop it
% properly when printing. The following lines allow to respect the font
% size.
textDimension = get(hText,'Extent');
figureDimension = get(hFigure,'Position');
newWidth=textDimension(3)*figureDimension(3);
newHeight=textDimension(4)*figureDimension(4);
set(hFigure,'Position',[figureDimension(1) figureDimension(2) newWidth newHeight]);
% Draw the figure
drawnow;
texWarning = lastwarn;
lastwarn(lastMsg,lastId)
% and save it
if(~exist('fileName'))
print(hFigure,'-dmeta');
else
saveas(hFigure,fileName,'emf');
end
close(hFigure);