sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.34k stars 452 forks source link

Make sure latex doesn't do weird things with R output #7664

Open kcrisman opened 14 years ago

kcrisman commented 14 years ago

jsmath doesn't understand output from R, so you have to turn off typesetting for it to work. E.g., it doesn't know what to do with this table which results from R output - I guess it's really a latex() problem.

sage: r.data('Cars93')
[1] "sage0"
sage: a._latex_()

% latex.default(sage3, file = "") 
%
\begin{table}[!tbp]
 \begin{center}
 \begin{tabular}{l}\hline\hline
\multicolumn{1}{c}{}\tabularnewline
\hline
sage0\tabularnewline
\hline
\end{tabular}

\end{center}

\end{table}

CC: @jasongrout @gvol

Component: packages: standard

Keywords: latex, R, jsmath, r-project

Issue created by migration from https://trac.sagemath.org/ticket/7664

jasongrout commented 14 years ago
comment:2

Can R latex its own expressions?

If not, we might explore the rpy2 interface a bit more to get the data into python formats.

Also, when evaluating in the R system, we ought to enclose things in a div that tells jsmath "Hands off!"

kcrisman commented 14 years ago
comment:3

Replying to @jasongrout:

Can R latex its own expressions?

Apparently using the Hmisc package. From our own doctests in r.py:

    def _latex_(self):
        r"""
        Return LaTeX representation of this R object.

        This calls the \code{latex} command in R.

        OUTPUT:
           a latex expression (basically a string)

        EXAMPLES:
            sage: latex(r(2))  #optional requires the Hmisc R package
            2
        """
        self._check_valid()
        P = self.parent()
        # latex is in Hmisc, this is currently not part of Sage's R!!!
        try:
            P.library('Hmisc')
        except ImportError:
            raise RuntimeError, "The R package 'Hmisc' is required for R to LaTeX conversion, but it is not available."
        return LatexExpr(P.eval('latex(%s, file="");'%self.name()))

Note that this requires the package 'survival', which is also recommended but not currently in Sage.

Anyway, I checked some more and apparently the problem is that jsmath doesn't understand the latex output created by Hmisc - namely, it doesn't know what a table is, nor center, nor tabular.

So maybe if EMBEDDED_MODE=True, we should have a thing that strips out all that other stuff and just puts in the stuff in the middle. Incidentally, the output in the optional doctest above is incorrect, we get

% latex.default(sage0, file = "") 
%
\begin{table}[!tbp]
 \begin{center}
 \begin{tabular}{r}\hline\hline
\multicolumn{1}{c}{}\tabularnewline
\hline
$2$\tabularnewline
\hline
\end{tabular}

\end{center}

\end{table}

so maybe this was with a previous version of Hmisc.

Or we could even strip out those things and replace them with equivalent HTML stuff when EMBEDDED_MODE is true.

kcrisman commented 12 years ago

Changed keywords from latex, R, jsmath to latex, R, jsmath, r-project

gvol commented 11 years ago
comment:6

FWIW, this affects sage-mode in Emacs when typesetting output as well.