Prettify function - font_style

Hello,
Some time ago I used a function prettify by Pierre Vuillemin which enhances some features of plots. It seems to change the initial font_style of the plot element, but I can’t find it in the code. In my plot before “prettifing” I have italic Times New Roman font, but after that it changes to normal font.
Here’s the code which was shared by the Author in the Scilab users mailing list. Can you see here any line which can influence the font_style?

function prettify(e,options)
   // Attempts to make a handle (e.g. a figure) pretty for articles.
   //
   // Syntax
   //    prettify(h)
   //    prettify(h, opt)
   //
   // Parameters
   //    h     : the thing to prettify (handle), e.g. a figure handle
   //    opt   : the options (structure)
   //
   // Description
   //    Given a handle, this routines attempts:
   //       - to transform the text it finds in latex,
   //       - to increase the font sizes,
   //       - to increase the thickness of lines.
   //
   //    Default values (of thickness, font sizes, etc.) can be modified by giving
   //    a structure containing the options to modify. 
   //    The available options are:
   //       - title_font_size (4),
   //       - labels_font_size (3),
   //       - thicks_font_size (2),
   //       - leg_font_size (3),
   //       - line_thickness (2).
   //       - xstring_font_size (2)
   //       - num_format ('')
   //
   // Example
   //    n = 1000;
   //    x = linspace(0,2*%pi,n);
   //    plot(x,sin(x),x,cos(x))
   //    h = gcf()
   //    title('Figure of $f_1(x)$ and $f_2(x)$')
   //    xlabel('$x$ (rad)')
   //    ylabel('$y$')
   //    legend('$f_1(x) = sin(x)$','$f_2(x) = cos(x)$')
   //    opt.line_thickness = 3
   //    opt.labels_font_size = 4
   //    prettify(h, opt)
   //
   
   // Options handling .........................................................
   if argn(2) < 2 then
      options = struct()
   end
   default_options = struct('title_font_size'   ,  4,...
                            'labels_font_size'  ,  3,...
                            'thicks_font_size'  ,  2,...
                            'num_format'        ,  '',...
                            'leg_font_size'     ,  3,...
                            'line_thickness'    ,  2,...
                            'xstring_font_size' ,  2)
   // Assignation of default options
   if ~and(isdef(fieldnames(default_options))) then
      options = fill_options(options, default_options)
      extract_fields(options)
   end
   // Switch case on the type of handle that has been provided .................
   select convstr(e.type,'l')
   case 'figure'
      prettify_fig(e)
   case 'axes'
      prettify_axes(e)
   case 'polyline'
      prettify_polyline(e)
   case 'legend'
      prettify_legend(e)
   case 'compound'
      prettify_compound(e)
   case 'text'
      prettify_text(e)
   end
endfunction

function prettify_text(t)
   t.font_size = xstring_font_size
endfunction

function prettify_fig(f)
   f.anti_aliasing   = '8x'
   for i = 1:length(f.children)
      prettify(f.children(i))
   end
endfunction

function prettify_axes(ax)
   // Axis thicks
   ax.font_size      = thicks_font_size
   ax.x_ticks.labels = latexify(ax.x_ticks.labels)
   ax.y_ticks.labels = latexify(ax.y_ticks.labels)
   ax.z_ticks.labels = latexify(ax.z_ticks.labels)
   // Axis labels
   labels_name       = ['x_label','y_label','z_label']
   for name = labels_name
      label             = ax(name)
      label.font_size   = labels_font_size
      label.text        = latexify(label.text)
   end
   // Title
   T                 = ax.title
   T.text            = latexify(T.text)
   T.font_size       = title_font_size
   //
   for i = 1:length(ax.children)
      prettify(ax.children(i))
   end
endfunction

function prettify_polyline(po)
   po.thickness      = line_thickness
endfunction

function prettify_legend(leg)
   leg.font_size     = leg_font_size
   leg.text          = latexify(leg.text)
endfunction

function prettify_compound(co)
   for i = 1:length(co.children)
      prettify(co.children(i))
   end
endfunction

function str = latexify(str)
   if str == '' then
      return
   end
   // Adds $ at the beginning and the end of a matrix of strings
   for i = 1:size(str,1)
      str(i) = '$' + wrap_in_text(str(i)) + '$'
   end
endfunction

