I write this long message because I am perplexed about the output
facilities in Julia, to the point that I have no idea how to proceed with
my design.
I want to write facilities for outputting in TeX/LaTeX my objects. I am
trying to port GAP code and my objects could be things like character
tables, polynomials, matrix of polynomials, etc… The main purpose of
LaTeX facilities would be to prepare presentations/papers from julia
computations.
With the philosophy I know, I would design this as follows:
-
a function
stringlatex
for each type, which would build the LaTeX output
recursively — for instance a matrix of Laurent polynomials would give\begin{array}{cc}\\ x^{10}+1 & y-2\\ y^{-5}+1 & x-y\end{array}
where
stringlatex
for a matrix calls itselfstringlatex
on each entry. -
a function
latex
which would in jupyter display the LaTeX inline and in
the Repl would call latex/xdvi or pdflatex/xpdf.
The main purpose of latex
would be mainly to check the output from
stringlatex
, but it could also serve as a display
function sometimes.
It seems in julia print functions are the primordial form for output and
string functions are derived from them, which seems a little more
complicated than the other way:
-
from string you can derive print as
write(string(x))
-
but from
print
to derivestring
you need more complex code as you can
see by looking at the source ofprint_to_string
Note added later: I discovered that there is an sprint
function which
can derive a string
function from any print
function: why is not
string
defined as sprint(print,...)
?
Looking at the source, it seems that everything is derived at the bottom
from show
with some options — this is good: all formatting routines
have the same recursive logic, so this way they can share code. Thus I
explored how to pass options to show
and I found two ways:
-
build an
IOContext
with a specific option, likeLaTeX=>true
-
use a mime-type:
show(io,"application/x-latex",x)
and here I got very confused: which I should use? What is the relationship
between IOContexts
and mime-types?
These questions started me on exploring further the output facilities of
julia which left me even more confused.
From my background in the 15 programming languages I learned along the
years (I am 67) I see 3 basic forms of print/string
functions which are
necessary in a language (of course there are other ones like
“export-to-latex” which is my current interest, but you may want also
“export-to-html” or “export-to-xxx” where xxx is anything you may think of:
R, Mathlab, Maple, Mathematica, etc…)
1- default output at the repl. This could have a ‘compact’ mode and a
‘verbose’ mode.
2- A way to print a representation which can be read back — in the
context of julia this means deriving from an object x a string s such
that eval(parse(s))=x
3- A ‘display’ function which would show an object in the most enlightening
way possible. For example, for a character table it would be a big
square matrix with labels on the rows and columns (perhaps several rows
of labels on the columns for the class names, class sizes, powermap,
etc…).
An for each of these modes there would be a string function and a print
function.
There is a ‘display’ function in julia, which seems to fit my requirement
3, but I could not find anything which fits items 1 or 2.
I think a facility for 2 is needed in the context of metaprogramming (even of the
most naive sort: preparing text files containing data which can be read in
julia – I found that this is not as trivial as it seems: matrices
outputted by GAP as vectors of vectors can sometimes not be read in julia
depending on what is the last token before a line break).
-
I could not discover which function does the printing at the repl.
-
The correspondence between string and print functions does not seem to
fit any coherent naming scheme: I could discover that ‘string’ is the
string function for ‘print’ and ‘repr’ the string function for showall, but
I could not find what are the string functions for ‘show’ or ‘showcompact’. -
Also I was suprised that there is a ‘summary’ function which is a string
function and not a print function contrary to the apparent philosophy of
the other ones.
Excuse me for this long rant and if I do not use the proper terminology,
but the point is that I am here to learn.