[sage-devel] Can LaTeX of strings be improved?

40 views
Skip to first unread message

Andrey Novoseltsev

unread,
May 22, 2010, 6:06:44 PM5/22/10
to sage-devel
Hello,

When strings or objects with only _repr_ are typeset, they are just
wrapped as a chunk of text. The main problem with it is removing line
breaks. Another one - removing indentation. For example, if "123\n 23"
is printed, we get

123
23

with 23 right under 23. The typeset version gives

123 23

Is it possible to modify this behaviour to keep line breaks and insert
some amount of space for every space character in the beginning of
lines? I understand that "perfect alignment" is pretty much
impossible, but even line breaks will make life better. It seems that
it should not be hard to do it, but I don't know much about LaTeX
framework, so I would be happy if someone else did it... Please let me
know if there are any reasons not to do this at all!

Thank you,
Andrey

--
To post to this group, send an email to sage-...@googlegroups.com
To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org
Message has been deleted

Robert Bradshaw

unread,
May 23, 2010, 12:17:10 AM5/23/10
to sage-...@googlegroups.com
On May 22, 2010, at 9:07 PM, Minh Nguyen wrote:

> Hi Andrey,
>
> On Sun, May 23, 2010 at 8:06 AM, Andrey Novoseltsev <novo...@gmail.com
> > wrote:
>> Hello,
>
> Just so that we are clear...
>
>
>> When strings or objects with only _repr_ are typeset, they are just
>> wrapped as a chunk of text. The main problem with it is removing line
>> breaks. Another one - removing indentation. For example, if "123\n
>> 23"
>> is printed, we get
>>
>> 123
>> 23
>
> ... the printed version is done handled by how __repr__ is defined to
> handle string representation of an object.

Though in Sage we use _repr_, which is used by __str__, __repr__ and
_latex_. I think this is a question about the default _latex_ which
does some postprocessing of _repr_.

sage: class A(SageObject):
....: def _repr_(self):
....: return "A\n B"
....:
sage: latex(A())
\texttt{A
B}

I'd imagine we could substituted all newlines with something
(\newline?), and leading/multiple whitespace with something (~), which
wouldn't be too hard.

- Robert

Andrey Novoseltsev

unread,
May 23, 2010, 12:27:09 AM5/23/10
to sage-devel
Hi Minh,

I probably was not very clear. I know how to change printed and
typeset version of a particular object that I create. What I don't
like is how just plain strings are typeset: if I put "123\n 23" in a
notebook cell in typeset mode and hit Shift-Enter, I will see "123 23"
in a pretty font, but on a single line. And as I understand, all
objects that don't have _latex_ will be typeset from _repr_ in the
same way - all newlines and indentations are gone. There are such
output examples in attachment to http://trac.sagemath.org/sage_trac/ticket/8694,
where I have fixed it for schemes by implementing _latex_. My point is
that if the default handling was a little bit more sophisticated, the
output of ALL objects without _latex_ would look better. And if a user
tries to output "123\n 23", then (s)he probably wants to have two
lines, no matter if the output is printed or typeset.

Hope this makes more sense ;-)
Andrey


On May 22, 10:07 pm, Minh Nguyen <nguyenmi...@gmail.com> wrote:
> Hi Andrey,
>
> On Sun, May 23, 2010 at 8:06 AM, Andrey Novoseltsev <novos...@gmail.com> wrote:
> > Hello,
>
> Just so that we are clear...
>
> > When strings or objects with only _repr_ are typeset, they are just
> > wrapped as a chunk of text. The main problem with it is removing line
> > breaks. Another one - removing indentation. For example, if "123\n 23"
> > is printed, we get
>
> > 123
> >  23
>
> ... the printed version is done handled by how __repr__ is defined to
> handle string representation of an object.
>
> > with 23 right under 23. The typeset version gives
>
> > 123 23
>
> And the LaTeX typeset version is handled by how _latex_ is defined to
> handle LaTeX typeset version of an object. But you might know this
> already.
>
> --
> Regards
> Minh Van Nguyen

Andrey Novoseltsev

unread,
May 23, 2010, 12:36:43 AM5/23/10
to sage-devel
On May 22, 10:17 pm, Robert Bradshaw <rober...@math.washington.edu>
wrote:
> Though in Sage we use _repr_, which is used by __str__, __repr__ and  
> _latex_. I think this is a question about the default _latex_ which  
> does some postprocessing of _repr_.
>
> sage: class A(SageObject):
> ....:     def _repr_(self):
> ....:         return "A\n B"
> ....:
> sage: latex(A())
> \texttt{A
>   B}
>
> I'd imagine we could substituted all newlines with something  
> (\newline?), and leading/multiple whitespace with something (~), which  
> wouldn't be too hard.
>
> - Robert