function out_str = wrap_in_text(str)
   // In a string containing normal text and latex expressions between $, this
   // routines wraps the text part inside \text{.} macros
   // '$f(x) = sin(x)$ is a nice function' --> 'f(x) = sin(x)\text{ is a nice function}'
   str   = stripblanks(str)
   T     = tokens(str, '$')
   if part(str,1) ~= '$' then
      start = 1
   else
      start = 2
   end
   for i = start:2:size(T,1)
      t = T(i,:)
      if isnum(T(i,:)) & ~isempty(num_format)
         t = sprintf(num_format,evstr(t))
      end
      T(i,:) = '\text{'+t+'}'
   end
   out_str = ''
   for i = 1:size(T,1)
      out_str = out_str + T(i,:)
   end
endfunction

function default = fill_options(opt, default)
   opt_fields  = fieldnames(opt)
   def_fields  = fieldnames(default)
   // Displays a warning if some fields in opt are not available options
   wrong_opt   = setdiff(opt_fields, def_fields)
   if ~isempty(wrong_opt) then
      warn_msg       = strcat(wrong_opt',''', ''')
      disp('Warning: option(s) '''+warn_msg+''' are invalid. Discarding.')
      possible_opt   = strcat(def_fields',''', ''')
      disp('Possible values are: '''+possible_opt+'''.')
   end
   // Replace default options with valid user-supplied options
   for f = intersect(def_fields, opt_fields)
      default(f) = opt(f)
   end
endfunction

function extract_fields(stru)
   varNames    = fieldnames(stru)'
   varValues   = []
   for v = varNames
      varValues   = [varValues,'stru.'+v+'']
   end
   exStr = '[' + strcat(varNames,',') + '] = resume(' + strcat(varValues,',')+');'
   execstr(exStr)
endfunction

prettify(h, opt)

Regards,
Iza

Hello,

When you use LaTeX rendering in Scilab you have to enclose the whole string between a leading and a trailing dollar. The wrap_in_text function above does consider strings that would not render otherwise, like in

title('Figure of $f_1(x)$ and $f_2(x)')

It changes a posteriori the title text (by accessing it with its handle) to

'$\text{Figure of }f_1(x)\text{ and }f_2(x)$'

There is no influence of the font_style property on LaTeX rendering, which uses the Computer Modern font set, with by default roman upright for the text and italic for the math formulas.

S.

Thank you, now it is clear.
How can I change all texts into italic in the prettify function?

Change T(i,:) = '\text{'+t+'}' to T(i,:) = '\text{\it '+t+'}'.

But IMHO having both text and math in italic is not pretty…

Thank you very much. In this plot I have no math, so I hope it will still be pretty :wink:

Prettify is a good one. I like it.

Sorry, but I just tested your version on the example attached to the code and it doesn’t work. As I understand all the texts should be in italic but the title and ticks labels are normal.
fig_prettify_italic

Hello,

Please include in your post the code you used to produce this plot.

S.

Hello, this is an example included in the beginning of the function code:

Example
n = 1000;
x = linspace(0,2*%pi,n);
plot(x,sin(x),x,cos(x))
h = gcf()
title('Figure of $f_1(x)$ and $f_2(x)$')
xlabel('$x$ (rad)')
ylabel('$y$')
legend('$f_1(x) = sin(x)$','$f_2(x) = cos(x)$')
opt.line_thickness = 3
opt.labels_font_size = 4
prettify(h, opt)

When I replace T(i,:) = '\text{'+t+'}' by T(i,:) = '\text{\it '+t+'}' in function wrap_in_text I get the following output:

Graphic window number 0

Thank you. Unfortunately it doesn’t work like that in my computer. Maybe it’s the version of Scilab? I’m using 6.0.2.

I just tested in 6.0.2 version and I get the above graph (everything in italic). I use your above script where I replaced T(i,:) = '\text{'+t+'}' by T(i,:) = '\text{\it '+t+'}' in function wrap_in_text. You have to run again the script where you have the definitions of prettify functions before retrying your example, otherwise you will still have the original definition of wrap_in_text.

S.

Sorry for the problem. I have prettify function in my library, so I had to change the names of all the functions in the script to build a new version prettify_italic. I started with changing only the main function name and that’s why it didn’t work. Thanks for your patience and help.