# HoldForm is not working as I would expect

Why HoldForm do this?

HoldForm[a/b \[Integral]f[x] \[DifferentialD]x]


Output is this ugly looking formula: $$\frac{a \int f(x) \, dx}{b}$$

I want the output to look exactly same as I typed it: $$\frac{a}{b} \int f(x) \, dx$$

What is the purpose of HoldForm when it does not hold the form of expression as it has been typed?

It always rearranges fractions from $$\frac{a}{b} c$$ to $$\frac{a c}{b}$$.

(The basic idea is from this answer by xzczd.)

Try this:

MakeBoxes[Times[a_, b_], StandardForm] := RowBox[{MakeBoxes@a, MakeBoxes@b}]
MakeBoxes[Times[a_, Power[b_, -1]], StandardForm] := MakeBoxes[Divide[a, b]]


Now

Hold[a/b  \[Integral]f[x] \[DifferentialD]x]


• While this will not preserve the input box structure exactly, it seems to be surprisingly robust and is very simple. Nice! May 8 at 13:19
• @LukasLang Yes, technically this method doesn't do what was requested in the question: it does not hold the form of expression "as it has been typed". It just produces the formatting the OP wished to get. May 8 at 13:39
• @LukasLang The only simple way to get exactly the form in which the expression was typed is to take the box form of the expression directly from the cell when the expression was typed. Then wrap the extracted boxes with RawBoxes for producing the output. May 8 at 13:45

### Why it happens

When you enter an expression and evaluate it in Mathematica, MakeExpression is used to convert the box expression (i.e. the description of the exact input you entererd) into an expression. For example, when you enter f[a] into a Notebook, what Mathematica sees is RowBox[{"f","[","a","]"}] (you can use Cell > Show expression to see this). Only after MakeExpression is applied, is it actually converted to f[a].

Similar conversions happen for all other types of valid input. In particular, this process removes certain kinds of information from the input. For example, a+b, a+(b), Plus[a,b] are all converted to a+b. At this point, it is too late to tell how a+b was entered. Something similar is happening in your case, only that Times and Plus have some additional simplification rules that are applied during these early phases. To prevent this from happening, we have to interfere with this process at or before the MakeExpression step, see below for an example of how to do that.

### Workaround

Here's a solution similar to what the Notation  paclet does:

HoldBoxesTemplate[t_] :=
TemplateBox[{t}, "HoldBoxes", DisplayFunction -> (# &)]
MakeExpression[TemplateBox[{b_}, "HoldBoxes", ___], frm_] :=
HoldComplete@HoldBoxes[b, frm]
MakeBoxes[HoldBoxes[b_, _], _] ^:=
HoldBoxesTemplate[b]
Normal@HoldBoxes[b_, frm_] ^:=
ReleaseHold@MakeExpression[b, frm]
AppendTo[CurrentValue[EvaluationNotebook[], InputAliases],
"hb" -> HoldBoxesTemplate["\[Placeholder]"]];


You can now press EschbEsc to insert a placeholder that will keep its contents in exactly the same shape:

The trick is to have an invisible TemplateBox around your expression that ensures the box structure is preserved through MakeExpression, rather than being converted into a normal expression (at which point there expression has already been "canonicalized", so that it's no longer possible to reconstruct the original shape). The expression returned is of the form HoldBoxes[...]:

• Interesting but rather complicated. If nobody comes with a simpler method I will accept your answer. May 8 at 13:14

It is just enough to wrap a/b. Try this:

Have fun!

• What is the point of your answer? If I have complex expression that contains several compound fractions I will not rewrite it with lots of HoldForm inside in appropriate positions. For such a manual work I would use rather Latex than Mathematica. HoldForm` is simply not doing what it claims it should do. And that is the problem, not that I would not know how to do it manually. May 8 at 11:52
• @azerbajdzan What is the point of your comment? 1. Mma is just constructed in such a way as it is. It is not arranged to return results in a beautiful form. It is not a Latex. 2. If you have a "complex expression that contains several compound fractions " you should write this directly in your question. I must not guess what exactly do you have in mind. May 8 at 17:15