2 * Introduction to alt-display::
3 * Functions and Variables for alt-display::
6 @node Introduction to alt-display, Functions and Variables for alt-display, alt-display-pkg, alt-display-pkg
7 @section Introduction to alt-display
9 The @emph{alt-display} package provides a means to change the way that
10 Maxima displays its output. The @var{*alt-display1d*} and
11 @var{*alt-display2d*} Lisp hooks were introduced to Maxima in 2002, but
12 were not easily accessible from the Maxima REPL until the introduction
15 The package provides a general purpose function to define alternative
16 display functions, and a separate function to set the display
17 function. The package also provides customized display functions to
18 produce output in @TeX{}, Texinfo, XML and all three output formats
21 Here is a sample session:
23 @c (linenum:1,display2d:true,reset_displays());
25 (%i1) load("alt-display.mac")$
26 (%i2) set_alt_display(2,tex_display)$
28 (%i3) x/(x^2+y^2) = 1;
29 \mbox@{\tt\red(@{\it \%o_3@}) \black@}$$@{@{x@}\over@{y^2+x^2@}@}=1$$
31 (%i4) set_alt_display(2,mathml_display)$
33 (%i5) x/(x^2+y^2) = 1;
34 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>mlabel</mi>
35 <mfenced separators=""><msub><mi>%o</mi> <mn>5</mn></msub>
36 <mo>,</mo><mfrac><mrow><mi>x</mi> </mrow> <mrow><msup><mrow>
37 <mi>y</mi> </mrow> <mn>2</mn> </msup> <mo>+</mo> <msup><mrow>
38 <mi>x</mi> </mrow> <mn>2</mn> </msup> </mrow></mfrac> <mo>=</mo>
39 <mn>1</mn> </mfenced> </math>
41 (%i6) set_alt_display(2,multi_display_for_texinfo)$
43 (%i7) x/(x^2+y^2) = 1;
47 \mbox@{\tt\red(@{\it \%o_7@}) \black@}$$@{@{x@}\over@{y^2+x^2@}@}=1$$
53 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>mlabel</mi>
54 <mfenced separators=""><msub><mi>%o</mi> <mn>7</mn></msub>
55 <mo>,</mo><mfrac><mrow><mi>x</mi> </mrow> <mrow><msup><mrow>
56 <mi>y</mi> </mrow> <mn>2</mn> </msup> <mo>+</mo> <msup><mrow>
57 <mi>x</mi> </mrow> <mn>2</mn> </msup> </mrow></mfrac> <mo>=</mo>
58 <mn>1</mn> </mfenced> </math>
68 If the alternative display function causes an error, the error is
69 trapped and the display function is reset to the default display. In the
70 following example, the @code{error} function is set to display the
71 output. This throws an error, which is handled by resetting the
72 2d-display to the default.
75 (%i8) set_alt_display(2,?error)$
79 Error in *alt-display2d*.
80 Messge: Condition designator ((MLABEL) $%O9 $X) is not of type
81 (OR SYMBOL STRING FUNCTION).
82 *alt-display2d* reset to nil.
83 -- an error. To debug this try: debugmode(true);
89 @node Functions and Variables for alt-display, , Introduction to alt-display, alt-display-pkg
90 @section Functions and Variables for alt-display
92 @anchor{alt_display_output_type}
93 @deffn {Function} alt_display_output_type (@var{form})
95 Determine the type of output to be printed. @var{Form} must be a lisp
96 form suitable for printing via Maxima's built-in @code{displa}
97 function. At present, this function returns one of three values:
98 @var{text}, @var{label} or @var{unknown}.
100 An example where @code{alt_display_output_type} is used. In
101 @code{my_display}, a text form is printed between a pair of tags
102 @verb{|TEXT;>>|} and @verb{|<<TEXT;|} while a label form is printed between
103 a pair tags @verb{|OUT;>>|} and @verb{|<<OUT;|} in addition to the usual
106 The function @mref{set_prompt} also ensures that input labels are
107 printed between matching @verb{|PROMPT;>>|} and @verb{|<<PROMPT;|} tags.
110 @url{https://sourceforge.net/p/maxima/mailman/maxima-discuss/thread/7792c096-7e07-842d-0c3a-b2f039ef1f15%40gmail.com/#msg37235035,Eric
114 (%i1) (load("mactex-utilities"), load("alt-display.mac")) $
116 (%i2) define_alt_display(my_display(form),
117 block([type,txttmplt,labtmplt],
118 txttmplt:"~%TEXT;>>~%~a~%<<TEXT;~%",
119 labtmplt:"~%OUT;>>~%(~a) ~a~a~a~%<<OUT;~%",
120 type:alt_display_output_type(form),
122 printf(true,txttmplt,first(form))
123 else if type='label then
124 printf(true,labtmplt,first(form),"$$",tex1(second(form)),"$$")
126 block([alt_display1d:false, alt_display2d:false], displa(form)))) $
128 (%i3) (set_prompt('prefix, "PROMPT;>>",'suffix, "<<PROMPT;"),
129 set_alt_display(1,my_display)) $
131 PROMPT;>>(%i4) <<PROMPT;integrate(x^n,x);
140 (%o4) $$\frac{x^{n+1}}{n+1}$$
142 PROMPT;>>(%i5) <<PROMPT;
145 @opencatbox{Categories:}
146 @category{Package alt-display}
152 @anchor{define_alt_display}
153 @deffn {Function} define_alt_display (@var{function}(@var{input}), @var{expr})
154 This function is similar to @code{define}: it evaluates its arguments
155 and expands into a function definition. The @var{function} is a
156 function of a single input @var{input}. For convenience, a substitution
157 is applied to @var{expr} after evaluation, to provide easy access to
160 Set a time-stamp on each prompt:
162 (%i1) load("alt-display.mac")$
164 (%i2) display2d: false$
166 (%i3) define_alt_display(time_stamp(x),
167 block([alt_display1d:false,alt_display2d:false],
168 prompt_prefix:printf(false,"~a~%",timedate()),
171 (%o3) time_stamp(x):=block(
172 [\*alt\-display1d\*:false,
173 \*alt\-display2d\*:false],
175 :printf(false,"~a~%",timedate()),displa(x))
176 (%i4) set_alt_display(1,time_stamp);
179 2017-11-27 16:15:58-06:00
183 The input line @code{%i3} defines @code{time_stamp} using
184 @code{define_alt_display}. The output line @code{%o3} shows that the
185 Maxima variable names @code{alt_display1d}, @code{alt_display2d} and
186 @code{prompt_prefix} have been replaced by their Lisp translations, as
187 has @code{displa} been replaced by @code{?displa} (the display
190 The display variables @code{alt_display1d} and @code{alt_display2d} are
191 both bound to @code{false} in the body of @code{time_stamp} to prevent
192 an infinite recursion in @code{displa}.
194 @opencatbox{Categories:}
195 @category{Package alt-display}
200 @anchor{info_display}
201 @deffn {Function} info_display (@var{form})
202 This is an alias for the default 1-d display function. It may be used as
203 an alternative 1-d or 2-d display function.
206 (%i1) load("alt-display.mac")$
208 (%i2) set_alt_display(2,info_display);
216 @opencatbox{Categories:}
217 @category{Package alt-display}
222 @anchor{mathml_display}
223 @deffn {Function} mathml_display (@var{form})
224 Produces MathML output.
227 (%i1) load("alt-display.mac")$
229 (%i2) set_alt_display(2,mathml_display);
230 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>mlabel</mi>
231 <mfenced separators=""><msub><mi>%o</mi> <mn>2</mn></msub>
232 <mo>,</mo><mi>done</mi> </mfenced> </math>
235 @opencatbox{Categories:}
236 @category{Package alt-display}
242 @deffn {Function} tex_display (@var{form})
246 (%i2) set_alt_display(2,tex_display);
247 \mbox@{\tt\red(@{\it \%o_2@}) \black@}$$\mathbf@{done@}$$
249 \mbox@{\tt\red(@{\it \%o_3@}) \black@}$$@{@{x@}\over@{y^2+x^2@}@}$$
252 @opencatbox{Categories:}
253 @category{Package alt-display}
258 @anchor{multi_display_for_texinfo}
259 @deffn {Function} multi_display_for_texinfo (@var{form})
260 Produces Texinfo output using all three display functions.
263 (%i2) set_alt_display(2,multi_display_for_texinfo)$
269 \mbox@{\tt\red(@{\it \%o_3@}) \black@}$$@{@{x@}\over@{y^2+x^2@}@}$$
275 <math xmlns="http://www.w3.org/1998/Math/MathML"> <mi>mlabel</mi>
276 <mfenced separators=""><msub><mi>%o</mi> <mn>3</mn></msub>
277 <mo>,</mo><mfrac><mrow><mi>x</mi> </mrow> <mrow><msup><mrow>
278 <mi>y</mi> </mrow> <mn>2</mn> </msup> <mo>+</mo> <msup><mrow>
279 <mi>x</mi> </mrow> <mn>2</mn> </msup> </mrow></mfrac> </mfenced> </math>
289 @opencatbox{Categories:}
290 @category{Package alt-display}
295 @anchor{reset_displays}
296 @deffn {Functions} reset_displays ()
297 Resets the prompt prefix and suffix to the empty string, and sets both
298 1-d and 2-d display functions to the default.
300 @opencatbox{Categories:}
301 @category{Package alt-display}
306 @anchor{set_alt_display}
307 @deffn {Function} set_alt_display (@var{num}, @var{display-function})
308 The input @var{num} is the display to set; it may be either 1 or 2. The
309 second input @var{display-function} is the display function to use. The
310 display function may be either a Maxima function or a @code{lambda}
313 Here is an example where the display function is a @code{lambda}
314 expression; it just displays the result as @TeX{}.
316 (%i1) load("alt-display.mac")$
318 (%i2) set_alt_display(2, lambda([form], tex(?caddr(form))))$
320 (%i3) integrate(exp(-t^2),t,0,inf);
321 $$@{@{\sqrt@{\pi@}@}\over@{2@}@}$$
324 A user-defined display function should take care that it @emph{prints}
325 its output. A display function that returns a string will appear to
326 display nothing, nor cause any errors.
328 @opencatbox{Categories:}
329 @category{Package alt-display}
335 @deffn {Function} set_prompt (@var{fix}, @var{expr})
336 Set the prompt prefix or suffix to @var{expr}. The input @var{fix} must
337 evaluate to one of @code{prefix}, @code{suffix}, @code{general},
338 @code{prolog} or @code{epilog}. The input @var{expr} must evaluate to
339 either a string or @code{false}; if @code{false}, the @var{fix} is reset
340 to the default value.
343 (%i1) load("alt-display.mac")$
344 (%i2) set_prompt('prefix,printf(false,"It is now: ~a~%",timedate()))$
346 It is now: 2014-01-07 15:23:23-05:00
350 The following example shows the effect of each option, except
351 @code{prolog}. Note that the @code{epilog} prompt is printed as Maxima
352 closes down. The @code{general} is printed between the end of input and
353 the output, unless the input line ends in @code{$}.
355 Here is an example to show where the prompt strings are placed.
358 (%i1) load("alt-display.mac")$
360 (%i2) set_prompt(prefix, "<<prefix>> ", suffix, "<<suffix>> ",
361 general, printf(false,"<<general>>~%"),
362 epilog, printf(false,"<<epilog>>~%"));
365 <<prefix>> (%i3) <<suffix>> x/y;
370 <<prefix>> (%i4) <<suffix>> quit();
375 Here is an example that shows how to colorize the input and output when
376 Maxima is running in a terminal or terminal emulator like
377 Emacs@footnote{Readers using the @code{info} reader in @code{Emacs} will
378 see the actual prompt strings; other readers will see the colorized
382 @c (%i2) set_prompt('prefix,"
\e[1;31m",'suffix,"
\e[0;32m",'general,"
\e[1;34m",'epilog,"
\e[00;m")$
384 (%i1) load("alt-display.mac")$
386 (%i2) cs(s) := printf(false,"~a[~am",ascii(27),s)$
388 (%i3) set_prompt(prefix,cs("1;31"),suffix,cs("0;32"),general,cs("1;34"),epilog,cs("00;"));
390 \e[1;31m(%i4)
\e[0;32m integrate(exp(-x^2),x,0,inf);
\e[1;34m
398 @figure{color_terminal}
401 Each prompt string starts with the ASCII escape character (27) followed
402 by an open square bracket (91); each string ends with a lower-case m
404 @url{https://misc.flogisoft.com/bash/tip_colors_and_formatting} and
405 @url{https://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x329.html} provide
406 information on how to use control strings to set the terminal colors.
408 @opencatbox{Categories:}
409 @category{Package alt-display}
416 @c TeX-master: "include-maxima"