Hi Robert,

That's precisely what I am talking about!

Andrey

Rob Beezer

unread,
May 23, 2010, 12:57:11 AM5/23/10
to sage-devel
1. In the notebook (or at the command line)

latex('thing-one\nthing-two')

produces

\texttt{thing-one
thing-two}

which would be useful if a string is encountered in tex's math-mode
($..$). But the "newline" would not be recognized as such by tex.

2. In the notebook,

view('thing-one\nthing-two')

produces in the "source" for the worksheet (use the "Edit" button to
see this)

"<html><span class="math">\newcommand{\Bold}[1]{\mathbf{#1}}
\hbox{thing-one
thing-two}</span></html>"

(Outer guotes added in hopes this comes through email/web OK)

which is rendered (I believe by jsMath) due the CSS "math" class and
the rendering has no line break, presumably since jsMath is following
tex conventions s in (1).

3. In the notebook, with the "typeset" button checked,

'thing-one\nthing-two'

has *exactly* the same output as in (2).

~~~~~~~~~~

It looks to me like the JSMath class converts all instances of
\texttt{} to \hbox{} and this is part of view/pretty_print.

The \texttt{} seems to originally come from the str_function()
function.

So as Robert suggests, it might be easy to just modify
str_function() to replace "\n" by "\newline" and get what I think
Andrey is after. All this is processing is contained in sag/misc/
latex.py. John Palmieri might have some insights/ideas as well.

Andrey - is there anything else you'd want to have happen to a string?

Rob

John H Palmieri

unread,
May 23, 2010, 1:22:35 AM5/23/10
to sage-devel
On May 22, 9:57 pm, Rob Beezer <goo...@beezer.cotse.net> wrote:

> So as Robert suggests, it might be easy to just modify
> str_function()  to replace "\n" by "\newline" and get what I think

That doesn't seem to work for me.

> Andrey is after.  All this is processing is contained in sag/misc/
> latex.py.  John Palmieri might have some insights/ideas as well.

I don't know about that, but I'll try: perhaps the best thing would be
to use some sort of verbatim environment for LaTeX typesetting of
strings. My impression is that people tried this a while ago and ran
into problems -- a while ago there were comments to this effect in
misc/latex.py, but they seem to have been removed, probably by me.

Regardless of the approach, it has to work with actual LaTeX and also
with jsMath; the current plan uses \texttt in plain LaTeX and changes
this to \hbox in jsMath (because jsMath doesn't understand the command
\texttt). I think jsMath has some sort of verbatim environment, so
you could try using that instead of \hbox. Similarly, you could try
\verb... in plain LaTeX. To implement any solution, it would probably
be a good idea to search the trac server and also search sage-devel
and sage-support for comments about LaTeX typesetting of strings, to
make sure that the proposed solution deals successfully with all
previous issues. If anyone does this searching, regardless of whether
they implement any solution, it would be very helpful to record the
results so that everyone has a good list of test cases.

Also, please cc me on any trac ticket about this.

--
John

Andrey Novoseltsev

unread,
May 23, 2010, 1:25:24 AM5/23/10
to sage-...@googlegroups.com
On Sat, May 22, 2010 at 10:57 PM, Rob Beezer <goo...@beezer.cotse.net> wrote:
> Andrey - is there anything else you'd want to have happen to a string?
>
> Rob
>
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>

Hi Rob,

I will be quite happy with newlines and some kind of indentations.

It may be reasonable to also typeset such things using a fixed width
font, since this will take care of indentations in the best possible
way, but I don't know if it worth it. After all, there is always a
possibility to use "print" explicitly when one wants to see a
complicated object where they are really important (e.g. I have some
matrices with labels printed all around and aligned with columns). It
is just annoying to type "print" all the time for things like
Sequence(..., cr=True) and for my objects that have _repr_ but no
_latex_.

Thank you!
Andrey

Rob Beezer

unread,
May 23, 2010, 2:15:09 AM5/23/10
to sage-devel
On May 22, 10:22 pm, John H Palmieri <jhpalmier...@gmail.com> wrote:
> On May 22, 9:57 pm, Rob Beezer <goo...@beezer.cotse.net> wrote:
>
> > So as Robert suggests, it might be easy to just modify
> > str_function()  to replace "\n" by "\newline" and get what I think
>
> That doesn't seem to work for me.

Based on some limited testing - just editing the text version of a
worksheet - would indicate that jsMath does not want to deal with
\newline or \\ to break lines. With no better idea, I was able to use
HTML paragraph tags to get two chunks of text on different lines, of
course. Maybe a string could be chunked up based on the presence of
\n, then each handled on its own, producing HTML (as currently),
though deciding just when to wrap these with <p></p> sounds
problematic (maybe just the presence of \n would be good enough?).

