Using the approach in TeXForm and large brackets (\Biggl[ etc):
$funcsPat = "sin" | "cos" | "tan" | "csc" | "sec" | "cot" | "log";
mytexrules = {
RowBox[{f : $funcsPat, "(", arg_, ")"}] /;
! MatchQ[arg (* exceptions to no parentheses *)
, RowBox[{_, "+" | "-", __}] | (* Plus *)
(_ /; LeafCount[arg] > 20) | (* complicated arg *)
(_ /; Count[arg, _FractionBox, Infinity] > 1) (* complicated arg *)
] :> Convert`TeX`BoxesToTeX[RowBox[{f, arg}]]
};
TeXForm[1 + x](*initializes System`Convert`TeXFormDump`$GreekWords*)
If[MatchQ[oldGreekWords, _List],(* reset $GreekWords, if necessary *)
System`Convert`TeXFormDump`$GreekWords = oldGreekWords];
If[! MatchQ[oldGreekWords, _List],
oldGreekWords = System`Convert`TeXFormDump`$GreekWords];
If[MatchQ[oldGreekWords, _List],
System`Convert`TeXFormDump`$GreekWords = Join[mytexrules, oldGreekWords],
"Warning: System`Convert`TeXFormDump`$GreekWords not initialized"];
Examples
As Nasser intimates, parsing traditional parenthesization, especially for functions and multiplication, is not easy and is different from the standard grammars found in computer languages. The language of math is probably evolving toward parenthesizing function-argument combination, probably because of the influence of calculators in school. At least I notice more mathematicians and students always using parentheses in lower-level undergraduate courses. (Nonetheless, people are still adhering to $\sin^2 x$ instead using the more straightforward $(\sin x)^2$.) The code below tries to adhere to the older tradition of omitting parentheses when unnecessary. "Unnecessary" always has involved a judgment call and does not follow strict rules. Of course, the code above implements a strict rule. One general rule is that arguments that are sums or differences should be enclosed in parentheses. Products need not be parenthesized, but it may be helpful to do so if the argument is too complicated (another judgment call).
Sin[x] // TeXForm
(* \sin x *)
Sin[x y] // TeXForm
(* \sin x y *)
1 + Sin[(x + y)/2] // HoldForm // TeXForm
(* 1+\sin \frac{x+y}{2} *)
Sin[x^2 y (x + y)/2] // TeXForm
(* \sin\left(\frac{1}{2} x^2 y (x+y)\right) *)
Sin[(x/2 + y)/2] // TeXForm
(* \sin\left(\frac{1}{2} \left(\frac{x}{2}+y\right)\right) *)
When a constant or variable is added to a function, I prefer to put the term in front of the function. Mathematica prefers to sort the terms into the standard ordering in TraditionalForm
. The use of HoldForm
above is a way to hold the ordering as typed.
TeXForm
first? How can I convert Sin[2x] from Mathematica to \sin 2x in LaTeX If you doTeXForm[Sin[2 x]]
then you will get what you want. I am not sure still what is it you are asking here. $\endgroup$\sin (2 x)
. Can I get\sin 2x
? $\endgroup$Sin[x y]
? do you want this to show as $\sin xy$ ? This is ambiguous. Someone can read this as $\sin(x) y$ instead of $\sin(xy)$. Explicit is always better than implicit. (except in some finite difference schemes) $\endgroup$