latex matrix printing

9 views
Skip to first unread message

Ryan Krauss

unread,
Jun 24, 2009, 2:37:12 PM6/24/09
to sympy-...@googlegroups.com
I have added a feature to allow latex users to specify the matrix type and delimitter in _print_Matrix.  It is in this branch:
git://github.com/ryanGT/sympy.git

The approach works for me, but it is no longer as clean to switch from inline=True to inline=False.  These options still work, but inline=False still uses smallmatrix and \\left( and \\right) by default.  I don't have a good idea how to switch the defaults when inline=False and no other options are passed in.

In [1]: import sympy

In [2]: sympy.var('x y')
Out[2]: (x, y)

In [4]: M = sympy.Matrix([[1+x, y],[y, x-1]])

In [5]: M
Out[5]:
[1 + x,      y]
[    y, -1 + x]

In [6]: sympy.latex(M)
Out[6]: '$\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 + x\\end{smallmatrix}\\right)$'

In [7]: sympy.latex(M, inline=False)
Out[7]: '\\begin{equation*}\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 + x\\end{smallmatrix}\\right)\\end{equation*}'

In [8]: sympy.latex(M, inline=False, mat_str='Bmatrix')
Out[8]: '\\begin{equation*}\\left(\\begin{Bmatrix}1 + x & y\\\\y & -1 + x\\end{Bmatrix}\\right)\\end{equation*}'

In [9]: sympy.latex(M, inline=False, mat_str='Bmatrix', mat_delim=None)
Out[9]: '\\begin{equation*}\\begin{Bmatrix}1 + x & y\\\\y & -1 + x\\end{Bmatrix}\\end{equation*}'

So, you really need to use a profile with inline=False.

Any ideas how to clean this up?

Ryan

Ondrej Certik

unread,
Jun 24, 2009, 3:15:20 PM6/24/09
to sympy-...@googlegroups.com

Nice patches. So your question is how to make it work with
inline=True? Or is your quesiton just how to polish the patches? I
think the patches are ok.

Ondrej

Ryan Krauss

unread,
Jun 24, 2009, 3:24:16 PM6/24/09
to sympy-...@googlegroups.com
They work fine.  The issue is that I think the defaults should be different with inline=False.  I don't think that this

'\\begin{equation*}\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 +
  x\\end{smallmatrix}\\right)\\end{equation*}'
is useful Latex.  I would not typically want to use smallmatrix if I am not using inline.

Simliarly, I think that
'\\begin{equation*}\\left(\\
begin{Bmatrix}1 + x & y\\\\y & -1 + x\\end{Bmatrix}\\right)\\end{equation*}'

will produce a matrix with paranthesis and square brackets.  So, if you use inline=False, with my patch you would also need to use mat_str=Bmatrix and mat_delim=None.  Since you can create one profile and pass it in, I am ok with it.  But previous users of inline=False might consider this a step backwards. 

So, ideally, I think there should be a way to have inline=False also change the defaults for mat_str and mat_delim.

Does that make sense?


Ryan

Ondrej Certik

unread,
Jun 24, 2009, 3:25:42 PM6/24/09
to sympy-...@googlegroups.com
On Wed, Jun 24, 2009 at 1:24 PM, Ryan Krauss<ryan...@gmail.com> wrote:
> They work fine.  The issue is that I think the defaults should be different
> with inline=False.  I don't think that this
> '\\begin{equation*}\\left(\\begin{smallmatrix}1 + x & y\\\\y & -1 +
>   x\\end{smallmatrix}\\right)\\end{equation*}'
> is useful Latex.  I would not typically want to use smallmatrix if I am not
> using inline.
>
> Simliarly, I think that
> '\\begin{equation*}\\left(\\
> begin{Bmatrix}1 + x & y\\\\y & -1 + x\\end{Bmatrix}\\right)\\end{equation*}'
>
> will produce a matrix with paranthesis and square brackets.  So, if you use
> inline=False, with my patch you would also need to use mat_str=Bmatrix and
> mat_delim=None.  Since you can create one profile and pass it in, I am ok
> with it.  But previous users of inline=False might consider this a step
> backwards.
>
> So, ideally, I think there should be a way to have inline=False also change
> the defaults for mat_str and mat_delim.
>
> Does that make sense?

Yes --- so just change the defaults I guess. E.g. at the place where
defaults are set, do it differently. Or is there any other problem?

Ondrej

Ryan Krauss

unread,
Jun 24, 2009, 5:21:23 PM6/24/09
to sympy-...@googlegroups.com
I think I have a plan.  Basically, I want two sets of defaults, one for inline=True and one for inline=False.  I think I can handle it without making it too messy.

Standby for updated branch push.

Ryan Krauss

unread,
Jun 24, 2009, 9:57:07 PM6/24/09
to sympy-...@googlegroups.com
OK, so I think my github branch latex_matrix contains a decent solution.  Basically, in the LatexPrinter __init__ method, if the incoming profile is not None and it has inline=False, then I modify the default _settings before updating from the profile.  This allows different default values for mat_str and mat_delim depending on whether or not inline=True.

It is in the latex_matix branch of
git://github.com/ryanGT/sympy.git

Should I also create an issue and attach this branch to it?

Ryan

Vinzent Steinberg

unread,
Jun 25, 2009, 12:41:46 PM6/25/09
to sympy-...@googlegroups.com
2009/6/25 Ryan Krauss <ryan...@gmail.com>

OK, so I think my github branch latex_matrix contains a decent solution.  Basically, in the LatexPrinter __init__ method, if the incoming profile is not None and it has inline=False, then I modify the default _settings before updating from the profile.  This allows different default values for mat_str and mat_delim depending on whether or not inline=True.

It is in the latex_matix branch of
Should I also create an issue and attach this branch to it?

Only if you fear that it could be forgotten. :)

Vinzent

Ondrej Certik

unread,
Jun 25, 2009, 12:50:21 PM6/25/09
to sympy-...@googlegroups.com

It's a good idea always. With so many patches going on right now, it's
easy to forget. I have just added you to the google code project, so
set the issue to NeedsReview.

Ondrej

Reply all
Reply to author
Forward
0 new messages