# How to display complex numbers with real part before imaginary part?

Also I would like to have latex(I) output \mathbf{i} instead of just i. Is there a way to do so?

How to display complex numbers with real part before imaginary part?

Also I would like to have latex(I) output \mathbf{i} instead of just i. Is there a way to do so?

2

One way to obtain `\mathbf{i}`

instead of just `i`

is to use a string replacement.

To also get the real part before the imaginary part, why not use a little helper function.

The following might do what you want.

```
def latex_of_complex(z):
r"""
Return a LaTeX string for this complex number.
EXAMPLE::
sage: latex_of_complex(0)
0
sage: latex_of_complex(2)
2
sage: latex_of_complex(4*i)
4 \mathbf{i}
sage: latex_of_complex(7 + 8*i)
7 + 8 \mathbf{i}
"""
if z == 0:
return LatexExpr('0')
a = z.real()
b = z.imag()
if b == 0:
return latex(a)
s = (latex(a) + ' + ') if a else ''
return LatexExpr(s) + latex(b * i).replace('i', r'\mathbf{i}')
```

Now use `latex_of_complex`

instead of `latex`

to latexify complex numbers
with the requested customization.

1

Thanks. It does what I want! Perhaps, sage should have a built-in function for that task. The codes above typeset a-bi as a+ -bi. but just a minor modification fixes that:

```
def cpx(z):
if z == 0:
return LatexExpr('0')
a = z.real()
b = z.imag()
if (a == 0) or (b==0) :
return LatexExpr(latex(z).replace('i',r'\mathbf{i}'))
elif b > 0:
s = '+'
else:
s = '-'
return latex(a) + LatexExpr(s) + latex(abs(b) * i).replace('i', r'\mathbf{i}')
```

1

Well after using the code a couple days, I modified it again and here is a better version. E.g. now 2+(1-sqrt(3))i is typeset as shown v.s. as 2+i+sqrt(3)i in the previous version.

```
def cpx(z):
r"""
Return a LaTeX string for this complex number.
EXAMPLE::
sage: latex_of_complex(0)
0
sage: latex_of_complex(2)
2
sage: latex_of_complex(4*i)
4 \mathbf{i}
sage: latex_of_complex(7 + 8*i)
7 + 8 \mathbf{i}
"""
if z == 0:
return LatexExpr('0')
re = z.real().simplify()
im = z.imag().simplify()
if (re == 0) or (im==0) :
return LatexExpr(latex(z).replace('i',r'\mathbf{i}'))
elif im > 0:
s = '+'
else:
s = '-'
rel = latex(re)
iml = latex(abs(im))
if rel.find('+') >= 1 or rel.find('-') >=1:
repl = r'\left('+rel+r'\right)'
else:
repl = rel
if iml.find('+') >=1 or iml.find('-') >=1:
impl = r'\left('+iml+r'\right)'+r'\mathbf{i}'
else:
impl = iml+r'\mathbf{i}'
if abs(im) ==1:
return LatexExpr(repl + s + r'\mathbf{i}')
return LatexExpr(repl + s + impl)
```

Good exploration of this pretty-printing exercise.

In my experience, pretty-printing always takes a while to get right.

Hints:

- add the new examples to the function's documentation
- update the function's documentation to use the new name
- try more examples and make more changes if needed

For instance, the following examples might suggest some changes:

```
sage: aa = [0, 1]
sage: bb = [0, 1, -1, 2, -2, 1 - sqrt(3), sqrt(3) - 1, 1 + sqrt(3), -1 - sqrt(3)]
sage: for a in aa:
....: for b in bb:
....: print('{:20} {:50}'.format(str(a + b * I), cpx(a + b * I)))
```

2

I don't know about redefining, but you can do something like this:

```
bI = var('bI', latex_name='\mathbf{i}')
var('a,b')
assume(a, 'real')
assume(b, 'real')
expr = (a + b*I)^3
show(expr.real_part() + bI*expr.imag_part())
```

Note that getting real and imaginary parts expands the expression, so this may be not what you want.

Thanks, Well, what I want is to make some homework and use sage (sagetex) to do the computations. Now I realize a bigger problem is that sage displays imaginary part in front of the real part. E.g. latex(7+8i) returns 8i + 7. I understand this is a different question but can I get some help here?

Asked: **
2019-01-04 09:39:35 -0600
**

Seen: **564 times**

Last updated: **Dec 22 '20**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.

Some things have changed in Sage since that question was asked and answered in 2019-01.

The same question was asked again in 2020-12, and answered taking these changes into account:

Ask Sage question 54821: How to arrange real and imaginary parts of a complex number