Texinfo compatibility fix
[maxima.git] / interfaces / emacs / emaxima / emaxima.sty
blobef6004570b3ccef715d2ca6f4a530c5fe5eb32fb
1 %%%% Identification
2 \ProvidesPackage{emaxima}[2004/12/20]
4 %%% Commentary
5 % This package can be loaded with the command
6 % \usepackage[options...]{emaxima}
7 % The possible options (explained below) are:
8 % breqn, lines, listings, preview
9 % The breqn option will use the LaTeX breqn package (which must be
10 % installed) to break long maxima lines into shorter lines
11 % automatically.
12 % The preview option will enable the Emacs preview package (which
13 % must be installed) to preview the maxima environments.
14 % The lines option will put lines before and after some of the
15 % environments.
16 % The listings option will use the LaTeX listings package (which must
17 % be installed) to typeset some of the maxima code. To use this, the
18 % file maxima.sty must be in the search path for TeX.
20 % This package provides the following environments:
21 % \begin{maxima}
22 % ...
23 % \end{maxima}
24 % and
25 % \begin{maximasession}
26 % ...
27 % \end{maximasession}
28 % (Both versions have starred forms: maxima* and maximasession*, which
29 % will be skipped when the document is typeset.)
30 % The maxima environments can be joined with [...] optional arguments
31 % (as described in the documentation).
32 % The maxima and maximasession environments are sometimes split into
33 % input and output, separated by
34 % \maximaoutput
35 % or (if the output is in TeX form)
36 % \maximaoutput*
37 %%%
39 %%%% How to customize
40 % Indentation
41 % \setlength{\maximaindent}{<len>} will set the indentation
42 % of all the maxima code
43 % Colors
44 % \renewcommand{\maximapromptcolor}{<color>}
45 % \renewcommand{\maximainputcolor}{<color>}
46 % \renewcommand{\maximaoutputcolor}{<color>}
47 % will set the colors of the TeX output of the sessions.
48 % Cell appearance
49 % The top, middle and bottom of a maxima cell are determined by the
50 % commands: \maximatop, \maximamiddle, \maximabottom
51 % By default, \maximamiddle is set to \maximaoutputmarker.
52 % If the lines option is used, then \maximatop and \maximabottom
53 % are set to \maximaboxtop and \maximaboxbottom, respectively.
54 % Otherwise, \maximatop and \maximabottom are set to do nothing.
55 % The top and bottom of a maxima session are determined by
56 % \maximasessiontop and \maximasessionbottom, respectively.
57 % By default, they do nothing.
58 % Fonts
59 % The verbatim output for maxima cells is inserted with
60 % \maximaverbatiminput. This will do one of two things.
61 % If the listings option is used, this is defined by
62 % \lstinputlisting[style=emaxima]{#1}, and so can be adjusted by
63 % resetting the emaxima style. By default, this style is given by
64 % \lstdefinestyle{emaxima}
65 % {language=maxima,
66 % aboveskip=0pt,
67 % belowskip=0pt,
68 % xleftmargin=\maximaindent}
69 % If the listings package is not used, then \maximaverbatiminput is
70 % defined by \VerbatimInput[xleftmargin=\maximaindent]{#1}
71 % (\VerbatimInput is from the fancyvrb package.)
72 % In either case, it can be adjusted by redefining
73 % \maximaverbatiminput, which is expected to indent everything by
74 % \maximaindent
75 % The fonts used in the maximasessions is \maximafont, by default
76 % \ttfamily.
79 %%%% Initial Code
80 \RequirePackage{ifthen}
81 \RequirePackage{alltt}
82 \usepackage{ifpdf}
83 \ifpdf
84 \usepackage[pdftex]{color}
85 \usepackage{pdfcolmk}
86 \color{black}
87 \else
88 \usepackage{color}
89 \fi
90 \newboolean{m@ximapreview}
91 \setboolean{m@ximapreview}{false}
92 \newboolean{m@ximabreqn}
93 \setboolean{m@ximabreqn}{false}
94 \newboolean{m@ximalistings}
95 \setboolean{m@ximalistings}{false}
96 \newboolean{m@ximalines}
97 \setboolean{m@ximalines}{false}
98 \newboolean{m@ximainline}
99 \setboolean{m@ximainline}{false}
101 %%%% Declaration of options
102 \DeclareOption{preview}
103 {\setboolean{m@ximapreview}{true}}
105 \DeclareOption{breqn}
106 {\setboolean{m@ximabreqn}{true}}
108 \DeclareOption{listings}
109 {\setboolean{m@ximalistings}{true}}
111 \DeclareOption{lines}
112 {\setboolean{m@ximalines}{true}}
114 \DeclareOption{inline}
115 {\setboolean{m@ximainline}{true}}
117 %%%% Execution of options
118 \ProcessOptions
120 %%%% Package loading
122 %%%% Main code
124 %%%% Some TeX coding
125 % First of all, a command to deactivate everything except \
126 \chardef \other = 12
127 \def\m@ximadeactivate{%
128 \catcode`\$ = \other
129 \catcode`\& = \other
130 \catcode`\# = \other
131 \catcode`\% = \other
132 \catcode`\~ = \other
133 \catcode`\^ = \other
134 \catcode`\_ = \other
135 \catcode`\ = \other
136 \catcode`\{ = \other
137 \catcode`\} = \other}
139 % Next, a comment environment which ends at the next \end,
140 % \maximaoutput or \maximasessionoutput.
141 % It does this by looking at the next token and seeing if it is
142 % one of these. If not, it discards it; if it is, it ends the
143 % comment environment.
144 % Putting \m@ximastartcomment (even inside a command) will start this.
145 \def\m@ximacomment{%
146 \futurelet\m@xima@token\m@ximacommentl@@k}
147 \def\m@ximacommentl@@k{%
148 \ifx\m@xima@token\end
149 \let\m@xima@next=\m@xima@endcomment
150 \else
151 \ifx\m@xima@token\maximaoutput
152 \let\m@xima@next=\m@xima@endcomment
153 \else
154 \ifx\m@xima@token\par
155 \let\m@xima@next=\m@ximaparcomment
156 \else
157 \let\m@xima@next=\m@ximacommenttwo
161 \m@xima@next}
163 \def\m@ximaparcomment#1\par{%
164 \m@ximacomment}
166 \def\m@ximacommenttwo#1 {%
167 \m@ximacomment}
169 \def\m@ximastartcomment{%
170 \begingroup
171 \m@ximadeactivate
172 \m@ximacomment}
174 \def\m@xima@endcomment{%
175 \endgroup}
177 % Also, a comment environment which only ends with \end
178 % Putting \m@ximastartfullcomment (even inside a command) will start this.
179 \def\m@ximafullcomment{%
180 \futurelet\m@xima@token\m@ximafullcommentl@@k}
181 \def\m@ximafullcommentl@@k{%
182 \ifx\m@xima@token\end
183 \let\m@xima@next=\m@xima@endfullcomment
184 \else
185 \ifx\m@xima@token\par
186 \let\m@xima@next=\m@ximaparfullcomment
187 \else
188 \let\m@xima@next=\m@ximafullcommenttwo
191 \m@xima@next}
193 \def\m@ximaparfullcomment#1\par{%
194 \m@ximafullcomment}
196 \def\m@ximafullcommenttwo#1 {%
197 \m@ximafullcomment}
199 \def\m@ximastartfullcomment{%
200 \begingroup
201 \m@ximadeactivate
202 \m@ximafullcomment}
204 \def\m@xima@endfullcomment{%
205 \endgroup}
207 % Next, a write environment which ends at the next \maximaoutput or \end.
208 % It does this by looking at the next token and seeing if it is
209 % one of these. If not, it writes it; if it is, it ends the
210 % write environment.
211 % It can be started with \m@ximastartwrite
212 \def\m@ximatempfile{\jobname.tmp}
214 \newwrite\m@ximaout
216 \def\m@ximawriteskipline#1 {%
217 \m@ximawrite}
219 \def\m@ximawrite{%
220 \futurelet\m@xima@token\m@ximawritel@@k}
222 \def\m@ximawritel@@k{%
223 \ifx\m@xima@token\end
224 \let\m@xima@next=\m@xima@endwrite
225 \else
226 \ifx\m@xima@token\maximaoutput
227 \let\m@xima@next=\m@xima@endwrite
228 \else
229 \ifx\m@xima@token\par
230 \let\m@xima@next=\m@xima@parwrite
231 \else
232 \let\m@xima@next=\m@ximawritetwo
236 \m@xima@next}
238 \def\m@xima@parwrite#1\par{%
239 \immediate\write\m@ximaout{}
240 \m@ximawrite}
242 \def\m@ximawritetwo#1 {%
243 \immediate\write\m@ximaout{\noexpand#1}%
244 \m@ximawrite}
246 \def\m@ximastartwriteskipline{%
247 \begingroup
248 \immediate\openout \m@ximaout \m@ximatempfile
249 \m@ximadeactivate
250 \m@ximawriteskipline}
252 \def\m@ximastartwrite{%
253 \begingroup
254 \immediate\openout \m@ximaout \m@ximatempfile
255 \m@ximadeactivate
256 \m@ximawrite}
258 \def\m@xima@endwrite{%
259 % \immediate\write\m@ximaout{ }
260 \immediate\closeout\m@ximaout%
261 \endgroup
262 \m@ximawritetempfile}
264 % A command to verbatim typeset the file.
265 % It is determined by the command \maximaverbatiminput,
266 % whose default value depends on the option listings.
268 \newcommand{\m@ximawritetempfile}
269 {\par
270 \noindent
271 \maximaverbatiminput{\m@ximatempfile}
272 \par}
274 % The \maximaverbatiminput command depends on whether the
275 % listings option is given.
276 % The command should indent the code to \maximaindent
278 \newlength{\maximaindent}
279 \setlength{\maximaindent}{2ex}
280 \newcommand{\m@ximaindent}
281 {\setlength{\leftskip}{\maximaindent}
282 \setlength{\rightskip}{\maximaindent}}
285 \ifthenelse{\boolean{m@ximalistings}}
286 {\RequirePackage{maxima}
287 \lstdefinestyle{emaxima}
288 {language=maxima,
289 aboveskip=0pt,
290 belowskip=0pt,
291 xleftmargin=\maximaindent}
292 \newcommand{\maximaverbatiminput}[1]
293 {\lstinputlisting[style=emaxima]{#1}}}
294 {\RequirePackage{fancyvrb}
295 \newcommand{\maximaverbatiminput}[1]
296 {\VerbatimInput[xleftmargin=\maximaindent]{#1}}}
298 % Now, a command to see if the argument ends in a colon or not
299 % \maximaendsincolon{string} will set the boolean maximaendsincolon
300 % to be true if string ends with a colon, false otherwise.
302 \newboolean{m@ximaendsincolon}
303 \newcommand{\maximaendsincolon}[1]
304 {\setboolean{m@ximaendsincolon}{false}
305 \m@ximacheckcolon#1\end}
307 \newcommand{\m@ximacheckcolon}[1]
308 {\ifx#1\end
309 \let\next=\relax
310 \else
311 \let\next=\m@ximacheckcolon
312 \ifx#1:
313 \setboolean{m@ximaendsincolon}{true}
314 \else
315 \setboolean{m@ximaendsincolon}{false}
318 \next}
320 \newcommand{\m@ximaremovecolon}[1]
321 {{\m@cstrip#1}}
322 \def\m@cstrip#1:{#1}
324 %% Now, set some environments
326 %% We need environments for maxima, maxima*, maximasession, maximasession*
327 %% Any environment that ends in * will be a comment environment
329 \newenvironment{maxima*}[1][]
330 {\m@ximastartfullcomment}
333 \newenvironment{maximasession*}[1][]
334 {\m@ximastartfullcomment}
337 %% The maxima environment should write verbatim the ``top'' part
338 %% (before an output marker) and the the bottom verbatim or TeXed.
340 %% A macro for writing Maxima in the ``proper'' font
342 \newcommand{\Maxima}{\textsf{\textsl{Maxima}}}
344 %% The top of the box
346 \newcommand{\m@ximanoparttop}
347 {\vskip -1ex
348 \hbox to \hsize{\vrule depth 1ex height .3pt width .4pt
349 \vrule height .4pt depth 0pt width 1.1em
350 \lower .4ex \hbox{\tiny ~\Maxima}
351 \hrulefill
352 \vrule depth 1ex height .3pt width .4pt}
353 \vskip 1ex}
355 \newcommand{\m@ximaparttop}
356 {\vskip -1ex
357 \hbox to \hsize{\vrule depth 1ex height .3pt width .4pt
358 \vrule height .4pt depth 0pt width 1.1em
359 \lower .4ex \hbox{\tiny ~\Maxima}
360 \hrulefill
361 \lower .4ex \hbox{\tiny~\m@ximapartname}
362 \hrulefill
363 \vrule depth 1ex height .3pt width .4pt}
364 \vskip 1ex}
366 \newcommand{\maximaboxtop}
367 {\ifthenelse{\equal{\m@ximapartname}{}}
368 {\m@ximanoparttop}
369 {\m@ximaparttop}}
371 %% The bottom of the box
373 \newcommand{\maximaboxbottom}
374 {\hbox to \hsize{\vrule depth 0ex height 1ex width .4pt
375 \hrulefill
376 \vrule depth 0ex height 1ex width .4pt}}
378 %% The middle of the box
380 \newcommand{\maximaoutputmarker}
381 {\par\noindent
382 \hspace*{\maximaindent}
383 \rule{.4\linewidth}{0.4pt}
384 \par\noindent}
386 % \newcommand{\maximaoutputmarker}
387 % {\par\noindent
388 % \vskip -1ex
389 % \hbox to \hsize{\hskip 1.5em
390 % \vrule height .4pt depth 0pt width 3em
391 % \lower .4ex \hbox{\tiny Output}
392 % \hrulefill
393 % \hskip 1.5em}
394 % \vskip 1ex
395 % \par\noindent}
397 %% Setting the name
399 \newcommand{\m@ximasetname}[1]
400 {\maximaendsincolon{#1}
401 \ifthenelse{\equal{#1}{}}
402 {\renewcommand{\m@ximapartname}{}}
403 {\ifthenelse{\boolean{m@ximaendsincolon}}
404 % {\renewcommand{\m@ximapartname}{Definition of #1}}
405 {\renewcommand{\m@ximapartname}{Definition of \m@ximaremovecolon{#1}}}
406 {\renewcommand{\m@ximapartname}{#1}}}}
408 %% Now the environments
410 \newboolean{m@ximaverbatimoutput}
411 \newboolean{m@ximatexoutput}
413 \newcommand{\m@ximapartname}{}
415 % Default top, middle and bottom of the maxima environment
416 \ifthenelse{\boolean{m@ximalines}}
417 {\newcommand{\maximatop}{\maximaboxtop}
418 \newcommand{\maximamiddle}{\maximaoutputmarker}
419 \newcommand{\maximabottom}{\maximaboxbottom}}
420 {\newcommand{\maximatop}{}
421 \newcommand{\maximamiddle}{\maximaoutputmarker}
422 \newcommand{\maximabottom}{}}
424 \newcommand{\maximaoutput}{}
426 \newenvironment{maxima}[1][]%
427 {~\par\renewcommand{\maximaoutput}
428 {\@ifstar{\maximatexoutput}{\maximaverbatimoutput}}
429 \begingroup
430 \m@ximasetname{#1}
431 \setboolean{m@ximaverbatimoutput}{false}
432 \setboolean{m@ximatexoutput}{false}
433 \maximatop
434 \ifthenelse{\equal{\m@ximapartname}{}}
435 {\m@ximastartwrite}{\m@ximastartwriteskipline}}
436 {\ifthenelse{\boolean{m@ximaverbatimoutput}}
437 {} % verbatim output
438 {\ifthenelse{\boolean{m@ximatexoutput}}
439 {\par\noindent} % TeX output
440 {}} % no output
441 \maximabottom
442 \endgroup
443 \par\noindent}
445 \newcommand{\maximaverbatimoutput}
446 {\setboolean{m@ximaverbatimoutput}{true}
447 \maximamiddle
448 \m@ximastartwrite}
450 \newcommand{\maximatexoutput}
451 {\def\m##1\\{%
452 \par\noindent
453 \begingroup\maximafont\color{\maximaoutputcolor}
454 \begin{em@ximam@th} ##1 \end{em@ximam@th}\endgroup}
455 \def\t##1.##2\\{%
456 \par\noindent
457 \begingroup\maximafont\color{\maximaoutputcolor}\textcolor{\maximapromptcolor}
458 {(\%t##1)}\begin{em@ximam@th} ##2 \end{em@ximam@th}\endgroup}
459 \def\p{\begingroup\color{\maximaoutputcolor}
460 \setupm@ximasessionverbatim\dom@ximasessionpreprompt}
461 \setboolean{m@ximatexoutput}{true}
462 \maximamiddle
463 \m@ximaindent
464 \par\noindent}
466 %% Some stuff to help with lists of examples
468 \newcommand{\maximaexamplesname}{List of Maxima Examples}
470 %% Some stuff of the list of examples
471 \@ifclassloaded{book}
472 {\newcounter{maximaexample}[chapter]
473 \newcommand\listofmaximaexamples{%
474 \if@twocolumn
475 \@restonecoltrue\onecolumn
476 \else
477 \@restonecolfalse
479 \chapter*{\maximaexamplesname}%
480 \@mkboth{\MakeUppercase\maximaexamplesname}%
481 {\MakeUppercase\maximaexamplesname}%
482 \@starttoc{lom}%
483 \if@restonecol\twocolumn\fi
485 \newcommand{\maxcaption}[1]{\addcontentsline{lom}{figure}
486 {\protect\numberline{\thechapter.\themaximaexample} {\protect\ignorespaces #1}}}}
487 {\newcounter{maximaexample}
488 \newcommand\listofmaximaexamples{%
489 \if@twocolumn
490 \@restonecoltrue\onecolumn
491 \else
492 \@restonecolfalse
494 \section*{\maximaexamplesname}%
495 \@mkboth{\MakeUppercase\maximaexamplesname}%
496 {\MakeUppercase\maximaexamplesname}%
497 \@starttoc{lom}%
498 \if@restonecol\twocolumn\fi
500 \newcommand{\maxcaption}[1]{\addcontentsline{lom}{figure}
501 {\protect\numberline{\themaximaexample}
502 {\protect\ignorespaces #1}}}}
504 \newcommand{\l@example}[2]{\par\noindent#1 {\itshape #2}}
507 %% The maximasession environment should ignore the ``top'' part
508 %% (before an output marker) and the the bottom verbatim or TeXed.
509 %% If TeXed, some commands need to be set up.
511 \newboolean{m@ximasessionverbatimoutput}
512 \newboolean{m@ximasessiontexoutput}
514 \newcommand{\maximasessiontop}{}
515 \newcommand{\maximasessionbottom}{}
517 \newenvironment{maximasession}[1][]
518 {~\par\renewcommand{\maximaoutput}
519 {\begingroup
520 \@ifstar{\maximasessiontexoutput}{\maximasessionverbatimoutput}}
521 \ifthenelse{\equal{#1}{}}
522 {}{\stepcounter{maximaexample}
523 \maxcaption{#1}}
524 \setboolean{m@ximasessionverbatimoutput}{false}
525 \setboolean{m@ximasessiontexoutput}{false}
526 \m@ximastartcomment}
527 {\ifthenelse{\boolean{m@ximasessionverbatimoutput}}
528 {\maximasessionbottom
529 \endgroup\par\noindent} % verbatim output
530 {\ifthenelse{\boolean{m@ximasessiontexoutput}}
531 {\par\maximasessionbottom
532 \endgroup\par\noindent} % TeX output
533 {}}}
535 \newenvironment{maximanu}
536 {\begin{maxima}}
537 {\end{maxima}}
539 \newenvironment{maximasessionnu}
540 {\begin{maximasession}}
541 {\end{maximasession}}
543 \newenvironment{maximanu*}
544 {\begin{maxima*}}
545 {\end{maxima*}}
547 \newenvironment{maximasessionnu*}
548 {\begin{maximasession*}}
549 {\end{maximasession*}}
551 \ifthenelse{\boolean{m@ximapreview}}
552 {\RequirePackage{preview}
553 \PreviewEnvironment[{[]}]{maxima}
554 \PreviewEnvironment{maximasession}}
557 \newcommand{\maximasessionverbatimoutput}
558 {\setboolean{m@ximasessionverbatimoutput}{true}
559 \maximasessiontop
560 \m@ximastartwrite}
562 \ifthenelse{\boolean{m@ximainline}}
563 {\newenvironment{em@ximam@th}
564 {\hfil\(}
565 {\)\hfil}}
566 {\ifthenelse{\boolean{m@ximabreqn}}
567 {\RequirePackage[cmbase]{flexisym}
568 \RequirePackage{breqn}
569 \newenvironment{em@ximam@th}
570 {\begin{dmath*}}
571 {\end{dmath*}}}
572 {\newenvironment{em@ximam@th}
573 {\begin{displaymath}}
574 {\end{displaymath}}}}
576 \newcommand{\maximapromptcolor}{red}
577 \newcommand{\maximainputcolor}{blue}
578 \newcommand{\maximaoutputcolor}{blue}
579 \newcommand{\maximafont}{\ttfamily}
582 \def\m@ximauncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}
583 \def\m@ximapercent{\%}
584 \def\setupm@ximasessionverbatim{
585 \obeylines \m@ximauncatcodespecials \obeyspaces}
586 {\obeyspaces\global\let =\ }
587 {\catcode`\|=0 \catcode`\\=12 %
588 |obeylines|gdef|dom@ximatexsessionverbatim#1.#2\\{|maximafont|par|noindent|textcolor{|maximapromptcolor}{(|m@ximapercent{}i#1)}#2|par|endgroup}
589 |obeylines|gdef|dom@ximasessionpreprompt#1\\{|par|noindent#1|par|endgroup}}
591 \newcommand{\maximasessionpreoutput}{}
593 \newcommand{\m@ximasetupsessiontexoutput}
594 {\def\i{\begingroup\color{\maximainputcolor}
595 \setupm@ximasessionverbatim\dom@ximatexsessionverbatim}
596 \def\o##1.##2\\{%
597 \par\noindent
598 \begingroup\maximafont\color{\maximaoutputcolor}\textcolor{\maximapromptcolor}
599 {(\%o##1)}\begin{em@ximam@th} ##2 \end{em@ximam@th}\endgroup}
600 \def\oo##1\\{%
601 \par\noindent
602 \begingroup\maximafont\color{\maximaoutputcolor}
603 \begin{em@ximam@th} ##1 \end{em@ximam@th}\endgroup}
604 \def\t##1.##2\\{%
605 \par\noindent
606 \begingroup\maximafont\color{\maximaoutputcolor}\textcolor{\maximapromptcolor}
607 {(\%t##1)}\begin{em@ximam@th} ##2 \end{em@ximam@th}\endgroup}
608 \def\ps{maximasessionpreoutput}
609 \def\p{\begingroup\color{\maximaoutputcolor}
610 \setupm@ximasessionverbatim\dom@ximasessionpreprompt}}
612 \newcommand{\maximasessiontexoutput}
613 {\setboolean{m@ximasessiontexoutput}{true}
614 \maximasessiontop
615 \m@ximasetupsessiontexoutput
616 \m@ximaindent}