10
$\begingroup$

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.

$\endgroup$
2
  • 1
    $\begingroup$ Related: Multiplication sign in TeXForm? $\endgroup$ Commented Dec 25, 2014 at 17:57
  • 2
    $\begingroup$ Another way: HoldForm[Divide[1, 2] Divide[3, 4]] // TeXForm $\endgroup$ Commented Dec 25, 2014 at 19:53

4 Answers 4

10
$\begingroup$

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} *) 

Addendum

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}$$

Second Addendum

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.)

$\endgroup$
6
  • $\begingroup$ How can I automatically apply this to my input 1/2*3/4 ? $\endgroup$ Commented Dec 26, 2014 at 13:32
  • $\begingroup$ @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. $\endgroup$ Commented Dec 26, 2014 at 13:59
  • $\begingroup$ 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. $\endgroup$ Commented Dec 26, 2014 at 14:08
  • $\begingroup$ @Crypto, please provide example, so I know what you have in mind. For instance, is it just the product of two fractions? $\endgroup$ Commented Dec 26, 2014 at 14:11
  • $\begingroup$ 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) $\endgroup$ Commented Dec 26, 2014 at 14:21
4
$\begingroup$

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}$

$\endgroup$
4
  • $\begingroup$ 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. $\endgroup$ Commented Dec 26, 2014 at 13:49
  • $\begingroup$ @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. $\endgroup$ Commented Dec 26, 2014 at 22:09
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Dec 28, 2014 at 19:09
  • $\begingroup$ @Crypto please see the second answer I just added. If again this doesn't work for you please give an example where it fails. $\endgroup$ Commented Dec 28, 2014 at 23:29
3
$\begingroup$

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.:

enter image description here

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.

$\endgroup$
1
  • $\begingroup$ Thank You for this second method. $\endgroup$ Commented Jan 2, 2015 at 14:56
2
$\begingroup$

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.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.