Rob

Nathan O'Treally

unread,
May 23, 2010, 3:00:45 AM5/23/10
to sage-devel
On 23 Mai, 08:15, Rob Beezer <goo...@beezer.cotse.net> wrote:
> Based on some limited testing - just editing the text version of a
> worksheet - would indicate that jsMath does not want to deal with
> \newline or \\ to break lines.  With no better idea, I was able to use
> HTML paragraph tags to get two chunks of text on different lines, of
> course.  Maybe a string could be chunked up based on the presence of
> \n, then each handled on its own, producing HTML (as currently),
> though deciding just when to wrap these with <p></p> sounds
> problematic (maybe just the presence of \n would be good enough?).

I think the presence of \n indicates some ASCII art (_asciiart_()?,
_is_asciiart_()?), but I would use <br /> instead of <p>.

-Leif

ablondin

unread,
May 24, 2010, 5:17:57 PM5/24/10
to sage-devel
What about using the verbatim environment of Latex ?

class A(SageObject):

def _latex_(self):
from sage.misc.latex import LatexExpr
return '\\begin{verbatim}\n123\n 23\n\\end{verbatim}'


sage: attach latex.sage
sage: latex(A())
\begin{verbatim}
123
23
\end{verbatim}

In the notebook, it works fine for me.
Alex
> For more options, visit this group athttp://groups.google.com/group/sage-devel
> URL:http://www.sagemath.org

John H Palmieri

unread,
May 24, 2010, 6:01:55 PM5/24/10
to sage-devel
On May 24, 2:17 pm, ablondin <alexandre.blondin.ma...@gmail.com>
wrote:
> What about using the verbatim environment of Latex ?
>
> class A(SageObject):
>
>     def _latex_(self):
>         from sage.misc.latex import LatexExpr
>         return '\\begin{verbatim}\n123\n 23\n\\end{verbatim}'
>
> sage: attach latex.sage
> sage: latex(A())
> \begin{verbatim}
> 123
>  23
> \end{verbatim}
>
> In the notebook, it works fine for me.

From the notebook, try executing

view(A())

I bet you'll see a box saying

Unknown environment "verbatim"

This is because jsMath doesn't know about the verbatim environment.
The extension

<http://www.math.union.edu/~dpvc/jsMath/authors/verb.html>

might be an option here, but that doesn't seem to deal with newlines
very well.

--
John

ablondin

unread,
May 25, 2010, 12:11:10 AM5/25/10
to sage-devel
You're right ! It doesn't work with the view() function. On the other
hand, when I select latex instead of sage and I enter the command

\sage{A()}

it displays 123 on the first line and 23 on the second line, with the
2's aligned properly, i.e. it handles the newlines and indentation as
desired. That said, I guess it's not a clean solution...

Alex
> For more options, visit this group athttp://groups.google.com/group/sage-devel
> URL:http://www.sagemath.org

John H Palmieri

unread,
May 25, 2010, 4:00:36 PM5/25/10
to sage-devel
On May 24, 9:11 pm, ablondin <alexandre.blondin.ma...@gmail.com>
wrote:

> You're right ! It doesn't work with the view() function. On the other
> hand, when I select latex instead of sage and I enter the command
>
> \sage{A()}
>
> it displays 123 on the first line and 23 on the second line, with the
> 2's aligned properly, i.e. it handles the newlines and indentation as
> desired. That said, I guess it's not a clean solution...

Another not clean solution: typeset strings with

\begin{verbatim}
string
\end{verbatim}

and then put "\\begin{verbatim}" in the jsmath_avoid list. This
results in inconsistent behavior: jsMath output appears in blue, while
LaTeX output appears in black.

Another option: use the \verb option for jsMath, but break the string
at newlines and typeset each line individually, inserting line breaks
explicitly in the html produced by the JSMath class in sage/misc/
latex.py.

--
John

ross kyprianou

unread,
May 25, 2010, 6:11:27 PM5/25/10
to sage-...@googlegroups.com
I couldnt see a solution to this in this thread so heres a related question

The following produces the exact output Id like to produce (to assign to the
_repr_ property of a class)

%latex
N(\mu,\sigma^2)


How can I set things up to do this in a (_repr_) function (as a sage
statement/function call)?
Does a function exist so I call it to produce such an output?

I was hoping something like this would work

from sage.misc.latex import pretty_print
pretty_print_default(True)
pretty_print('N('+'\mu'+','+'\sigma^2'+')')

but all it displays is

N(\mu,\sigma^2)

(hope I didnt miss something too obvious - thanks)

Reply all
Reply to author
Forward
0 new messages