Skip to content

Conversation

@oscargus
Copy link
Contributor

@oscargus oscargus commented Feb 8, 2019

References to other Issues or PRs

Inspired by #3001

Brief description of what is fixed or changed

A new function multiline_latex is added to more easily split multiline equations.

It is simply based on the number of addition/subtraction terms and one can limit the number of terms per line.

Both align* and eqnarray can be used. I was thinking of adding IEEEeqnarray which is better, but then it may not be that regularly used and I couldn't really figure out parameter settings etc to make sense.

Other comments

Release Notes

  • printing
    • New function multiline_latex which simplifies generating code for long expressions

@sympy-bot
Copy link

sympy-bot commented Feb 8, 2019

Hi, I am the SymPy bot (v147). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • printing
    • New function multiline_latex which simplifies generating code for long expressions (#15944 by @oscargus)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.5.

Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it.

Click here to see the pull request description that was parsed.

<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234". See
https://github.com/blog/1506-closing-issues-via-pull-requests . Please also
write a comment on that issue linking back to this pull request once it is
open. -->
Inspired by #3001

#### Brief description of what is fixed or changed
A new function `multiline_latex` is added to more easily split multiline equations.

It is simply based on the number of addition/subtraction terms and one can limit the number of terms per line.

Both `align*` and `eqnarray` can be used. I was thinking of adding `IEEEeqnarray` which is better, but then it may not be that regularly used and I couldn't really figure out parameter settings etc to make sense.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below. See
https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
on how to write release notes. The bot will check your release notes
automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* printing
   * New function `multiline_latex` which simplifies generating code for long expressions
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@jksuom
Copy link
Member

jksuom commented Feb 8, 2019

I would prefer having use_eqnarray False by default. It seems that the amstex environments produce better spacing.

\begin{align*}
  x^2+y^2 &= z^2 \\
  x^3+y^3 &< z^3
\end{align*}

\begin{eqnarray}
  x^2+y^2 &=& z^2 \\
  x^3+y^3 &<& z^3
\end{eqnarray}

@jksuom
Copy link
Member

jksuom commented Feb 8, 2019

I finally succeeded in uploading an image.

image

@oscargus
Copy link
Contributor Author

oscargus commented Feb 8, 2019

I agree with your conclusions in your example. I used beamer and there the conclusion was the opposite:
image

Now, admittedly, that is not a good reference. I'll try out a few more realistic ones and get back, but that was really the reason I even started to look at something different than align* (which was used in the original code).

@oscarbenjamin
Copy link
Collaborator

I thought eqnarray was considered obsolete: https://tex.stackexchange.com/questions/196/eqnarray-vs-align

@oscargus
Copy link
Contributor Author

I wasn't aware that eqnarray has been that deprecated recently...

Anyway, I changed it to align* as default and also added IEEEeqnarray as an option.

environment : "string", optional
Which LaTeX wnvironment to use for the output. Options are "align*"
(default), "eqnarray", and "IEEEeqnarray". Note the only the first
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shuold be "that" instead of "the". Will fix in the next commit, but waiting for other review comments before running a new test...

terms_per_line : integer, optional
Number of terms per line to print. Default is 1.
environment : "string", optional
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gah! Should be string, not "string". As below.


# Based on code from https://github.com/sympy/sympy/issues/3001
l = LatexPrinter(**settings)
if environment.lower().startswith('e'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about if environment == 'eqnarray' (same below)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that, but then one need to type it correctly and considering that the alternatives are eqnarray, IEEEeqnarray, and array* it made sense (to me), to have a more "sloppy" checking of alternatives so that array and ieeeeqnarray also works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clearly, I mean align*, not array*

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the advantage of sloppy checking. It's better to list the possibilities in the docstring then someone can get it right first time they write their code. Allowing anything that starts with e here leads to a backward compatibility problem if you ever decide to add another environment that begins with e.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! Should it be case sensitive or not?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go case-sensitive. I can see that you have already listed the 3 possibilities in the docstring so I'd go with that. Also these are the names of LaTeX environments which are case-sensitive.

nonumber = r'\nonumber'
end_term = '\n\\end{IEEEeqnarray}'
doubleet = True
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the else could be an error after all correct cases are handled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that may be an idea.

term = terms[i]
term_start = ''
term_end = r''
sign = r'+'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why the above are raw strings (no big deal...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably copy-paste or that they initially included things where it was needed... Makes sense to remove it.

\end{eqnarray}
Using "IEEEeqnarray":
\begin{IEEEeqnarray}{rCl}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a missing print here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thanks!

Copy link
Collaborator

@oscarbenjamin oscarbenjamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a couple of comments. Your own comments also seemed reasonable

@oscargus
Copy link
Contributor Author

Thanks for the review @oscarbenjamin ! I'll update it eventually.

@oscargus
Copy link
Contributor Author

Comments fixed (apart from the sloppy environment detection).

& & - \cos{\left(\log{\left(y \right)} \right)}
\end{eqnarray}
Using "IEEEeqnarray":
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be double quotes or backticks like above?

def multiline_latex(lhs, rhs, terms_per_line=1, environment="align", use_dots=False, **settings):
r"""
This function generates a LaTeX equation with a multiline right-hand side
in either an ``eqnarray`` or ``align*`` environment.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Below you list 3 environments including IEEEeqnarray but here only the other two.

Copy link
Collaborator

@oscarbenjamin oscarbenjamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left comments but otherwise good.

@oscargus
Copy link
Contributor Author

Good comments! Will fix.

@oscargus
Copy link
Contributor Author

I have merged with current master (fixes done since long, but didn't comment on that).

@oscarbenjamin
Copy link
Collaborator

+1 to merge if you're finished with it.

@oscargus
Copy link
Contributor Author

oscargus commented May 2, 2019

OK! I merge it then!

@oscargus oscargus merged commit 9013298 into sympy:master May 2, 2019
@oscargus oscargus deleted the termsperline branch May 2, 2019 08:00
@moorepants
Copy link
Member

Is there a way to enable this with init_printing()?

@moorepants
Copy link
Member

I tried init_printing(latex_printer=multiline_latex) naively, but that fails with:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/miniconda/envs/multibody-book/lib/python3.10/site-packages/IPython/core/formatters.py:343, in BaseFormatter.__call__(self, obj)
    341     method = get_real_method(obj, self.print_method)
    342     if method is not None:
--> 343         return method()
    344     return None
    345 else:

File ~/miniconda/envs/multibody-book/lib/python3.10/site-packages/sympy/interactive/printing.py:226, in _init_ipython_printing.<locals>._print_latex_text(o)
    222 """
    223 A function to generate the latex representation of sympy expressions.
    224 """
    225 if _can_print(o):
--> 226     s = latex(o, mode=latex_mode, **settings)
    227     if latex_mode == 'plain':
    228         return '$\\displaystyle %s$' % s

TypeError: multiline_latex() missing 1 required positional argument: 'rhs'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants