1 <?xml version="1.0" encoding="UTF-8"?>
3 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
4 * Copyright (C) 2009 - 2010 - Calixte DENIZET
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
6 * Copyright (C) 2019 - Samuel GOUGEON
8 * This file is hereby licensed under the terms of the GNU GPL v2.0,
9 * pursuant to article 5.3.4 of the CeCILL v.2.1.
10 * This file was originally licensed under the terms of the CeCILL v2.1,
11 * and continues to be available under such terms.
12 * For more information, see the COPYING file which you should have received
13 * along with this program.
16 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
17 xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns3="http://www.w3.org/1999/xhtml"
18 xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
19 xmlns:scilab="http://www.scilab.org" xml:id="prettyprint" xml:lang="en">
21 <refname>prettyprint</refname>
23 Converts a Scilab object into some corresponding LaTeX, TeX, MathML or HTML strings
30 str = prettyprint(a, exportFormat)
31 str = prettyprint(a, exportFormat, delimiter)
32 str = prettyprint(a, exportFormat, delimiter, processByElement)
33 str = prettyprint(a, exportFormat, delimiter, processByElement, isWrapped)
37 <title>Arguments</title>
42 <para>a Scilab object.
45 Supported types: booleans, encoded integers, real or complex numbers,
46 polynomials and rationals with real or complex coefficients, strings,
47 cells, linear dynamical systems (as generated with syslin()),
51 Supported sizes: scalar, vector, matrix. Hypermatrices are not
52 supported (only the first page is processed.)
59 <term>exportFormat</term>
62 is the output format. Possible case-insensitive values are 'latex' (default),
63 'tex', 'mathml', 'html', or 'html4'.
68 <term>delimiter</term>
70 <para> is a string indicating the delimiter type to bracket the whole
71 resulting matrix. It is only used if <varname>processByElement</varname> is
72 false. It can be '(' (default), '{', '[', '|', '||', or '' if no bracketing
76 In HTML, the '_' delimiter value can be used to format the input matrix
77 without delimiter but with cells borders.
81 Delimiters used for the components defining a linear dynamical system
82 are always "(" and ")", even with <varname>delimiter</varname>="",
83 unless <varname>exportFormat</varname>="html4" is used: Then, "|"
84 is imposed as inner syslin delimiter. This is useful when the result
85 must be rendered in a Scilab GUI like <literal>messagebox()</literal>,
86 that supports only a restricted version of HTML.4
92 <term>processByElement</term>
94 <para> is a boolean: If set to <literal>%f</literal> (default), the result
95 <varname>str</varname> is a single string representing the whole input
96 matrix. Otherwise, <varname>str</varname> has as many elements as the input
97 matrix <varname>a</varname>.
102 <term>isWrapped</term>
105 is a boolean to indicate if the result must be wrapped inside technical
106 delimiters. The default value is %T (true).
107 The delimiters depend on the export format. It is
110 '$' for latex and tex
120 nothing if <varname>processByElement</varname> is
121 <literal>%T</literal> ;
125 <literal><table valign="middle" cellspacing="0" cellpadding="3" ..></literal>
126 otherwise. Note that the "</table>"
127 closure is then always included, even if
128 <varname>isWrapped</varname> is <literal>%F</literal>.
129 This allows to fully customize the table style and attributes,
130 instead of using the default ones.
142 <para> a single string (if <varname>processByElement</varname> is %F),
143 or a matrix of strings otherwise (with size(str)==size(a)):
144 the representation of the input object <varname>a</varname>.
151 <title>Description</title>
153 <literal>prettyprint()</literal> provides a formatted representation of a Scilab object.
154 The format can be TeX, LaTeX, MathML, or HTML.
157 The result can be used in third party applications, or within Scilab: Almost all graphic
158 functions requiring or accepting some text input support it as LaTeX expressions
159 (see <link linkend="math_rendering_features_in_graphic">Scilab graphic features</link>).
162 In addition, some GUI features accept and render HTML inputs. The special export format
163 'html4' must be used to display linear dynamical systems in a Scilab GUI like messagebox().
166 When an array of cells includes some matrices or other arrays, the same chosen delimiters
167 are used for the cells array and for its elements that are also arrays.
171 The output format of all decimal numbers included in the input object is driven by the
172 <literal>format()</literal> function.
176 <title>Rendering some MathML or/and LaTeX codes in a HTML page:</title>
178 Only the Firefox web browser supports natively the <math> MathML tag,
179 that can embed some mathML code, and renders it.
182 To render MathML code in <emphasis>any</emphasis> web browser,
185 put the following HTML instruction only once before the first use of
186 <literal><math></literal>:
187 <script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async/>.
188 This is typically done in the <literal><head>..</head></literal>
189 section of the HTML page.
192 put every set of MathML instructions between <literal><math>..</math></literal>.
198 The same <literal><script..></literal> instruction declaring the
199 <emphasis>MathJax</emphasis> package can be used to parse and render
200 LaTeX expressions directly written in the HTML code, embedded between
201 <literal>\[ ..\]</literal> brackets instead of
202 <literal><math>..</math></literal>.
205 Hence, the HTML code of a page can mix some LaTeX and MathML instructions
206 that will be parsed and rendered accordingly.
209 <para>Please see examples provided in the dedicated section below.</para>
213 <title>Examples</title>
214 <programlisting role="example"><![CDATA[
215 str = prettyprint(rand(3,3)) // Return the LaTeX representation of a 3,3 matrix
216 xstring(0.2,0.2,str) // Show the representation in a graphic Windows
218 prettyprint(rand(3,4),"mathml") // Return the MathML representation of a 3,4 matrix
219 prettyprint(rand(3,4),"mathml","[") // Return the MathML representation of a 3,4 matrix with '[' as delimiter
221 s=poly(0,'s'); G=[1,s;1+s^2,3*s^3];
222 xstring(0.2,0.2,prettyprint(G*s-1)); // Show a polynomial through a LaTeX representation
226 <emphasis role="bold">
227 Set of objects to be defined before using them in all next examples:
230 <programlisting role="example"><![CDATA[
231 bool = [ %t %t ; %f %t ]
233 26595 1212 4257 -4466 9784
234 -4226 3404 5743 3 10032
235 30471 6 14918 267 30 ])
236 num = [-123.45, %inf, 5.427e-123
238 cx = [complex(1,%nan), complex(-1.25d-12, 2)
239 complex(%inf,6.1e167), complex(1,-%inf)]
240 p = [[0*%z ; %z], (%i-%z).^[1 3;2 4]]; string(p)
241 text = ["André''s got 50% of 1430 $, & the ""remainder"" 1 month later."
242 "x=A\B is such that A*x=B, with A in {a<b, 1-a, ~a, ^a}. _#"]
243 ce = {["a b"; "cdefg"], %t, %z./[%z-1, %z^2+1] ; (1-%s)^3, %pi, int8(rand(2,3)*200)}
245 A = grand(3,3,"uin",-999,999)/100;
246 B = grand(3,2,"uin",-999,999)/100;
247 C = grand(2,3,"uin",-999,999)/100;
248 linsys = syslin("c", A, B, C);
250 L = list("Booleans:", bool, "int16:", i16, "Decimal numbers:", num, ..
251 "Complex numbers:", cx, "Polynomials:", p, "Text:", text, ..
252 "Cells array, embedding rationals:", ce, "Linear dynamical system:");
255 --> bool = [ %t %t; %f %t ];
261 > 26595 1212 4257 -4466 9784
262 > -4226 3404 5743 3 10032
263 > 30471 6 14918 267 30 ])
265 26595 1212 4257 -4466 9784
266 -4226 3404 5743 3 10032
270 --> num = [-123.45, %inf, 5.427e-123
277 --> cx = [complex(1,%nan), complex(-1.25d-12, 2)
278 > complex(%inf,6.1e167), complex(1,-%inf)]
280 1. + Nani -1.250D-12 + 2.i
281 Inf + 6.10D+167i 1. - Infi
284 --> p = [[0*%z ; %z], (%i-%z).^[1 3;2 4]]; string(p)
287 !0 i - z - i + 3z + 3iz - z !
290 !z - 1 - 2iz + z 1 + 4iz - 6z - 4iz + z !
293 --> text = ["André''s got 50% of 1430 $, & the ""remainder"" 1 month later."
294 > "x=A\B is such that A*x=B, with A in {a<b, 1-a, ~a, ^a}. _#"]
296 !André's got 50% of 1430 $, & the "remainder" 1 month later. !
297 !x=A\B is such that A*x=B, with A in {a<b, 1-a, ~a, ^a}. _# !
300 --> ce = makecell([2 3],["a b"; "cdefg"], %t, %z./[%z-1, %z^2+1], ..
301 (1-%s)^3, %pi, int8(rand(2,3)*200))
303 [2x1 string ] [1x1 boolean ] [ r ]
304 [1x1 polynomial] [1x1 constant] [2x3 int8]
308 <emphasis role="bold">Export to LaTeX format</emphasis>:
310 <programlisting role="example"><![CDATA[
311 // Please run the first example section to define objects to be converted,
312 // before executing this section.
313 prettyprint(bool, "latex")
314 prettyprint(i16, "latex")
315 prettyprint(num, "latex")
316 prettyprint(cx, "latex")
317 prettyprint(p, "latex")
318 prettyprint(text, "latex")
319 prettyprint(ce, "latex")
320 prettyprint(linsys, "latex", "")
323 --> prettyprint(bool, "latex")
325 ${\begin{pmatrix}T&T\cr F&T\cr \end{pmatrix}}$
327 --> prettyprint(i16, "latex")
329 ${\begin{pmatrix}26595&1212&4257&-4466&9784\cr -4226&3404&5743&3&10032\cr 3047
330 1&6&14918&267&30\cr \end{pmatrix}}$
332 --> prettyprint(num, "latex")
334 ${\begin{pmatrix}-123.45&{\infty}&5.43\!\times\!10^{-123}\cr {\mathrm{NaN}}&0&
335 {-\infty}\cr \end{pmatrix}}$
337 --> prettyprint(cx, "latex")
339 ${\begin{pmatrix}1+{\mathrm{NaN}}i&-1.250\!\times\!10^{-12}+2i\cr {\infty}+6.1
340 0\!\times\!10^{167}i&1{-\infty}i\cr \end{pmatrix}}$
342 --> prettyprint(p, "latex")
344 ${\begin{pmatrix}0z&i-z &-i+3z +3iz^{2} -z^{3} \cr z &-1-2iz +z^{2} &1+4iz -6z
345 ^{2} -4iz^{3} +z^{4} \cr \end{pmatrix}}$
347 --> prettyprint(text, "latex")
349 ${\begin{pmatrix}\mathsf{\text{André's got 50\% of 1430 \$, \& the "remainder"
350 1 month later.}}\cr \mathsf{\text{x=A\backslash\!B is such that A*x=B, with A
351 in \{a\!<\!b, 1-a, \sim\!a, \^\;\,a\}. _#}}\cr \end{pmatrix}}$
353 --> prettyprint(ce, "latex")
355 ${\begin{pmatrix}{\begin{pmatrix}\mathsf{\text{a b}}\cr \mathsf{\text{cdefg}}\
356 cr \end{pmatrix}}&T&{\begin{pmatrix}{\frac{z }{-1+z }}&{\frac{z }{1+z^{2} }}\c
357 r \end{pmatrix}}\cr 1-3s +3s^{2} -s^{3} &3.1415927&{\begin{pmatrix}51&23&-121\
358 cr 125&122&66\cr \end{pmatrix}}\cr \end{pmatrix}}$
360 --> prettyprint(linsys, "latex", "")
362 ${\begin{matrix}{\left\{\begin{array}{rcl}\dot{X}(t)&=&{\begin{pmatrix}9.35&-5
363 .94&-1.21\cr 6.65&-6.92&2.83\cr 2.49&2.7&0.34\cr \end{pmatrix}} X(t)+{\begin{p
364 matrix}-0.11&-8.3\cr 1.84&7.45\cr -4.93&-1.35\cr \end{pmatrix}}U(t)\cr Y(t) &=
365 & {\begin{pmatrix}5.45&5.61&0.94\cr -5.82&5.41&0.14\cr \end{pmatrix}} X(t) \en
366 d{array}\right.}\cr \end{matrix}}$
370 <emphasis role="bold">Export to LaTeX and rendering in a graphic figure</emphasis>:
372 <programlisting role="example"><![CDATA[
373 // Please run the first example section to define objects to be converted,
374 // before executing this section.
377 gcf().axes_size = [500 670];
378 gca().margins = 0.01*[1 1 1 1];
380 if type(obj)<>10 | part(obj,$)<>":"
381 obj = prettyprint(obj, "latex");
383 r = stringbox(obj, 0, 0);
384 y = y - strange(r(2,:))-0.015;
385 xstring(0.1, y, obj);
387 obj = prettyprint(linsys,"latex","")
388 y = y - strange(stringbox(obj, 0, 0)(2,:))-0.02;
389 xstring(0.1, y, obj);
390 set(gca().children, "fractional_font","on", "font_size",2.5);
392 <scilab:image><![CDATA[
393 bool = [ %t %t; %f %t ]
395 26595 1212 4257 -4466 9784
396 -4226 3404 5743 3 10032
397 30471 6 14918 267 30 ])
398 num = [-123.45, %inf, 5.427e-123
400 cx = [complex(1,%nan), complex(-1.25d-12, 2)
401 complex(%inf,6.1e167), complex(1,-%inf)]
402 p = [[0*%z ; %z], (%i-%z).^[1 3;2 4]]; string(p)
403 text = ["André''s got 50% of 1430 $, & the ""remainder"" 1 month later."
404 "x=A\B is such that A*x=B, with A in {a<b, 1-a, ~a, ^a}. _#"]
405 ce = {["a b"; "cdefg"], %t, %z./[%z-1, %z^2+1] ; (1-%s)^3, %pi, int8(rand(2,3)*200)}
407 A = grand(3,3,"uin",-999,999)/100;
408 B = grand(3,2,"uin",-999,999)/100;
409 C = grand(2,3,"uin",-999,999)/100;
410 linsys = syslin("c", A, B, C);
412 L = list("Booleans:", bool, "int16:", i16, "Decimal numbers:", num, ..
413 "Complex numbers:", cx, "Polynomials:", p, "Text:", text, ..
414 "Cells array, embedding rationals:", ce, "Linear dynamical system:");
417 gcf().axes_size = [500 670];
418 gca().margins = 0.01*[1 1 1 1];
420 if type(obj)<>10 | part(obj,$)<>":"
421 obj = prettyprint(obj, "latex");
423 r = stringbox(obj, 0, 0);
424 y = y - strange(r(2,:))-0.015;
425 xstring(0.1, y, obj);
427 obj = prettyprint(linsys,"latex","")
428 y = y - strange(stringbox(obj, 0, 0)(2,:))-0.02;
429 xstring(0.1, y, obj);
430 set(gca().children, "fractional_font","on", "font_size",2.5);
434 <emphasis role="bold">Export to LaTeX and rendering in a HTML.5 page for any web browser</emphasis>,
437 <programlisting role="example"><![CDATA[
438 // Please run the first example section to define objects to be converted,
439 // before executing this section.
442 mathjaxURL = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"
443 html = ["<!doctype html>"
445 "<meta http-equiv=""Content-Type"" content=""text/html;charset=UTF-8"">"
446 "<script src=''" + mathjaxURL + "'' async></script>"
447 "<!-- then any LaTeX expression delimited with \[ .. \] will be rendered -->"
452 if type(obj)<>10 | part(obj,$)<>":"
453 obj = prettyprint(obj, "latex", "(", %f, %f);
454 html = [html ; "\[ "+ obj + "\]" ; "<br/>"];
456 html = [html ; obj + " <br/>"];
459 html = [html ; "\[ "+ prettyprint(linsys, "latex", "") + "\]<br/><br/>"];
461 html = [html ; "</body>" ; "</html>"];
462 File = TMPDIR + filesep() + "prettyprint_mathjax.html"
465 edit(File) // See the HTML code
466 winopen(File) // Display the page in your browser
470 Please note that for string input, the protection needed for some special LaTeX characters
471 is displayed as is by MathJax:
475 <imagedata fileref="../images/prettyprint_LaTeX_MathJax.png"/>
481 <emphasis role="bold">MathML:</emphasis> Exporting Scilab objects into MathML, and
482 rendering them in a HTML page using the <math> tag.
483 We still use <emphasis>MathJax</emphasis> to render the code in any web browser:
485 <programlisting role="example"><![CDATA[
486 // Please run the first example section to define objects to be converted,
487 // before executing this section.
490 mathjaxURL = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"
491 html = ["<!doctype html>"
494 "<meta http-equiv=""Content-Type"" content=""text/html;charset=UTF-8"">"
495 "<script src=''" + mathjaxURL + "'' async></script>"
496 "<!-- then any MathML expression embedded in <math>..</math> will be rendered -->"
502 if type(obj)<>10 | part(obj,$)<>":"
503 obj = prettyprint(obj, "mathml", "(", %f, %f);
504 html = [html ; "<math>" ; obj ; "</math><br/><br/>"];
506 html = [html ; obj + " <br/>"];
509 html = [html ; "<math>" ; prettyprint(linsys, "mathml", "") ; "</math>"];
511 html = [html ; "</body>" ; "</html>"];
512 File = TMPDIR + filesep() + "prettyprint_mathML.html"
515 editor(File) // See the HTML and MathML code
516 winopen(File) // Render the page in your browser
520 <imagedata fileref="../images/prettyprint_MathML.png"/>
526 <emphasis role="bold">Export in HTML(4) for rendering in messagebox()</emphasis>:
527 Only the "|" or "||" delimiters can be used (or "" for no delimiter").
528 In addition, "_" is used in HTML mode to get any table with borders:
530 <programlisting role="example"><![CDATA[
531 // Please run the first example section to define objects to be converted,
532 // before executing this section.
535 html = ["<!doctype html>"
537 "<meta http-equiv=""Content-Type"" content=""text/html;charset=UTF-8"">"
542 if type(obj)<>10 | part(obj,$)<>":"
543 obj = prettyprint(obj, "html", "|");
544 html = [html ; obj ; "<br/><br/>"];
546 html = [html ; obj + " <br/>"];
549 html = [html ; prettyprint(linsys, "html", "|4")];
551 html = [html ; "</body>" ; "</html>"];
552 File = TMPDIR + filesep() + "prettyprint_HTML.html"
555 edit(File) // See the HTML code
556 winopen(File) // Display the page in your browser
557 messagebox(html, "prettyprint() test => messagebox(HTML.4)");
560 <para>Rendering in a messagebox():</para>
563 <imagedata fileref="../images/prettyprint_HTML4_GUI.png"/>
568 Still in HTML, using "_" to get table borders:
570 <programlisting role="example"><![CDATA[
571 // Please run the first example section to define i16, before executing this section.
573 messagebox(["The table is:" ; "<br>" ; prettyprint(i16,"html","_")])
578 <imagedata fileref="../images/prettyprint_HTML_table_borders.png"/>
583 <refsection role="see also">
584 <title>See also</title>
585 <simplelist type="inline">
587 <ulink url="https://www.mathjax.org">MathJax</ulink>
590 <ulink url="http://www.w3.org/1998/Math/MathML">MathML namespace</ulink>
593 <link linkend="math_rendering_features_in_graphic">math_rendering_features_in_graphic</link>
596 <link linkend="format">format</link>
599 <link linkend="xnumb">xnumb</link>
602 <link linkend="string">string</link>
605 <link linkend="pol2str">pol2str</link>
608 <link linkend="msprintf">msprintf</link>
611 <link linkend="uicontrol">uicontrol(table)</link>
614 <link linkend="sci2exp">sci2exp</link>
618 <refsection role="history">
619 <title>History</title>
622 <revnumber>5.2.0</revnumber>
628 <revnumber>6.1.0</revnumber>
630 Export to HTML added.