![]() |
Formatted and structured output | ![]() |
In the previous section, we have seen that output from applications is encapsulated in blocks of the form
DATA_BEGINformat:messageDATA_END |
Currently implemented formats include verbatim, utf8, latex, html, ps, file (the name of an image file), math (a formula in prefix Scheme notation), scheme and command, as well as any other data format for which TeXmacs has a converter from format-snippet to TeXmacs (blocks in unknown formats are treated as verbatim). Certain formats, such as verbatim, allow the message to recursively contain blocks of the same form. The scheme format is used for sending TeXmacs trees in the form of Scheme expressions. A detailed description of all formats can be found in the section on the internals of the plug-in system.
The formula plug-in demonstrates the use of LaTeX as the output format. It consists of the files
formula/Makefile
formula/progs/init-formula.scm
formula/src/formula.cpp
The body of the main loop of
formula.cpp
is given by
int i, nr;
cin >> nr;
cout << DATA_BEGIN << "latex:";
cout << "$";
for (i=1; i<nr; i++)
cout << "x_{" << i << "}+";
cout << "x_{" << i << "}$";
cout << DATA_END;
cout.flush (); |
Similarly, the use of nested output blocks is demonstrated by the nested plug-in; see in particular the source file nested/src/nested.cpp.
Remark 1. LaTeX is the most common transmission format for mathematical formulas, because this is the format which is most widely used. More semantically secure alternatives are the scheme and math formats, and we recommend you to keep in mind the possibility of sending your output in tree format.
Nevertheless, we enriched standard LaTeX with the \* and \bignone commands for multiplication and closing big operators. This allows us to distinguish between
a \* (b + c)
(i.e. \(a\) multiplied by \(b + c\)) and
f(x + y)
(i.e. \(f\) applied to \(x + y\)). Similarly, in
\sum_{i=1}^m a_i \bignone + \sum_{j=1}^n b_j \bignone
the \bignone command is used in order to specify the scopes of the \sum operators.
It turns out that the systematic use of the \* and \bignone commands, in combination with clean LaTeX output for the remaining constructs, makes it a priori possible to associate an appropriate meaning to your output. In particular, this usually makes it possible to write additional routines for copying and pasting formulae between different systems.
It is important to remind that structured output can be combined with the power of TeXmacs as a structured editor. For instance, the markup plug-in demonstrates the definition of an additional tag foo, which is used as an additional primitive in the output of the application. More precisely, the markup plug-in consists of the following files:
markup/Makefile
markup/packages/session/markup.ts
markup/progs/init-markup.scm
markup/src/markup.cpp
The style package markup.ts contains the following definition for foo:
<math|<assign|foo|<macro|x|<frac|1|1+x>>>> |
The foo tag is used in the following way in the body of the main loop of
markup.cpp
:
char buffer[100];
cin.getline (buffer, 100, '\n');
cout << DATA_BEGIN << "latex:";
cout << "$\\foo{" << buffer << "}$";
cout << DATA_END;
cout.flush (); |
Notice that the style package markup.ts also defines the markup-output environment:
<assign|markup-output|<macro|body|<generic-output|<with|par-mode|center|body>>>> |
This has the effect of centering the output in sessions started using Insert\(\rightarrow\)Session\(\rightarrow\)Markup.