HoldForm does not Hold Form for fractions sometimes
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.)
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}$
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.