- // Separator is "+", except if the following expression would have a leading minus sign
- it++;
- if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
- os << "+";
- }
-
- if (!overall_coeff.is_equal(_ex0())) {
- if (overall_coeff.info(info_flags::positive)) os << '+';
- overall_coeff.bp->printcsrc(os,type,precedence);
+ numeric coeff;
+ bool first = true;
+
+ // First print the overall numeric coefficient, if present
+ if (!overall_coeff.is_zero()) {
+ if (!is_a<print_tree>(c))
+ overall_coeff.print(c, 0);
+ else
+ overall_coeff.print(c, precedence());
+ first = false;
+ }
+
+ // Then proceed with the remaining factors
+ epvector::const_iterator it = seq.begin(), itend = seq.end();
+ while (it != itend) {
+ coeff = ex_to<numeric>(it->coeff);
+ if (!first) {
+ if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
+ } else {
+ if (coeff.csgn() == -1) c.s << '-';
+ first = false;
+ }
+ if (!coeff.is_equal(_num1) &&
+ !coeff.is_equal(_num_1)) {
+ if (coeff.is_rational()) {
+ if (coeff.is_negative())
+ (-coeff).print(c);
+ else
+ coeff.print(c);
+ } else {
+ if (coeff.csgn() == -1)
+ (-coeff).print(c, precedence());
+ else
+ coeff.print(c, precedence());
+ }
+ if (is_a<print_latex>(c))
+ c.s << ' ';
+ else
+ c.s << '*';
+ }
+ it->rest.print(c, precedence());
+ ++it;
+ }
+
+ if (precedence() <= level) {
+ if (is_a<print_latex>(c))
+ c.s << ")}";
+ else
+ c.s << ")";
+ }