Sage: Ticket #14382: Fix LaTeXing of strings
https://trac.sagemath.org/ticket/14382
<p>
<a class="closed ticket" href="https://trac.sagemath.org/ticket/11498" title="enhancement: Improve LaTeXing of strings (closed: fixed)">#11498</a> made "ASCII-art" strings look reasonably well when displayed via LaTeX methods. One of the changes was damaged in <a class="closed ticket" href="https://trac.sagemath.org/ticket/9774" title="enhancement: Use MathJax instead of jsMath (closed: fixed)">#9774</a>, resulting in invalid LaTeX code that works in <a class="missing wiki">MathJax?</a>, but not in SageTeX. Let's fix it again.
</p>
<p>
Apply <a class="attachment" href="https://trac.sagemath.org/attachment/ticket/14382/trac_14382-texttt.patch" title="Attachment 'trac_14382-texttt.patch' in Ticket #14382">trac_14382-texttt.patch</a><a class="trac-rawlink" href="https://trac.sagemath.org/raw-attachment/ticket/14382/trac_14382-texttt.patch" title="Download"></a>.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/14382
Trac 1.1.6ddrakeFri, 29 Mar 2013 17:11:34 GMTcc set
https://trac.sagemath.org/ticket/14382#comment:1
https://trac.sagemath.org/ticket/14382#comment:1
<ul>
<li><strong>cc</strong>
<em>ddrake</em> added
</li>
</ul>
TicketjhpalmieriFri, 29 Mar 2013 17:18:39 GMTcc changed
https://trac.sagemath.org/ticket/14382#comment:2
https://trac.sagemath.org/ticket/14382#comment:2
<ul>
<li><strong>cc</strong>
<em>jhpalmieri</em> added
</li>
</ul>
TicketnovoseltFri, 29 Mar 2013 17:30:16 GMTcc, status changed; keywords, author set
https://trac.sagemath.org/ticket/14382#comment:3
https://trac.sagemath.org/ticket/14382#comment:3
<ul>
<li><strong>cc</strong>
<em>cremona</em> added
</li>
<li><strong>status</strong>
changed from <em>new</em> to <em>needs_review</em>
</li>
<li><strong>keywords</strong>
<em>latex</em> added
</li>
<li><strong>author</strong>
set to <em>Andrey Novoseltsev</em>
</li>
</ul>
TicketnthieryTue, 02 Apr 2013 02:38:48 GMT
https://trac.sagemath.org/ticket/14382#comment:4
https://trac.sagemath.org/ticket/14382#comment:4
<p>
You might want to have a look at <a class="closed ticket" href="https://trac.sagemath.org/ticket/13624" title="defect: KeyError raised by GraphLatex.dot2tex_picture when edge_labels=True (closed: fixed)">#13624</a>; 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!
</p>
<p>
Cheers,
</p>
<blockquote>
<p>
Nicolas
</p>
</blockquote>
TicketnovoseltTue, 02 Apr 2013 02:55:43 GMT
https://trac.sagemath.org/ticket/14382#comment:5
https://trac.sagemath.org/ticket/14382#comment:5
<p>
I don't think <code>\phantom{x}</code> 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 <a class="closed ticket" href="https://trac.sagemath.org/ticket/13624" title="defect: KeyError raised by GraphLatex.dot2tex_picture when edge_labels=True (closed: fixed)">#13624</a> there has to be proper latex treatment of strings. The general case has to be uniform, I think, not like in <a class="closed ticket" href="https://trac.sagemath.org/ticket/14256" title="enhancement: Nicer latex output for strings using \text (closed: duplicate)">#14256</a>.
</p>
TicketjhpalmieriTue, 02 Apr 2013 21:25:00 GMT
https://trac.sagemath.org/ticket/14382#comment:6
https://trac.sagemath.org/ticket/14382#comment:6
<p>
Can you provide examples that this is supposed to fix? Running SageTeX on a file containing
</p>
<pre class="wiki">\[
\sage{RubiksCube()}
\]
</pre><p>
doesn't work either before or after the patch.
</p>
TicketjhpalmieriTue, 02 Apr 2013 21:25:40 GMT
https://trac.sagemath.org/ticket/14382#comment:7
https://trac.sagemath.org/ticket/14382#comment:7
<p>
By the way, line 337 should say <code>MathJax</code>, not <code>jsMath</code>.
</p>
TicketjhpalmieriTue, 02 Apr 2013 21:30:24 GMT
https://trac.sagemath.org/ticket/14382#comment:8
https://trac.sagemath.org/ticket/14382#comment:8
<p>
Here's a LaTeX file:
</p>
<pre class="wiki">\documentclass{article}
\usepackage{sagetex}
\begin{document}
Let's try this:
\[
\sage{"1 2"}
\]
Doesn't work.
\end{document}
</pre><p>
Running latex on this, then sagetex, then latex, I get this error:
</p>
<pre class="wiki">! Missing } inserted.
<inserted text>
}
l.7 \sage{"1 2"}
?
</pre>
TicketddrakeTue, 02 Apr 2013 22:04:03 GMT
https://trac.sagemath.org/ticket/14382#comment:9
https://trac.sagemath.org/ticket/14382#comment:9
<p>
As long as you have verbatim things, this won't work in TeX documents. The <code>\sage</code> 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".)
</p>
<p>
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 <a class="missing wiki">MathJax?</a>, and by regular LaTeX. Those environments are really pretty different.
</p>
TicketnovoseltWed, 03 Apr 2013 02:09:17 GMT
https://trac.sagemath.org/ticket/14382#comment:10
https://trac.sagemath.org/ticket/14382#comment:10
<p>
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.
</p>
<p>
After playing some more, I see that MathJax that we have does support <code>\tt</code>, so I'll try to use that instead of verb.
</p>
TicketjhpalmieriWed, 03 Apr 2013 02:40:43 GMT
https://trac.sagemath.org/ticket/14382#comment:11
https://trac.sagemath.org/ticket/14382#comment:11
<p>
I don't think that <code>\tt</code> will handle spaces correctly, but maybe I'm wrong.
</p>
<p>
One option we could consider: use <code>EMBEDDED_MODE</code> 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 <code>eval</code> method for <code>MathJax</code>. We used to do this with <code>jsMath</code>, if I recall correctly.
</p>
TicketnovoseltWed, 03 Apr 2013 02:47:43 GMT
https://trac.sagemath.org/ticket/14382#comment:12
https://trac.sagemath.org/ticket/14382#comment:12
<p>
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.
</p>
<p>
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. #
</p>
TicketnovoseltWed, 03 Apr 2013 02:53:31 GMTstatus changed; work_issues set
https://trac.sagemath.org/ticket/14382#comment:13
https://trac.sagemath.org/ticket/14382#comment:13
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
<li><strong>work_issues</strong>
set to <em>try text/tt</em>
</li>
</ul>
<p>
Ahhh, it seems that <code>\text{\tt ...}</code> works quite well, I guess with <code>\tt</code> in math mode escaping was switching to a different font. Will try to make another patch this week.
</p>
TicketnthieryWed, 03 Apr 2013 03:04:45 GMT
https://trac.sagemath.org/ticket/14382#comment:14
https://trac.sagemath.org/ticket/14382#comment:14
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/14382#comment:11" title="Comment 11">jhpalmieri</a>:
</p>
<blockquote class="citation">
<p>
One option we could consider: use <code>EMBEDDED_MODE</code> to detect whether we're using the notebook.
</p>
</blockquote>
<p>
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.
</p>
TicketnovoseltThu, 04 Apr 2013 04:49:25 GMT
https://trac.sagemath.org/ticket/14382#comment:15
https://trac.sagemath.org/ticket/14382#comment:15
<p>
OK, how about this one: old-style verb in EMBEDDED_MODE and text-tt wrapping otherwise. <code>RubiksCube()</code> looks perfect when rendered through SageTeX! (I didn't change documentation/doctests yet.)
</p>
<p>
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.
</p>
TicketjhpalmieriThu, 04 Apr 2013 05:22:24 GMT
https://trac.sagemath.org/ticket/14382#comment:16
https://trac.sagemath.org/ticket/14382#comment:16
<p>
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 <code>\text{\tt{...}}</code> into <code>\verb|...|</code>, or something like that?
</p>
TicketnovoseltWed, 17 Apr 2013 19:40:32 GMTstatus changed; work_issues deleted
https://trac.sagemath.org/ticket/14382#comment:17
https://trac.sagemath.org/ticket/14382#comment:17
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>needs_review</em>
</li>
<li><strong>work_issues</strong>
<em>try text/tt</em> deleted
</li>
</ul>
<p>
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.
</p>
TicketnovoseltWed, 17 Apr 2013 19:49:37 GMT
https://trac.sagemath.org/ticket/14382#comment:18
https://trac.sagemath.org/ticket/14382#comment:18
<p>
Also, since verb is now used for MathJax only, I left <code>\phantom{\verb</code> which I complained about originally - it is not a valid LaTeX still, but it does give better look in MathJax - <code>RubiksCube()</code> looks now perfectly both in the notebook and via SageTeX.
</p>
TicketnovoseltThu, 16 May 2013 19:14:38 GMTattachment set
https://trac.sagemath.org/ticket/14382
https://trac.sagemath.org/ticket/14382
<ul>
<li><strong>attachment</strong>
set to <em>trac_14382_doctests.2.patch</em>
</li>
</ul>
TicketnovoseltThu, 16 May 2013 19:15:13 GMTattachment set
https://trac.sagemath.org/ticket/14382
https://trac.sagemath.org/ticket/14382
<ul>
<li><strong>attachment</strong>
set to <em>trac_14382_doctests.patch</em>
</li>
</ul>
TicketnovoseltThu, 16 May 2013 19:18:04 GMTdescription changed
https://trac.sagemath.org/ticket/14382#comment:19
https://trac.sagemath.org/ticket/14382#comment:19
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/14382?action=diff&version=19">diff</a>)
</li>
</ul>
<p>
Fixed the doctest in tutorials which I missed before (by the way, the patchbot caught the problem in the English version only, not German).
</p>
TicketnovoseltThu, 16 May 2013 20:14:58 GMT
https://trac.sagemath.org/ticket/14382#comment:20
https://trac.sagemath.org/ticket/14382#comment:20
<p>
Apply trac_14382_fix_latex_of_strings.patch trac_14382_doctests.patch
</p>
TicketnovoseltFri, 17 May 2013 20:56:23 GMT
https://trac.sagemath.org/ticket/14382#comment:21
https://trac.sagemath.org/ticket/14382#comment:21
<p>
Things can be simplified after switching to MathJax 2.2:
</p>
<ul><li><a class="ext-link" href="https://github.com/mathjax/MathJax/issues/380"><span class="icon"></span>https://github.com/mathjax/MathJax/issues/380</a>
</li><li><a class="ext-link" href="https://github.com/mathjax/MathJax/issues/381"><span class="icon"></span>https://github.com/mathjax/MathJax/issues/381</a>
</li></ul>
TicketnovoseltMon, 17 Jun 2013 23:52:45 GMT
https://trac.sagemath.org/ticket/14382#comment:22
https://trac.sagemath.org/ticket/14382#comment:22
<p>
Spaces in verb are handled correctly with 2.2 in SVG mode, but are still quite off in HTML-CSS, so I think that patches here are still the way to go!
</p>
TicketjhpalmieriFri, 21 Jun 2013 21:12:33 GMT
https://trac.sagemath.org/ticket/14382#comment:23
https://trac.sagemath.org/ticket/14382#comment:23
<p>
With this patch, running <code>view(RubiksCube())</code> from the command line produces output in which the numbers are lined up, but the horizontal lines are too short: LaTeX is turning each pair of dashes <code>--</code> into an "en-dash". If you want to use this approach, maybe you need to use <code>{-}{-}</code> instead of <code>--</code>.
</p>
<p>
From the notebook, I don't see anything useful, but that may be specific to this machine. I'll try it on another machine eventually.
</p>
<p>
From SageTeX, it works, but it requires the package <code>amsmath</code> because of the <code>\text{...}</code> command. I'm not sure that's ideal.
</p>
TicketnovoseltFri, 21 Jun 2013 21:52:46 GMTstatus changed; work_issues set
https://trac.sagemath.org/ticket/14382#comment:24
https://trac.sagemath.org/ticket/14382#comment:24
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
<li><strong>work_issues</strong>
set to <em>dashes and commanline view</em>
</li>
</ul>
<p>
Interesting, was working fine for me except for the command-line which I didn't think about. Will keep working.
</p>
TicketnovoseltFri, 21 Jun 2013 22:13:38 GMT
https://trac.sagemath.org/ticket/14382#comment:25
https://trac.sagemath.org/ticket/14382#comment:25
<p>
Does anyone recall any reason to use <code>\text{\tt }</code> rather than <code>\texttt{ }</code>? The latter should be eventually supported by MathJax (i.e. they plan to do it) while the former is a more general problem of not supporting nested font switches or something like that. Directly from LaTeX it seems to be identical.
</p>
<p>
I am also puzzled by combining of dashes - it does not happen for hand-written code or via !SageTeX, why does it happen for <code>view</code>???
</p>
TicketjhpalmieriFri, 21 Jun 2013 22:28:56 GMT
https://trac.sagemath.org/ticket/14382#comment:26
https://trac.sagemath.org/ticket/14382#comment:26
<p>
Probably use <code>\mathtt</code> instead of <code>\texttt</code>, since LaTeX output in Sage should be in math mode. Otherwise, I don't know why use <code>\text{\tt }</code> instead of <code>\mathtt</code>. I don't understand the dash issue, either.
</p>
TicketnovoseltFri, 21 Jun 2013 22:38:04 GMT
https://trac.sagemath.org/ticket/14382#comment:27
https://trac.sagemath.org/ticket/14382#comment:27
<p>
<code>\mathtt</code> is not a monospaced font. I can't see the difference between <code>\text{\tt }</code> and <code>\texttt{ }</code>, but the latter works without amsmath. On the other hand, without amsmath it does not scale properly, so amsmath is still desirable. I don't really see the problem with it - hard to imagine that someone will use Sage for typesetting math and yet object to loading a standard math package.
</p>
TicketjhpalmieriFri, 21 Jun 2013 22:41:46 GMT
https://trac.sagemath.org/ticket/14382#comment:28
https://trac.sagemath.org/ticket/14382#comment:28
<p>
You shouldn't use <code>\texttt{...}</code> within math mode. It might work, but if so, it's taking advantage of a bug.
</p>
TicketnovoseltFri, 21 Jun 2013 22:44:44 GMT
https://trac.sagemath.org/ticket/14382#comment:29
https://trac.sagemath.org/ticket/14382#comment:29
<p>
OK, then it seems to me that missing dashes for the command line are the only issue!
</p>
TicketnovoseltFri, 21 Jun 2013 23:08:33 GMTattachment set
https://trac.sagemath.org/ticket/14382
https://trac.sagemath.org/ticket/14382
<ul>
<li><strong>attachment</strong>
set to <em>trac_14382_fix_latex_of_strings.patch</em>
</li>
</ul>
<p>
Use text-tt
</p>
TicketnovoseltFri, 21 Jun 2013 23:09:57 GMTstatus changed; work_issues deleted
https://trac.sagemath.org/ticket/14382#comment:30
https://trac.sagemath.org/ticket/14382#comment:30
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>needs_review</em>
</li>
<li><strong>work_issues</strong>
<em>dashes and commanline view</em> deleted
</li>
</ul>
<p>
Didn't run tests yet, but they should not be affected by this change, hopefully.
</p>
TicketnovoseltSun, 30 Jun 2013 19:37:54 GMT
https://trac.sagemath.org/ticket/14382#comment:31
https://trac.sagemath.org/ticket/14382#comment:31
<p>
All tests pass both for me and the patchbot.
</p>
<p>
John, what exactly was "From the notebook, I don't see anything useful"? Was it indeed something with a single machine or some generic issue? I've had these patches on for a while and didn't experience any problems under Windows and Linux.
</p>
TicketjhpalmieriFri, 26 Jul 2013 04:54:52 GMTattachment set
https://trac.sagemath.org/ticket/14382
https://trac.sagemath.org/ticket/14382
<ul>
<li><strong>attachment</strong>
set to <em>trac_14382-texttt.patch</em>
</li>
</ul>
<p>
use \text{\texttt{...}}
</p>
TicketjhpalmieriFri, 26 Jul 2013 04:55:24 GMTattachment set
https://trac.sagemath.org/ticket/14382
https://trac.sagemath.org/ticket/14382
<ul>
<li><strong>attachment</strong>
set to <em>trac_14382-delta.patch</em>
</li>
</ul>
<p>
for review only: diff between previous patches and texttt patch
</p>
TicketjhpalmieriFri, 26 Jul 2013 04:55:57 GMTdescription changed
https://trac.sagemath.org/ticket/14382#comment:32
https://trac.sagemath.org/ticket/14382#comment:32
<ul>
<li><strong>description</strong>
modified (<a href="/ticket/14382?action=diff&version=32">diff</a>)
</li>
</ul>
<p>
Looks okay except that <code>\tt</code> <a class="ext-link" href="http://www.ctan.org/tex-archive/info/l2tabu/"><span class="icon"></span>is an obsolete command</a>, and <code>\texttt</code> should be used instead. I've attached a new patch which does this, along with a <code>delta</code> patch which shows the difference between my patch and your two patches. All tests pass for me and the output looks the same as with <code>\tt</code>.
</p>
TicketnovoseltFri, 26 Jul 2013 06:15:01 GMTstatus changed; reviewer set
https://trac.sagemath.org/ticket/14382#comment:33
https://trac.sagemath.org/ticket/14382#comment:33
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
<li><strong>reviewer</strong>
set to <em>John Palmieri</em>
</li>
</ul>
<p>
Wow, I thought LaTeX is once and for all and only new things can appear without obsolescence! Will keep it in mind for the future, changes are fine with me!
</p>
TicketjdemeyerMon, 29 Jul 2013 11:07:11 GMTmilestone changed
https://trac.sagemath.org/ticket/14382#comment:34
https://trac.sagemath.org/ticket/14382#comment:34
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
TicketnovoseltMon, 19 Aug 2013 18:37:09 GMT
https://trac.sagemath.org/ticket/14382#comment:35
https://trac.sagemath.org/ticket/14382#comment:35
<p>
Apply trac_14382-texttt.patch
</p>
TicketjdemeyerTue, 20 Aug 2013 12:55:54 GMTstatus changed; resolution, merged set
https://trac.sagemath.org/ticket/14382#comment:36
https://trac.sagemath.org/ticket/14382#comment:36
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>fixed</em>
</li>
<li><strong>merged</strong>
set to <em>sage-5.12.beta3</em>
</li>
</ul>
Ticket