## HoldForm does not Hold Form for fractions sometimes

10

3

I'm facing a strange behavior of HoldForm.

I need to display 1/2*3/4 in LaTeX like this : $$\frac{1}{2} \times \frac{3}{4}$$

So I use Mathematica : 1/2* 3/4 // HoldForm // TeXForm BUT I get $$\frac{3}{2\ 4}$$

First the writing 2 space 4 is ambigous and second it does not hold form at all :(

Can you help me ? Thank you ! (happy Holidays)

EDIT : I would need an automatic transformation of any input to correct TeX or an automatic correction of any output to correct TeX.

1

– Karsten 7. – 2014-12-25T17:57:54.750

2Another way: HoldForm[Divide[1, 2] Divide[3, 4]] // TeXForm – Fred Simons – 2014-12-25T19:53:03.047

10

Use HoldForm applied to each fraction to keep the fractions from combining.

HoldForm[1/2] HoldForm[3/4]


to produce $$\frac{1}{2} \frac{3}{4}$$

or

HoldForm[(1/2) (3/4)]


to produce $$\frac{3}{2 \times 4}$$

Using TeXForm produces the desired LaTex code.

(HoldForm[1/2] HoldForm[3/4]) // TeXForm
(* \frac{1}{2} \frac{3}{4} *)


Simpler is

Infix[f[1/2, 3/4], "\[Times]"] // TeXForm
(* \frac{1}{2}\times \frac{3}{4} *)


which also provides the times sign. $$\frac{1}{2}\times \frac{3}{4}$$

z1 z2 /. Times -> Cross /. {z1 -> 1/2, z2 -> 3/4} // TeXForm


also produces the desired output. (This is based on the third Answer to 39061.)

How can I automatically apply this to my input 1/2*3/4 ? – Crypto – 2014-12-26T13:32:10.923

@Crypto, that depends on the form of your input. If you literally want to change 1/2*3/4 to the form in my last equation, doing so is easy. However, I would guess that you want to do something more complicated. Please give an actual example, if possible, if your input. – bbgodfrey – 2014-12-26T13:59:32.340

I mean, I am looking for a way to get the more accurate TeX translation of my input.

I can make any transformation before or after, apply any Mathematica function or any post processing. Currently, I get the TeX Output and add a \times where there is a space. So the current output is $$\frac{3}{2 \times 4}$$ I would like any other transformation to be more accurate. How to convert HoldForm[1/2*3/4] to HoldForm[1/2] HoldForm[3/4] in an aotomated way for example. – Crypto – 2014-12-26T14:08:43.727

@Crypto, please provide example, so I know what you have in mind. For instance, is it just the product of two fractions? – bbgodfrey – 2014-12-26T14:11:58.990

I have seen this kind of behaviour only with fractions (but maybe there are others). So first, I would need a trick to bypass this fraction behaviour automatically. (Maybe there is nothing simple) – Crypto – 2014-12-26T14:21:50.517

@Crypto, there are many ways to address this issue, depending on how the fractions appear in your input. For instance, if your input contains, for instance, z1 z2, which you later replace by fractions, then the second addendum (which I shall add in a few minutes) works well, I believe. – bbgodfrey – 2014-12-26T15:24:07.963

4

The behavior you observe is due to the formatting rules associated with Times. Please start by reading my answer here: Returning an unevaluated expression with values substituted in. We can apply a similar technique here though the result is not quite as desired if we merely block Times during Box creation. We get:

$\left(1*\frac{1}{2}\right)*\left(3*\frac{1}{4}\right)$

This form is due to the internal format of 1/2 and 3/4:

Hold[1/2, 3/4] // FullForm

Hold[Times[1, Power[2, -1]], Times[3, Power[4, -1]]]


One way to handle this is to post-process the Box form yield the format we desire:

SetAttributes[hf, HoldAll]

MakeBoxes[hf[args__], fmt_] :=
Block[{Times}, MakeBoxes[HoldForm[args], fmt]] /.
RowBox[{"(", RowBox[{n_, "*", FractionBox["1", d_]}], ")"}] :> FractionBox[n, d]


Now using hf in place of HoldForm:

hf[1/2*3/4] // TeXForm

\frac{1}{2}*\frac{3}{4}


Formatted:

$\frac{1}{2}*\frac{3}{4}$

Rather very complicated oO, but it could be an option ! I need an automated method to deal with MM input or TeX output in order to convert it into correct TeX. – Crypto – 2014-12-26T13:49:07.833

@Crypto does using hf in place of HoldForm not work for you? If it fails please give me an example so that I can try to improve either my code or my recommendation. – Mr.Wizard – 2014-12-26T22:09:06.657

1Your function seems to correct one type problem with fraction. But I am more looking for something able to display TeX in the exact form I write them. Probably MM is not the right tool to use. I am disapointed. – Crypto – 2014-12-28T19:09:05.137

@Crypto please see the second answer I just added. If again this doesn't work for you please give an example where it fails. – Mr.Wizard – 2014-12-28T23:29:50.243

3

I am posting a second answer because I am now taking a very different interpretation of your problem. In a comment below my first answer you state:

Your function seems to correct one type problem with fraction. But I am more looking for something able to display TeX in the exact form I write them. Probably MM is not the right tool to use. I am disapointed.

I assumed that you were looking for TeX conversion of arbitrary expressions generated by (evaluation in) Mathematica but if instead you simply want TeX for expressions in "the exact form I write them" you may be able to use Strings, e.g.:

The string was created using standard input methods. \[Times] was entered with Esc*Esc.

Here is the input in copyable form:

"\!$$\*FractionBox[\(1$$, $$2$$]\)\[Times]\!$$\*FractionBox[\(3$$, $$4$$]\)" // TeXForm


And the output formatted by MathJax:

$\frac{1}{2}\times \frac{3}{4}$

Critically this method avoids interpretation of your raw input into e.g. Times and Power, thereby bypassing those "pretty printing" rules that were changing your expression in an unwanted way.

Thank You for this second method. – Crypto – 2015-01-02T14:56:02.237

2

This unexpected behaviour of HoldForm and Hold seems to be due to the function MakeExpression and not a bug in HoldForm or Hold.

Having entered

Hold[1/2 3/4]


the frontend sends the command

MakeExpression[BoxData[RowBox[{"Hold","[",RowBox[{RowBox[{"1","/","2"}],
RowBox[{"3","/","4"}]}],"]"}]],StandardForm]


to the kernel for further evaluation. The essential part is

MakeExpression[BoxData[RowBox[{RowBox[{"a","/","b"}],
RowBox[{"c","/","d"}]}]],StandardForm]

(* HoldComplete[(a c)/(b d)] *)


So already in MakeExpression the numerators and denominaters are collected to one numerator and denominator, before Hold or HoldForm is used.