# Prettify function - font\_style

**URL:** <https://scilab.discourse.group/t/prettify-function-font-style/229>\
**Category:** Graphics and plotting\
**Created:** [November 10, 2023, 2:38pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229 "2023-11-10T14:38:42Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 10, 2023, 2:38pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/1 "2023-11-10T14:38:42Z")

</div>

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?

```auto
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

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 10, 2023, 2:53pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/2 "2023-11-10T14:53:45Z")

</div>

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

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

```

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

```auto
'$\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.

```auto

```

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 10, 2023, 3:14pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/3 "2023-11-10T15:14:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 10, 2023, 3:19pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/4 "2023-11-10T15:19:03Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 10, 2023, 6:16pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/5 "2023-11-10T18:16:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![cfuttrup](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@cfuttrup](https://scilab.discourse.group/u/cfuttrup)\
**Post date:** [November 11, 2023, 6:19pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/6 "2023-11-11T18:19:38Z")

</div>

Prettify is a good one. I like it.

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 11, 2023, 6:31pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/7 "2023-11-11T18:31:58Z")

</div>

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](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/5be26d7b0453ebd511dcc2ffe91653e2b4d51293.png)

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 12, 2023, 9:29pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/8 "2023-11-12T21:29:07Z")

</div>

Hello,

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

S.

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 13, 2023, 10:23am UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/9 "2023-11-13T10:23:59Z")

</div>

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 13, 2023, 10:38am UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/10 "2023-11-13T10:38:29Z")

</div>

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](https://global.discourse-cdn.com/free1/uploads/utc/original/1X/f76967356fa8035021e7a99876f95d1a8303e908.svg)

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 13, 2023, 10:46am UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/11 "2023-11-13T10:46:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mottelet](https://yyz2.discourse-cdn.com/free1/user_avatar/scilab.discourse.group/mottelet/32/13_2.png) [@mottelet](https://scilab.discourse.group/u/mottelet)\
**Post date:** [November 13, 2023, 11:00am UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/12 "2023-11-13T11:00:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Iza](https://avatars.discourse-cdn.com/v4/letter/i/34f0e0/32.png) [@Iza](https://scilab.discourse.group/u/Iza)\
**Post date:** [November 13, 2023, 2:41pm UTC](https://scilab.discourse.group/t/prettify-function-font-style/229/13 "2023-11-13T14:41:45Z")

</div>

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.
