Opened 8 years ago

# Fix LaTeXing of strings — at Version 19

Reported by: Owned by: novoselt was major sage-5.12 user interface latex cremona, ddrake, jhpalmieri Andrey Novoseltsev N/A

#11498 made "ASCII-art" strings look reasonably well when displayed via LaTeX methods. One of the changes was damaged in #9774, resulting in invalid LaTeX code that works in MathJax?, but not in SageTeX. Let's fix it again.

### comment:3 Changed 8 years ago by novoselt

• Authors set to Andrey Novoseltsev
• Status changed from new to needs_review

### comment:4 Changed 8 years ago by nthiery

You might want to have a look at #13624; it replaces \verb back to \text to help dot2tex. Not really your business, but maybe it would make sense to use \text in the easy cases, with no \phantom's? This is quite more readable!

Cheers,

Nicolas

### comment:5 Changed 8 years ago by novoselt

I don't think \phantom{x} is likely to cause troubles, and I didn't figure out a better way to insert a "monotype width space" compatible with MathJax we use. The method in question here is fallback to when classes do not produce latex code at all, and in this case using verb is the best we can do to preserve formatting: keep line breaks and vertical alignment in case it is important. In particular cases like #13624 there has to be proper latex treatment of strings. The general case has to be uniform, I think, not like in #14256.

### comment:6 Changed 8 years ago by jhpalmieri

Can you provide examples that this is supposed to fix? Running SageTeX on a file containing

$\sage{RubiksCube()}$


doesn't work either before or after the patch.

### comment:7 Changed 8 years ago by jhpalmieri

By the way, line 337 should say MathJax, not jsMath.

### comment:8 Changed 8 years ago by jhpalmieri

Here's a LaTeX file:

\documentclass{article}
\usepackage{sagetex}
\begin{document}

Let's try this:
$\sage{"1 2"}$
Doesn't work.

\end{document}


Running latex on this, then sagetex, then latex, I get this error:

! Missing } inserted.
<inserted text>
}
l.7 \sage{"1 2"}

?


### comment:9 Changed 8 years ago by ddrake

As long as you have verbatim things, this won't work in TeX documents. The \sage macro relies on pulling in the output via a label, and verbatim things don't work well when pushed around with labels and references. (They are "fragile".)

I mentioned this on another ticket, but one problem we have is that we use the LaTeX output for two different environments: for consumption by MathJax?, and by regular LaTeX. Those environments are really pretty different.

### comment:10 Changed 8 years ago by novoselt

OK, my bad, it still does not work with SageTeX, althoug the generated code is at least valid in LaTeX for copy-pasting, while putting verb inside of phantom is not.

After playing some more, I see that MathJax that we have does support \tt, so I'll try to use that instead of verb.

### comment:11 follow-up: ↓ 14 Changed 8 years ago by jhpalmieri

I don't think that \tt will handle spaces correctly, but maybe I'm wrong.

One option we could consider: use EMBEDDED_MODE to detect whether we're using the notebook. Or we could produce output that SageTeX can handle but not MathJax, and then post-process it if using MathJax, converting the relevant string, maybe in the eval method for MathJax. We used to do this with jsMath, if I recall correctly.

### comment:12 Changed 8 years ago by novoselt

Actually one of the reasons for using verb was that strings may contain all kinds of special characters and it is tricky to display them all properly. So I think that for MathJax we still are better off using verb, but perhaps in non-embedded mode something else can be done, although special characters still have to be allowed.

A combination of tt and phantom in array can produce proper spacing, it seems, but somehow special characters have different width when escaped, e.g. #

### comment:13 Changed 8 years ago by novoselt

• Status changed from needs_review to needs_work
• Work issues set to try text/tt

Ahhh, it seems that \text{\tt ...} works quite well, I guess with \tt in math mode escaping was switching to a different font. Will try to make another patch this week.

### comment:14 in reply to: ↑ 11 Changed 8 years ago by nthiery

One option we could consider: use EMBEDDED_MODE to detect whether we're using the notebook.

I did not really think about it, but even in EMBEDDED_MODE one may want to produce latex code for something else than mathjax. So maybe that's not super robust.

### comment:15 Changed 8 years ago by novoselt

OK, how about this one: old-style verb in EMBEDDED_MODE and text-tt wrapping otherwise. RubiksCube() looks perfect when rendered through SageTeX! (I didn't change documentation/doctests yet.)

Note that both ways generate valid LaTeX code, although verb one does not work in command arguments and text-tt does not work in MathJax... Nevertheless I think it is the way to go - particular cases where this treatment is insufficient should provide adequate handling of LaTeX generation themselves.

### comment:16 Changed 8 years ago by jhpalmieri

I think Nicolas's point has some merit. Could we instead have text-tt wrapping in general, and then in the MathJax eval method, do a regular expression search-and-replace to change \text{\tt{...}} into \verb|...|, or something like that?

### comment:17 Changed 8 years ago by novoselt

• Status changed from needs_work to needs_review
• Work issues try text/tt deleted

New version uses text-tt and switches to verb in MathJax. I don't think that regular expressions can be used, since it is necessary to balance braces. There was a code in free modules removing the old verb wrappers - I switched it to using string representation in this case. If this is not what is desired, then I think no fiddling has to be done here at all, since without verb there are no issues with command arguments.

### comment:18 Changed 8 years ago by novoselt

Also, since verb is now used for MathJax only, I left \phantom{\verb which I complained about originally - it is not a valid LaTeX still, but it does give better look in MathJax - RubiksCube() looks now perfectly both in the notebook and via SageTeX.

### comment:19 Changed 8 years ago by novoselt

• Description modified (diff)

Fixed the doctest in tutorials which I missed before (by the way, the patchbot caught the problem in the English version only, not German).

Note: See TracTickets for help on using tickets.