3
$\begingroup$

This question already has an answer here:

I want Mathemtica to stop manipulating my polynomials! I mean, I want the output of

Print[3 x + 5 + x^2]

to be just $3x+5+x^2$, not $5+3x+x^2$ as Mathematica returns.

How can I do this?

$\endgroup$

marked as duplicate by Mr.Wizard Jul 4 '14 at 0:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Maybe wrapping it in Defer[] will do what you want? (I guess it will depend on how you want to use the expression.) $\endgroup$ – Daniel Lichtblau Jul 3 '14 at 22:05
  • 2
    $\begingroup$ @Behzad You can't both have and not have evaluation ... What is your use case for this exactly? Understand that will help come up with a solution. Generally, consider everything you enter into Mathematica as program code, not a mathematical expression. The way Mathematica works implies that this transformation will take place and it's not a good idea to try to change that. What I suggest instead is to have two steps: 1) one processing step where you manipulate symbolic expressions, letting Mathematica work the way it does 2) and a formatting step where you print them to specification. $\endgroup$ – Szabolcs Jul 3 '14 at 22:17
  • 3
    $\begingroup$ Specifically for your example, With[{a=3}, Print@HoldForm[a x - 5 + x^2]] does work, but I don't know if it's general enough for your use case. $\endgroup$ – Szabolcs Jul 3 '14 at 22:19
  • 1
    $\begingroup$ Related: 9570, 20714 $\endgroup$ – Michael E2 Jul 4 '14 at 0:17
  • 1
    $\begingroup$ Also related: (6358), (7880), (15744), (21519), (22431), (30216), (31974), (40165), (46535) $\endgroup$ – Mr.Wizard Jul 4 '14 at 0:27
7
$\begingroup$

Will

a = -3; Print[Defer[\[FormalA] x + 5 + x^2] /. \[FormalA] -> a]

-3 x + 5 + x^2

work for you?

$\endgroup$
  • $\begingroup$ it may not work for 1 or 0. but don't know what's OP need. $\endgroup$ – Kuba Jul 4 '14 at 0:05
4
$\begingroup$

What about

Print[HoldForm[3 x + 5 + x^2]]

?

$\endgroup$
  • $\begingroup$ Well, if we have parameters, there will be another problem: commands a=-3; Print[HoldForm[ax-5+x^2]] yield $ax-5+x^2$, not $-3x-5+x^2$. $\endgroup$ – Behzad Jul 3 '14 at 22:13
  • 1
    $\begingroup$ @Behzad There will always be a problem unless you post full context :) this answer fits the question. You can use With to inject in held expressions but I suppose it may still be a problem with sign marks. With[{a = -3}, HoldForm[a x - 5 + x^2]] $\endgroup$ – Kuba Jul 3 '14 at 22:19
  • $\begingroup$ @Kuba Interesting question though, because fun = 3 x + 5 + x^2; and then Print[HoldForm@fun] doesn't work. $\endgroup$ – eldo Jul 3 '14 at 22:26
  • $\begingroup$ @eldo With Set it is already lost so even if you inject it it will be formated :) but with SetDelayed you can do: ClearAll[fun]; fun := 3 x + 5 + x^2; and then HoldForm[fun] /. OwnValues@fun $\endgroup$ – Kuba Jul 3 '14 at 22:30
  • $\begingroup$ @kuba Brilliant ! If OP wants this, he could even append a // Print after /. OwnValues@fun. $\endgroup$ – eldo Jul 3 '14 at 22:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.