4 * Functions and Variables for File Input and Output::
5 * Functions and Variables for TeX Output::
6 * Functions and Variables for Fortran Output::
9 @c -----------------------------------------------------------------------------
10 @node Comments, Files, File Input and Output, File Input and Output
12 @c -----------------------------------------------------------------------------
14 A comment in Maxima input is any text between @code{/*} and @code{*/}.
16 The Maxima parser treats a comment as whitespace for the purpose of finding
17 tokens in the input stream; a token always ends at a comment. An input such as
18 @code{a/* foo */b} contains two tokens, @code{a} and @code{b},
19 and not a single token @code{ab}. Comments are otherwise ignored by Maxima;
20 neither the content nor the location of comments is stored in parsed input
23 Comments can be nested to arbitrary depth. The @code{/*} and @code{*/}
24 delimiters form matching pairs. There must be the same number of @code{/*}
25 as there are @code{*/}.
30 @c /* aa is a variable of interest */ aa : 1234;
31 @c /* Value of bb depends on aa */ bb : aa^2;
32 @c /* User-defined infix operator */ infix ("b");
33 @c /* Parses same as a b c, not abc */ a/* foo */b/* bar */c;
34 @c /* Comments /* can be nested /* to any depth */ */ */ 1 + xyz;
37 (%i1) /* aa is a variable of interest */ aa : 1234;
39 (%i2) /* Value of bb depends on aa */ bb : aa^2;
41 (%i3) /* User-defined infix operator */ infix ("b");
43 (%i4) /* Parses same as a b c, not abc */ a/* foo */b/* bar */c;
45 (%i5) /* Comments /* can be nested /* to any depth */ */ */ 1 + xyz;
49 @opencatbox{Categories:}
53 @c -----------------------------------------------------------------------------
54 @node Files, Functions and Variables for File Input and Output, Comments, File Input and Output
56 @c -----------------------------------------------------------------------------
58 A file is simply an area on a particular storage device which contains data
59 or text. Files on the disks are figuratively grouped into "directories".
60 A directory is just a list of files. Commands which deal with files are:
63 appendfile batch batchload
64 closefile file_output_append filename_merge
65 file_search file_search_maxima file_search_lisp
66 file_search_demo file_search_usage file_search_tests
67 file_type file_type_lisp file_type_maxima
68 load load_pathname loadfile
69 loadprint pathname_directory pathname_name
70 pathname_type printfile save
71 stringout with_stdout writefile
74 When a file name is passed to functions like @mrefcomma{plot2d}@w{}
75 @mrefcomma{save} or @mref{writefile} and the file name does not include a path,
76 Maxima stores the file in the current working directory. The current working
77 directory depends on the system like Windows or Linux and on the installation.
79 @c -----------------------------------------------------------------------------
80 @node Functions and Variables for File Input and Output, Functions and Variables for TeX Output, Files, File Input and Output
81 @section Functions and Variables for File Input and Output
82 @c -----------------------------------------------------------------------------
84 @c -----------------------------------------------------------------------------
86 @deffn {Function} appendfile (@var{filename})
88 Appends a console transcript to @var{filename}. @code{appendfile} is the same
89 as @mrefcomma{writefile} except that the transcript file, if it exists, is
92 @mref{closefile} closes the transcript file opened by @code{appendfile} or
95 @opencatbox{Categories:}
96 @category{File output}
97 @category{Console interaction}
101 @c -----------------------------------------------------------------------------
103 @deffn {Function} batch @
104 @fname{batch} (@var{filename}) @
105 @fname{batch} (@var{filename}, @var{option}) @
106 @fname{batch} (@var{S}) @
107 @fname{batch} (@var{S}, @var{option})
109 @code{batch(@var{filename})} reads Maxima expressions from @var{filename} and
110 evaluates them. @code{batch} searches for @var{filename} in the list
111 @mrefdot{file_search_maxima} See also @mrefdot{file_search}
113 @code{batch(@var{S})} reads Maxima expressions from the input stream @var{S}
114 as created by @mref{openr}.
115 The behavior of @code{batch} in this case is the same as if the input
116 were a file name, and in the remainder of this description,
117 what is said about input files applies to input streams as well,
118 except that the comments about searching for files do not apply to streams.
120 @code{batch(@var{filename}, @code{demo})} is like @code{demo(@var{filename})}.
121 In this case @code{batch} searches for @var{filename} in the list
122 @mrefdot{file_search_demo} See @mrefdot{demo}
124 @code{batch(@var{filename}, @code{test})} is like @mref{run_testsuite} with the
125 option @code{display_all=true}. For this case @code{batch} searches
126 @var{filename} in the list @code{file_search_maxima} and not in the list
127 @mref{file_search_tests} like @code{run_testsuite}. Furthermore,
128 @code{run_testsuite} runs tests which are in the list
129 @mrefdot{testsuite_files} With @code{batch} it is possible to run any file in
130 a test mode, which can be found in the list @code{file_search_maxima}. This is
131 useful, when writing a test file.
133 @var{filename} comprises a sequence of Maxima expressions, each terminated with
134 @code{;} or @code{$}. The special variable @mref{%} and the function
135 @mref{%th} refer to previous results within the file. The file may include
136 @code{:lisp} constructs. Spaces, tabs, and newlines in the file are ignored.
137 A suitable input file may be created by a text editor or by the
138 @mref{stringout} function.
140 @code{batch} reads each input expression from @var{filename}, displays
141 the input to the console, computes the corresponding output expression,
142 and displays the output expression. Input labels are assigned to the
143 input expressions and output labels are assigned to the output
144 expressions. @code{batch} evaluates every input expression in the file
145 unless there is an error. If user input is requested (by @mref{asksign}
146 or @mrefcomma{askinteger} for example) @code{batch} pauses to collect
147 the requisite input and then continue; if @code{batch_answers_from_file}
148 is @code{true}, the input is read from the file itself
149 @mrefdot{batch_answers_from_file}
151 @c CTRL-C BREAKS batch IN CMUCL, BUT CLISP (ALTHOUGH IT SHOWS "User break")
152 @c KEEPS GOING !!! DON'T KNOW ABOUT GCL !!!
153 It may be possible to halt @code{batch} by typing @code{control-C} at the
154 console. The effect of @code{control-C} depends on the underlying Lisp
157 @code{batch} has several uses, such as to provide a reservoir for working
158 command lines, to give error-free demonstrations, or to help organize one's
159 thinking in solving complex problems.
161 @code{batch} evaluates its arguments.
163 When called with no second argument or with the option @code{demo},
164 @code{batch} returns the path of @var{filename},
165 if the argument is a file name,
166 or the path of the file for which the input stream was opened,
167 if the argument is a file input stream.
168 If the argument is a string input stream,
169 a representation of the input stream is returned.
171 When called with the option @code{test}, the return value
172 is a an empty list @code{[]} or a list with @var{filename} and the numbers of
173 the tests which have failed.
175 See also @mrefcomma{load} @mrefcomma{batchload}
176 @mrefcomma{batch_answers_from_file} and @mrefdot{demo}
178 @opencatbox{Categories:}
179 @category{Session management}
180 @category{File input}
184 @c RECOMMEND CUTTING THIS ITEM, AS THE load SUBSUMES FUNCTIONALITY OF batchload
186 @c -----------------------------------------------------------------------------
188 @deffn {Function} batchload @
189 @fname{batchload} (@var{filename}) @
190 @fname{batchload} (@var{S}) @
192 Reads Maxima expressions from input file @var{filename} or input stream @var{S}
194 without displaying the input or output expressions and without assigning labels to
195 output expressions. Printed output (such as produced by @mref{print} or
196 @mrefparen{describe}) is displayed, however.
198 The special variable @mref{%} and the function @mref{%th} refer to previous
199 results from the interactive interpreter, not results within the file.
200 The file cannot include @code{:lisp} constructs.
202 @code{batchload} evaluates its argument.
204 @code{batchload} returns the path of @var{filename},
205 if the argument is a file name,
206 or the path of the file for which the input stream was opened,
207 if the argument is a file input stream.
208 If the argument is a string input stream,
209 a representation of the input stream is returned.
211 See also @mrefcomma{batch} and @mrefdot{load}
212 @c batchload APPEARS TO HAVE THE SAME EFFECT AS load.
213 @c WHY NOT GET RID OF batchload ???
215 @opencatbox{Categories:}
216 @category{Session management}
217 @category{File input}
221 @c -----------------------------------------------------------------------------
223 @deffn {Function} closefile ()
225 Closes the transcript file opened by @mref{writefile} or @mrefdot{appendfile}
227 @opencatbox{Categories:}
228 @category{File output}
229 @category{Console interaction}
233 @c -----------------------------------------------------------------------------
234 @anchor{batch_answers_from_file}
235 @defvr {Option variable} batch_answers_from_file
236 Default value: @code{false}
238 If @code{true}, then @code{batch} reads answers to interactive questions
239 from its input file or stream.
241 Example: Maxima's interactive testsuite includes something like
245 [asksign (foo), sign (foo), sign (foo)];
250 The first line makes Maxima ask a question; when
251 @code{batch_answers_from_file} is @code{true}, the second line is read
252 as the answer to the question; and the third line provides the expected
255 The command-line option @code{--batch-string} binds
256 @code{batch_answers_from_file} to @code{true}. The @code{run_testsuite}
257 function, as a default, also binds @code{batch_answers_from_file} to
258 @code{true}. @mref{command_line_options} and @mrefdot{run_testsuite}
260 @opencatbox{Categories:}
261 @category{File input}
262 @category{Global flags}
266 @c -----------------------------------------------------------------------------
267 @anchor{file_output_append}
268 @defvr {Option variable} file_output_append
269 Default value: @code{false}
271 @code{file_output_append} governs whether file output functions append or
272 truncate their output file. When @code{file_output_append} is @code{true}, such
273 functions append to their output file. Otherwise, the output file is truncated.
275 @mrefcomma{save} @mrefcomma{stringout} and @mref{with_stdout} respect
276 @code{file_output_append}. Other functions which write output files do not
277 respect @code{file_output_append}. In particular, plotting and translation
278 functions always truncate their output file, and @mref{tex} and
279 @mref{appendfile} always append.
280 @c WHAT ABOUT WRITEFILE ??
282 @opencatbox{Categories:}
283 @category{File output}
284 @category{Global flags}
290 @c -----------------------------------------------------------------------------
291 @anchor{filename_merge}
292 @deffn {Function} filename_merge (@var{path}, @var{filename})
294 Constructs a modified path from @var{path} and @var{filename}. If the final
295 component of @var{path} is of the form @code{###.@var{something}}, the component
296 is replaced with @code{@var{filename}.@var{something}}. Otherwise, the final
297 component is simply replaced by @var{filename}.
299 The result is a Lisp pathname object.
301 @opencatbox{Categories:}
302 @category{File input}
303 @category{File output}
307 @c -----------------------------------------------------------------------------
309 @deffn {Function} file_search @
310 @fname{file_search} (@var{filename}) @
311 @fname{file_search} (@var{filename}, @var{pathlist})
313 @code{file_search} searches for the file @var{filename} and returns the path to
314 the file (as a string) if it can be found; otherwise @code{file_search} returns
315 @code{false}. @code{file_search (@var{filename})} searches in the default
316 search directories, which are specified by the
317 @mrefcomma{file_search_maxima} @mrefcomma{file_search_lisp} and
318 @mref{file_search_demo} variables.
320 @code{file_search} first checks if the actual name passed exists,
321 before attempting to match it to ``wildcard'' file search patterns.
322 See @mref{file_search_maxima} concerning file search patterns.
324 The argument @var{filename} can be a path and file name, or just a file name,
325 or, if a file search directory includes a file search pattern, just the base of
326 the file name (without an extension). For example,
329 file_search ("/home/wfs/special/zeta.mac");
330 file_search ("zeta.mac");
331 file_search ("zeta");
334 all find the same file, assuming the file exists and
335 @code{/home/wfs/special/###.mac} is in @code{file_search_maxima}.
337 @code{file_search (@var{filename}, @var{pathlist})} searches only in the
338 directories specified by @var{pathlist}, which is a list of strings. The
339 argument @var{pathlist} supersedes the default search directories, so if the
340 path list is given, @code{file_search} searches only the ones specified, and not
341 any of the default search directories. Even if there is only one directory in
342 @var{pathlist}, it must still be given as an one-element list.
344 The user may modify the default search directories.
345 See @mrefdot{file_search_maxima}
347 @code{file_search} is invoked by @mref{load} with @code{file_search_maxima} and
348 @mref{file_search_lisp} as the search directories.
350 @opencatbox{Categories:}
351 @category{File input}
355 @c -----------------------------------------------------------------------------
356 @anchor{file_search_maxima}
357 @anchor{file_search_lisp}
358 @anchor{file_search_demo}
359 @anchor{file_search_usage}
360 @anchor{file_search_tests}
361 @defvr {Option variable} file_search_maxima
362 @defvrx {Option variable} file_search_lisp
363 @defvrx {Option variable} file_search_demo
364 @defvrx {Option variable} file_search_usage
365 @defvrx {Option variable} file_search_tests
367 These variables specify lists of directories to be searched by
368 @mrefcomma{load} @mrefcomma{demo} and some other Maxima functions. The default
369 values of these variables name various directories in the Maxima installation.
371 The user can modify these variables, either to replace the default values or to
372 append additional directories. For example,
375 file_search_maxima: ["/usr/local/foo/*.mac",
376 "/usr/local/bar/*.mac"]$
379 replaces the default value of @code{file_search_maxima}, while
382 file_search_maxima: append (file_search_maxima,
383 ["/usr/local/foo/*.mac", "/usr/local/bar/*.mac"])$
386 appends two additional directories. It may be convenient to put such an
387 expression in the file @code{maxima-init.mac} so that the file search path is
388 assigned automatically when Maxima starts.
389 See also @ref{Introduction for Runtime Environment}.
391 Each element of the search list is a Common Lisp wildcard pathname.
392 Briefly, a wildcard filename looks like @code{"*.lisp"}, which matches
393 any filename with an extension of @code{"lisp"}. A directory
394 component of @code{*} matches any directory in the current directory,
395 and @code{**} matches any directory and subdirectories in the current
398 So, @code{file_search_maxima} includes
399 @code{"/home/username/.maxima/**/*.mac"}. This means look in all
400 subdirectories of @code{"/home/username/.maxima/"} for files with
401 extension @code{"mac"}. This includes subdirectories of
402 subdirectories. Thus, @code{load("file")} will find
403 @code{"/home/username/.maxima/dir1/subdir1/file.mac"}.
405 To only search for a single level of subdirectories, use
406 @code{"/home/username/.maxima/*/*.mac"}. This means Maxima will not
407 find the file @code{"/home/username/.maxima/dir1/subdir1/file.mac"}
408 when Maxima tries to, say, @code{load("file")}.
410 Further information about Common Lisp pathnames maybe be found in
411 @url{http://www.lispworks.com/documentation/HyperSpec/Body/19_b.htm,
412 CLHS Section 19.2: Pathnames}.
414 @opencatbox{Categories:}
415 @category{File input}
416 @category{Global variables}
420 @c -----------------------------------------------------------------------------
422 @deffn {Function} file_type (@var{filename})
424 Returns a guess about the content of @var{filename}, based on the filename
425 extension. @var{filename} need not refer to an actual file; no attempt is made
426 to open the file and inspect the content.
428 The return value is a symbol, either @code{object}, @code{lisp}, or
429 @code{maxima}. If the extension is matches one of the values in
430 @code{file_type_maxima}, @code{file_type} returns @code{maxima}. If the
431 extension matches one of the values in @code{file_type_lisp}, @code{file_type}
432 returns @code{lisp}. If none of the above, @code{file_type} returns
435 See also @mrefdot{pathname_type}
437 See @mref{file_type_maxima} and @mref{file_type_lisp} for the default values.
443 @c ["test.lisp", "test.mac", "test.dem", "test.txt"]);
446 (%i2) map('file_type,
447 ["test.lisp", "test.mac", "test.dem", "test.txt"]);
448 (%o2) [lisp, maxima, maxima, object]
451 @opencatbox{Categories:}
452 @category{File input}
456 @c -----------------------------------------------------------------------------
457 @anchor{file_type_lisp}
458 @defvr {Option variable} file_type_lisp
459 Default value: @code{[l, lsp, lisp]}
461 @code{file_type_lisp} is a list of file extensions that maxima recognizes
462 as denoting a Lisp source file.
464 See also @mrefdot{file_type}
467 @c -----------------------------------------------------------------------------
468 @anchor{file_type_maxima}
469 @defvr {Option variable} file_type_maxima
470 Default value: @code{[mac, mc, demo, dem, dm1, dm2, dm3, dmt, wxm]}
472 @code{file_type_maxima} is a list of file extensions that maxima recognizes
473 as denoting a Maxima source file.
475 See also @mrefdot{file_type}
478 @c -----------------------------------------------------------------------------
480 @deffn {Function} load (@var{filename})
482 Evaluates expressions in @var{filename}, thus bringing variables, functions, and
483 other objects into Maxima. The binding of any existing object is clobbered by
484 the binding recovered from @var{filename}.
486 @var{filename} must be a string, symbol,
487 or Lisp pathname (as created by @mref{filename_merge}).
488 To find the file, @code{load} calls
489 @mref{file_search} with @mref{file_search_maxima} and
490 @mref{file_search_lisp} as the search directories. If @code{load} succeeds, it
491 returns the name of the file. Otherwise @code{load} prints an error message.
493 @code{load} works equally well for Lisp code and Maxima code. Files created by
494 @mrefcomma{save} @mrefcomma{translate_file} and @mrefcomma{compile_file} which
495 create Lisp code, and @mrefcomma{stringout} which creates Maxima code, can all
496 be processed by @code{load}. @code{load} calls @mref{loadfile} to load Lisp
497 files and @mref{batchload} to load Maxima files.
499 @code{load} does not recognize @code{:lisp} constructs in Maxima files, and
500 while processing @var{filename}, the global variables @code{_}, @code{__},
501 @code{%}, and @code{%th} have whatever bindings they had when @code{load} was
504 Note also that structures will only be read back as structures if
505 they have been defined by @code{defstruct} before the @code{load} command
508 See also @mrefcomma{loadfile} for Lisp files; and @mrefcomma{batch} @mrefcomma{batchload} and
509 @mrefdot{demo} for Maxima files.
511 See @mref{file_search} for more detail about the file search mechanism.
512 The @mref{numericalio} chapter describes many functions
513 for loading csv and other data files.
515 During Maxima file loading, the variable @mref{load_pathname} is bound to the pathname of the file
518 @code{load} evaluates its argument.
520 @opencatbox{Categories:}
521 @category{Session management}
522 @category{File input}
526 @c -----------------------------------------------------------------------------
527 @anchor{load_pathname}
528 @defvr {System variable} load_pathname
529 Default value: @code{false}
531 When a file is loaded with the functions @mrefcomma{load} @mref{loadfile} or
532 @mref{batchload} the system variable @code{load_pathname} is bound to the
533 pathname of the file which is processed.
535 The variable @code{load_pathname} can be accessed from the file during the
540 Suppose we have a batchfile @code{test.mac} in the directory
542 @code{"/home/dieter/workspace/mymaxima/temp/"} with the following commands
546 print("The value of load_pathname is: ", load_pathname)$
547 print("End of batchfile")$
550 then we get the following output
553 (%i1) load("/home/dieter/workspace/mymaxima/temp/test.mac")$
554 The value of load_pathname is:
555 /home/dieter/workspace/mymaxima/temp/test.mac
559 @opencatbox{Categories:}
560 @category{File input}
564 @c RECOMMEND CUTTING THIS ITEM, AS THE load SUBSUMES FUNCTIONALITY OF loadfile
566 @c -----------------------------------------------------------------------------
568 @deffn {Function} loadfile (@var{filename})
570 Evaluates Lisp expressions in @var{filename}. @code{loadfile} does not invoke
571 @mrefcomma{file_search} so @code{filename} must include the file extension and
572 as much of the path as needed to find the file.
574 @code{loadfile} can process files created by @mrefcomma{save}@w{}
575 @mrefcomma{translate_file} and @mrefdot{compile_file} The user may find it
576 more convenient to use @mref{load} instead of @code{loadfile}.
578 @opencatbox{Categories:}
579 @category{Session management}
580 @category{File input}
584 @c loadprint DOESN'T EXACTLY WORK LIKE THIS, BUT IT HARDLY SEEMS WORTH FIXING
585 @c I GUESS THIS COULD BE UPDATED TO DESCRIBE THE WAY IT ACTUALLY WORKS
587 @c -----------------------------------------------------------------------------
589 @defvr {Option variable} loadprint
590 Default value: @code{true}
592 @code{loadprint} tells whether to print a message when a file is loaded.
596 When @code{loadprint} is @code{true}, always print a message.
598 When @code{loadprint} is @code{'loadfile}, print a message only if
599 a file is loaded by the function @code{loadfile}.
601 When @code{loadprint} is @code{'autoload},
602 print a message only if a file is automatically loaded.
603 See @mrefdot{setup_autoload}
605 When @code{loadprint} is @code{false}, never print a message.
608 @opencatbox{Categories:}
609 @category{File input}
610 @category{Global flags}
614 @c -----------------------------------------------------------------------------
616 @deffn {Function} directory (@var{path})
618 Returns a list of the files and directories found in @var{path}
621 @var{path} may contain wildcard characters (i.e., characters which represent
622 unspecified parts of the path),
623 which include at least the asterisk on most systems,
624 and possibly other characters, depending on the system.
626 @code{directory} relies on the Lisp function DIRECTORY,
627 which may have implementation-specific behavior.
629 @opencatbox{Categories:}
630 @category{File input}
634 @c -----------------------------------------------------------------------------
635 @anchor{pathname_directory}
636 @anchor{pathname_name}
637 @anchor{pathname_type}
638 @deffn {Function} pathname_directory (@var{pathname})
639 @deffnx {Function} pathname_name (@var{pathname})
640 @deffnx {Function} pathname_type (@var{pathname})
642 These functions return the components of @var{pathname}.
647 @c pathname_directory("/home/dieter/maxima/changelog.txt");
648 @c pathname_name("/home/dieter/maxima/changelog.txt");
649 @c pathname_type("/home/dieter/maxima/changelog.txt");
652 (%i1) pathname_directory("/home/dieter/maxima/changelog.txt");
653 (%o1) /home/dieter/maxima/
654 (%i2) pathname_name("/home/dieter/maxima/changelog.txt");
656 (%i3) pathname_type("/home/dieter/maxima/changelog.txt");
660 @opencatbox{Categories:}
661 @category{File input}
665 @c -----------------------------------------------------------------------------
667 @deffn {Function} printfile (@var{path})
669 Prints the file named by @var{path} to the console. @var{path} may be a string
670 or a symbol; if it is a symbol, it is converted to a string.
672 If @var{path} names a file which is accessible from the current working
673 directory, that file is printed to the console. Otherwise, @code{printfile}
674 attempts to locate the file by appending @var{path} to each of the elements of
675 @mref{file_search_usage} via @mrefdot{filename_merge}
677 @code{printfile} returns @var{path} if it names an existing file,
678 or otherwise the result of a successful filename merge.
680 @opencatbox{Categories:}
681 @category{File input}
682 @category{Console interaction}
688 @c -----------------------------------------------------------------------------
690 @deffn {Function} save @
691 @fname{save} (@var{filename}, @var{name_1}, @var{name_2}, @var{name_3}, @dots{}) @
692 @fname{save} (@var{filename}, values, functions, labels, @dots{}) @
693 @fname{save} (@var{filename}, [@var{m}, @var{n}]) @
694 @fname{save} (@var{filename}, @var{name_1}=@var{expr_1}, @dots{}) @
695 @fname{save} (@var{filename}, all) @
696 @fname{save} (@var{filename}, @var{name_1}=@var{expr_1}, @var{name_2}=@var{expr_2}, @dots{})
698 Stores the current values of @var{name_1}, @var{name_2}, @var{name_3}, @dots{},
699 in @var{filename}. The arguments are the names of variables, functions, or
700 other objects. If a name has no value or function associated with it, it is
701 ignored. @code{save} returns @var{filename}.
703 @code{save} stores data in the form of Lisp expressions.
704 If @var{filename} ends in @code{.lisp} the
705 data stored by @code{save} may be recovered by @code{load (@var{filename})}.
708 The global flag @mref{file_output_append} governs whether @code{save} appends or
709 truncates the output file. When @code{file_output_append} is @code{true},
710 @code{save} appends to the output file. Otherwise, @code{save} truncates the
711 output file. In either case, @code{save} creates the file if it does not yet
714 The special form @code{save (@var{filename}, values, functions, labels, ...)}
715 stores the items named by @mrefcomma{values} @mrefcomma{functions}@w{}
716 @mrefcomma{labels} etc. The names may be any specified by the variable
717 @mrefdot{infolists} @code{values} comprises all user-defined variables.
719 The special form @code{save (@var{filename}, [@var{m}, @var{n}])} stores the
720 values of input and output labels @var{m} through @var{n}. Note that @var{m}
721 and @var{n} must be literal integers. Input and output labels may also be
722 stored one by one, e.g., @code{save ("foo.1", %i42, %o42)}.
723 @code{save (@var{filename}, labels)} stores all input and output labels.
724 When the stored labels are recovered, they clobber existing labels.
726 The special form @code{save (@var{filename}, @var{name_1}=@var{expr_1},
727 @var{name_2}=@var{expr_2}, ...)} stores the values of @var{expr_1},
728 @var{expr_2}, @dots{}, with names @var{name_1}, @var{name_2}, @dots{}
729 It is useful to apply this form to input and output labels, e.g.,
730 @code{save ("foo.1", aa=%o88)}. The right-hand side of the equality in this
731 form may be any expression, which is evaluated. This form does not introduce
732 the new names into the current Maxima environment, but only stores them in
735 These special forms and the general form of @code{save} may be mixed at will.
736 For example, @code{save (@var{filename}, aa, bb, cc=42, functions, [11, 17])}.
738 The special form @code{save (@var{filename}, all)} stores the current state of
739 Maxima. This includes all user-defined variables, functions, arrays, etc., as
740 well as some automatically defined items. The saved items include system
741 variables, such as @mref{file_search_maxima} or @mrefcomma{showtime} if they
742 have been assigned new values by the user; see @mrefdot{myoptions}
744 @code{save} evaluates @var{filename} and quotes all other arguments.
746 @opencatbox{Categories:}
747 @category{Session management}
748 @category{File output}
752 @c -----------------------------------------------------------------------------
754 @deffn {Function} stringout @
755 @fname{stringout} (@var{filename}, @var{expr_1}, @var{expr_2}, @var{expr_3}, @dots{}) @
756 @fname{stringout} (@var{filename}, [@var{m}, @var{n}]) @
757 @fname{stringout} (@var{filename}, input) @
758 @fname{stringout} (@var{filename}, functions) @
759 @fname{stringout} (@var{filename}, values)
761 @code{stringout} writes expressions to a file in the same form the expressions
762 would be typed for input. The file can then be used as input for the
763 @mref{batch} or @mref{demo} commands, and it may be edited for any purpose.
764 @code{stringout} can be executed while @mref{writefile} is in progress.
766 The global flag @mref{file_output_append} governs whether @code{stringout}
767 appends or truncates the output file. When @code{file_output_append} is
768 @code{true}, @code{stringout} appends to the output file. Otherwise,
769 @code{stringout} truncates the output file. In either case, @code{stringout}
770 creates the file if it does not yet exist.
772 The general form of @code{stringout} writes the values of one or more
773 expressions to the output file. Note that if an expression is a
774 variable, only the value of the variable is written and not the name
775 of the variable. As a useful special case, the expressions may be
776 input labels (@code{%i1}, @code{%i2}, @code{%i3}, @dots{}) or output labels
777 (@code{%o1}, @code{%o2}, @code{%o3}, @dots{}).
779 If @mref{grind} is @code{true}, @code{stringout} formats the output using the
780 @code{grind} format. Otherwise the @code{string} format is used. See
781 @code{grind} and @code{string}.
783 The special form @code{stringout (@var{filename}, [@var{m}, @var{n}])} writes
784 the values of input labels m through n, inclusive.
786 The special form @code{stringout (@var{filename}, input)} writes all
787 input labels to the file.
789 The special form @code{stringout (@var{filename}, functions)} writes all
790 user-defined functions (named by the global list @mrefparen{functions}) to the
793 The special form @code{stringout (@var{filename}, values)} writes all
794 user-assigned variables (named by the global list @mrefparen{values}) to the file.
795 Each variable is printed as an assignment statement, with the name of the
796 variable, a colon, and its value. Note that the general form of
797 @code{stringout} does not print variables as assignment statements.
799 @opencatbox{Categories:}
800 @category{Session management}
801 @category{File output}
805 @c -----------------------------------------------------------------------------
807 @deffn {Function} with_stdout @
808 @fname{with_stdout} (@var{f}, @var{expr_1}, @var{expr_2}, @var{expr_3}, @dots{}) @
809 @fname{with_stdout} (@var{s}, @var{expr_1}, @var{expr_2}, @var{expr_3}, @dots{})
811 Evaluates @var{expr_1}, @var{expr_2}, @var{expr_3}, @dots{} and writes any
812 output thus generated to a file @var{f} or output stream @var{s}. The evaluated
813 expressions are not written to the output. Output may be generated by
814 @mrefcomma{print} @mrefcomma{display} @mrefcomma{grind} among other functions.
816 The global flag @mref{file_output_append} governs whether @code{with_stdout}
817 appends or truncates the output file @var{f}. When @code{file_output_append}
818 is @code{true}, @code{with_stdout} appends to the output file. Otherwise,
819 @code{with_stdout} truncates the output file. In either case,
820 @code{with_stdout} creates the file if it does not yet exist.
822 @code{with_stdout} returns the value of its final argument.
824 See also @mref{writefile} and @mrefdot{display2d}
826 @c THIS DOESN'T SEEM VERY IMPORTANT TO MENTION ...
827 @c Note the binding of display2d to be
828 @c false, otherwise the printing will have things like "- 3" instead
832 @c THIS EXAMPLE USES SOME UNIX-ISH CONSTRUCTS -- WILL IT WORK IN WINDOWS ???
833 @c ALSO IT'S SORT OF COMPLICATED AND THE SIMPLER SECOND EXAMPLE ILLUSTRATES with_stdout BETTER !!!
834 @c mygnuplot (f, var, range, number_ticks) :=
835 @c block ([numer:true, display2d:false],
836 @c with_stdout("tmp.out",
837 @c dx: (range[2]-range[1])/number_ticks,
838 @c for x: range[1] thru range[2] step dx
839 @c do print (x, at (f, var=x))),
840 @c system ("echo \"set data style lines; set title '", f,"' ;plot '/tmp/gnu'
841 @c ;pause 10 \" | gnuplot"))$
842 (%i1) with_stdout ("tmp.out", for i:5 thru 10 do
843 print (i, "! yields", i!))$
844 (%i2) printfile ("tmp.out")$
853 @opencatbox{Categories:}
854 @category{File output}
858 @c -----------------------------------------------------------------------------
860 @deffn {Function} writefile (@var{filename})
862 Begins writing a transcript of the Maxima session to @var{filename}.
863 All interaction between the user and Maxima is then recorded in this file,
864 @c FOLLOWING CLAIM PROBABLY NEEDS TO BE QUALIFIED
865 just as it appears on the console.
867 As the transcript is printed in the console output format, it cannot be reloaded
868 into Maxima. To make a file containing expressions which can be reloaded,
869 see @mref{save} and @mrefdot{stringout} @code{save} stores expressions in Lisp
870 form, while @code{stringout} stores expressions in Maxima form.
872 The effect of executing @code{writefile} when @var{filename} already exists
873 depends on the underlying Lisp implementation; the transcript file may be
874 clobbered, or the file may be appended. @mref{appendfile} always appends to
877 It may be convenient to execute @mref{playback} after @code{writefile} to save
878 the display of previous interactions. As @code{playback} displays only the
879 input and output variables (@code{%i1}, @code{%o1}, etc.), any output generated
880 by a print statement in a function (as opposed to a return value) is not
881 displayed by @code{playback}.
883 @mref{closefile} closes the transcript file opened by @code{writefile} or
886 @opencatbox{Categories:}
887 @category{File output}
888 @category{Console interaction}
892 @c -----------------------------------------------------------------------------
893 @node Functions and Variables for TeX Output, Functions and Variables for Fortran Output, Functions and Variables for File Input and Output, File Input and Output
894 @section Functions and Variables for TeX Output
895 @c -----------------------------------------------------------------------------
897 @c -----------------------------------------------------------------------------
899 Note that the built-in TeX output functionality of wxMaxima makes no use of
900 the functions described here but uses its own implementation instead.
903 @deffn {Function} tex @
904 @fname{tex} (@var{expr}) @
905 @fname{tex} (@var{expr}, @var{destination}) @
906 @fname{tex} (@var{expr}, false) @
907 @fname{tex} (@var{label}) @
908 @fname{tex} (@var{label}, @var{destination}) @
909 @fname{tex} (@var{label}, false)
911 Prints a representation of an expression suitable for the TeX document
912 preparation system. The result is a fragment of a document, which can be copied
913 into a larger document but not processed by itself.
915 @code{tex (@var{expr})} prints a TeX representation of @var{expr} on the
918 @code{tex (@var{label})} prints a TeX representation of the expression named by
919 @var{label} and assigns it an equation label (to be displayed to the left of the
920 expression). The TeX equation label is the same as the Maxima label.
922 @var{destination} may be an output stream or file name. When @var{destination}
923 is a file name, @code{tex} appends its output to the file. The functions
924 @code{openw} and @code{opena} create output streams.
926 @code{tex (@var{expr}, false)} and @code{tex (@var{label}, false)}
927 return their TeX output as a string.
929 @code{tex} evaluates its first argument after testing it to see if it is a
930 label. Quote-quote @code{''} forces evaluation of the argument, thereby
931 defeating the test and preventing the label.
933 See also @mref{tex1} and @mrefdot{texput}
938 (%i1) integrate (1/(1+x^3), x);
941 log(x - x + 1) sqrt(3) log(x + 1)
942 (%o1) - --------------- + ------------- + ----------
945 $$-@{@{\log \left(x^2-x+1\right)@}\over@{6@}@}+@{@{\arctan \left(@{@{2\,x-1
946 @}\over@{\sqrt@{3@}@}@}\right)@}\over@{\sqrt@{3@}@}@}+@{@{\log \left(x+1\right)
947 @}\over@{3@}@}\leqno@{\tt (\%o1)@}$$
949 (%i3) tex (integrate (sin(x), x));
952 (%i4) tex (%o1, "foo.tex");
956 @code{tex (@var{expr}, false)} returns its TeX output as a string.
959 @c S : tex (x * y * z, false);
963 (%i1) S : tex (x * y * z, false);
969 @opencatbox{Categories:}
970 @category{TeX output}
971 @category{File output}
975 @c -----------------------------------------------------------------------------
977 @deffn {Function} tex1 (@var{e})
979 Returns a string which represents the TeX output for the expressions @var{e}.
980 The TeX output is not enclosed in delimiters for an equation or any other
983 See also @mref{tex} and @mrefdot{texput}
988 @c tex1 (sin(x) + cos(x));
991 (%i1) tex1 (sin(x) + cos(x));
996 @c -----------------------------------------------------------------------------
998 @deffn {Function} texput @
999 @fname{texput} (@var{a}, @var{s}) @
1000 @fname{texput} (@var{a}, @var{f}) @
1001 @fname{texput} (@var{a}, @var{s}, @var{operator_type}) @
1002 @fname{texput} (@var{a}, [@var{s_1}, @var{s_2}], matchfix) @
1003 @fname{texput} (@var{a}, [@var{s_1}, @var{s_2}, @var{s_3}], matchfix)
1005 Assign the TeX output for the atom @var{a}, which can be a symbol or the name
1008 @code{texput (@var{a}, @var{s})} causes the @code{tex} function to interpolate
1009 the string @var{s} into the TeX output in place of @var{a}.
1011 @code{texput (@var{a}, @var{f})} causes the @code{tex} function to call the
1012 function @var{f} to generate TeX output. @var{f} must accept one argument,
1013 which is an expression which has operator @var{a},
1014 and must return either a string (the TeX output) or @code{false},
1015 indicating that the TeX function in effect when @code{texput} is called
1016 should handle the expression.
1017 @var{f} may call @code{tex1} to generate TeX output for the
1018 arguments of the input expression.
1020 @code{texput (@var{a}, @var{s}, @var{operator_type})}, where @var{operator_type}
1021 is @code{prefix}, @code{infix}, @code{postfix}, @code{nary}, or @code{nofix},
1022 causes the @code{tex} function to interpolate @var{s} into the TeX output in
1023 place of @var{a}, and to place the interpolated text in the appropriate
1026 @code{texput (@var{a}, [@var{s_1}, @var{s_2}], matchfix)} causes the @code{tex}
1027 function to interpolate @var{s_1} and @var{s_2} into the TeX output on either
1028 side of the arguments of @var{a}. The arguments (if more than one) are
1029 separated by commas.
1031 @code{texput (@var{a}, [@var{s_1}, @var{s_2}, @var{s_3}], matchfix)} causes the
1032 @code{tex} function to interpolate @var{s_1} and @var{s_2} into the TeX output
1033 on either side of the arguments of @var{a}, with @var{s_3} separating the
1036 See also @mref{tex} and @mrefdot{tex1}
1040 Assign TeX output for a variable.
1043 @c texput (me,"\\mu_e");
1047 (%i1) texput (me,"\\mu_e");
1054 Assign TeX output for an ordinary function (not an operator).
1057 @c texput (lcm, "\\mathrm{lcm}");
1058 @c tex (lcm (a, b));
1061 (%i1) texput (lcm, "\\mathrm@{lcm@}");
1062 (%o1) \mathrm@{lcm@}
1063 (%i2) tex (lcm (a, b));
1064 $$\mathrm@{lcm@}\left(a , b\right)$$
1068 Call a function to generate TeX output.
1071 @c texfoo (e) := block ([a, b], [a, b] : args (e),
1072 @c concat ("\\left[\\stackrel{",tex1(b),"}{",tex1(a),"}\\right]"))$
1073 @c texput (foo, texfoo);
1074 @c tex (foo (2^x, %pi));
1077 (%i1) texfoo (e) := block ([a, b], [a, b] : args (e),
1078 concat("\\left[\\stackrel@{",tex1(b),"@}@{",tex1(a),"@}\\right]"))$
1079 (%i2) texput (foo, texfoo);
1081 (%i3) tex (foo (2^x, %pi));
1082 $$\left[\stackrel@{\pi@}@{2^@{x@}@}\right]$$
1086 Assign TeX output for a prefix operator.
1090 @c texput ("grad", " \\nabla ", prefix);
1094 (%i1) prefix ("grad");
1096 (%i2) texput ("grad", " \\nabla ", prefix);
1103 Assign TeX output for an infix operator.
1107 @c texput ("~", " \\times ", infix);
1113 (%i2) texput ("~", " \\times ", infix);
1120 Assign TeX output for a postfix operator.
1124 @c texput ("##", "!!", postfix);
1128 (%i1) postfix ("##");
1130 (%i2) texput ("##", "!!", postfix);
1137 Assign TeX output for a nary operator.
1141 @c texput ("@@", " \\circ ", nary);
1142 @c tex (a @@ b @@ c @@ d);
1145 (%i1) nary ("@@@@");
1147 (%i2) texput ("@@@@", " \\circ ", nary);
1149 (%i3) tex (a @@@@ b @@@@ c @@@@ d);
1150 $$a \circ b \circ c \circ d$$
1154 Assign TeX output for a nofix operator.
1158 @c texput ("foo", "\\mathsc{foo}", nofix);
1162 (%i1) nofix ("foo");
1164 (%i2) texput ("foo", "\\mathsc@{foo@}", nofix);
1165 (%o2) \mathsc@{foo@}
1171 Assign TeX output for a matchfix operator.
1174 @c matchfix ("<<", ">>");
1175 @c texput ("<<", [" \\langle ", " \\rangle "], matchfix);
1178 @c texput ("<<", [" \\langle ", " \\rangle ", " \\, | \\,"],
1184 (%i1) matchfix ("<<", ">>");
1186 (%i2) texput ("<<", [" \\langle ", " \\rangle "], matchfix);
1187 (%o2) [ \langle , \rangle ]
1189 $$ \langle a \rangle $$
1191 (%i4) tex (<<a, b>>);
1192 $$ \langle a , b \rangle $$
1194 (%i5) texput ("<<", [" \\langle ", " \\rangle ", " \\, | \\,"],
1196 (%o5) [ \langle , \rangle , \, | \,]
1198 $$ \langle a \rangle $$
1200 (%i7) tex (<<a, b>>);
1201 $$ \langle a \, | \,b \rangle $$
1205 @opencatbox{Categories:}
1206 @category{TeX output}
1210 @c -----------------------------------------------------------------------------
1211 @anchor{get_tex_environment}
1212 @deffn {Function} get_tex_environment (@var{op})
1213 @deffnx {Function} set_tex_environment (@var{op}, @var{before}, @var{after})
1215 Customize the TeX environment output by @code{tex}.
1216 As maintained by these functions, the TeX environment comprises two strings:
1217 one is printed before any other TeX output, and the other is printed after.
1219 Only the TeX environment of the top-level operator in an expression
1220 is output; TeX environments associated with other operators are ignored.
1222 @code{get_tex_environment} returns the TeX environment which is applied
1223 to the operator @var{op}; returns the default if no other environment
1226 @code{set_tex_environment} assigns the TeX environment for the operator
1232 @c get_tex_environment (":=");
1233 @c tex (f (x) := 1 - x);
1234 @c set_tex_environment (":=", "$$", "$$");
1235 @c tex (f (x) := 1 - x);
1238 (%i1) get_tex_environment (":=");
1244 (%i2) tex (f (x) := 1 - x);
1251 (%i3) set_tex_environment (":=", "$$", "$$");
1253 (%i4) tex (f (x) := 1 - x);
1258 @opencatbox{Categories:}
1259 @category{TeX output}
1263 @c -----------------------------------------------------------------------------
1264 @anchor{get_tex_enviroment_default}
1265 @deffn {Function} get_tex_environment_default ()
1266 @deffnx {Function} set_tex_environment_default (@var{before}, @var{after})
1268 Customize the TeX environment output by @code{tex}.
1269 As maintained by these functions, the TeX environment comprises two strings:
1270 one is printed before any other TeX output, and the other is printed after.
1272 @code{get_tex_environment_default} returns the TeX environment which is
1273 applied to expressions for which the top-level operator has no
1274 specific TeX environment (as assigned by @code{set_tex_environment}).
1276 @code{set_tex_environment_default} assigns the default TeX environment.
1281 @c get_tex_environment_default ();
1282 @c tex (f(x) + g(x));
1283 @c set_tex_environment_default ("\\begin{equation}
1285 @c \\end{equation}");
1286 @c tex (f(x) + g(x));
1289 (%i1) get_tex_environment_default ();
1291 (%i2) tex (f(x) + g(x));
1292 $$g\left(x\right)+f\left(x\right)$$
1294 (%i3) set_tex_environment_default ("\\begin@{equation@}
1296 \\end@{equation@}");
1297 (%o3) [\begin@{equation@}
1300 (%i4) tex (f(x) + g(x));
1302 g\left(x\right)+f\left(x\right)
1307 @opencatbox{Categories:}
1308 @category{TeX output}
1312 @c -----------------------------------------------------------------------------
1313 @node Functions and Variables for Fortran Output, , Functions and Variables for TeX Output, File Input and Output
1314 @section Functions and Variables for Fortran Output
1315 @c -----------------------------------------------------------------------------
1317 @c -----------------------------------------------------------------------------
1319 @defvr {Option variable} fortindent
1320 Default value: @code{0}
1322 @code{fortindent} controls the left margin indentation of
1323 expressions printed out by the @mref{fortran} command. @code{0} gives normal
1324 printout (i.e., 6 spaces), and positive values will causes the
1325 expressions to be printed farther to the right.
1327 @opencatbox{Categories:}
1328 @category{Translation and compilation}
1332 @c -----------------------------------------------------------------------------
1334 @deffn {Function} fortran (@var{expr})
1336 Prints @var{expr} as a Fortran statement.
1337 The output line is indented with spaces.
1338 If the line is too long, @code{fortran} prints continuation lines.
1339 @code{fortran} prints the exponentiation operator @code{^} as @code{**},
1340 and prints a complex number @code{a + b %i} in the form @code{(a,b)}.
1342 @var{expr} may be an equation. If so, @code{fortran} prints an assignment
1343 statement, assigning the right-hand side of the equation to the left-hand side.
1344 In particular, if the right-hand side of @var{expr} is the name of a matrix,
1345 then @code{fortran} prints an assignment statement for each element of the
1348 If @var{expr} is not something recognized by @code{fortran},
1349 the expression is printed in @mref{grind} format without complaint.
1350 @code{fortran} does not know about lists, arrays, or functions.
1352 @mref{fortindent} controls the left margin of the printed lines.
1353 @code{0} is the normal margin (i.e., indented 6 spaces). Increasing
1354 @code{fortindent} causes expressions to be printed further to the right.
1356 When @mref{fortspaces} is @code{true}, @code{fortran} fills out
1357 each printed line with spaces to 80 columns.
1359 @code{fortran} evaluates its arguments; quoting an argument defeats evaluation.
1360 @code{fortran} always returns @code{done}.
1362 See also the function @mxref{function_f90, f90} for printing one or more
1363 expressions as a Fortran 90 program.
1368 (%i1) expr: (a + b)^12$
1369 (%i2) fortran (expr);
1372 (%i3) fortran ('x=expr);
1375 (%i4) fortran ('x=expand (expr));
1376 x = b**12+12*a*b**11+66*a**2*b**10+220*a**3*b**9+495*a**4*b**8+792
1377 1 *a**5*b**7+924*a**6*b**6+792*a**7*b**5+495*a**8*b**4+220*a**9*b
1378 2 **3+66*a**10*b**2+12*a**11*b+a**12
1380 (%i5) fortran ('x=7+5*%i);
1383 (%i6) fortran ('x=[1,2,3,4]);
1392 @opencatbox{Categories:}
1393 @category{Translation and compilation}
1397 @c -----------------------------------------------------------------------------
1399 @defvr {Option variable} fortspaces
1400 Default value: @code{false}
1402 When @code{fortspaces} is @code{true}, @code{fortran} fills out
1403 each printed line with spaces to 80 columns.
1405 @opencatbox{Categories:}
1406 @category{Translation and compilation}