I would like to avoid multiplying explicitely by the denominator, because I'm working with huge fractions.


f[x_,y_] := Cos[x - y]/(Sin[x] Cos[y])+ 1/Sin[x];

Mathematica transforms this into Csc[x] + Cos[x - y] Csc[x] Sec[y] which I'd like to avoid: my goal is precisely to get rid of the singularities. So I used

$PrePrint = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &;

but then Together[f[x,y]] does not put f with a common denominator, and Numerator just returns f[x,y].

So how can I get the numerator of f[x,y]? It should be Cos[x - y] + Cos[y].


2 Answers 2


You can change a system setting as first pointed out by Chip:

SetSystemOptions["SimplificationOptions" -> "AutosimplifyTrigs" -> False]


expr = Cos[x-y]/(Sin[x] Cos[y]) + 1/Sin[x]
Numerator @ Together[expr]

1/Sin[x] + Cos[x - y]/(Cos[y] Sin[x])

Cos[x - y] + Cos[y]


From the documentation:

$PrePrint is applied after Out[n] is assigned, but before the output result is printed.

Try setting

$Post = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &;
Cos[x - y]/(Sin[x] Cos[y])
% // Numerator
  • $\begingroup$ This forces to print the output, which may be a nuisance if the expression takes dozens of lines. I edited the question, introducing the sum, for which the suggested solution fails. $\endgroup$
    – anderstood
    Commented Nov 2, 2016 at 18:07
  • 1
    $\begingroup$ Then instead of $Post, $PrePrint, etc. just use the appropriate transformation rule, e.g. g = # /. {Csc[z_] :> 1/Defer@Sin[z], Sec[z_] :> 1/Defer@Cos[z]} &; and f[x,y] // g // Together // Numerator $\endgroup$
    – LLlAMnYP
    Commented Nov 2, 2016 at 18:35
  • $\begingroup$ I see, thank you. A last thing: If I define h[x_,y_] = f[x,y] // g // Together // Numerator, then h[1.1,2.2] does not evaluate Cos[2.2] because of Defer. So I changed g to g = # /. {Csc[z_] :> 1/Hold@Sin[z], Sec[z_] :> 1/Hold@Cos[z]} &; and defined h[x_,y_] = f[x,y] // g // Together // Numerator // Releasehold. It's seems to work, do you see any problem with that? $\endgroup$
    – anderstood
    Commented Nov 2, 2016 at 18:52
  • $\begingroup$ @anderstood At a glance, no problem, but I guess, some testing would be needed. I found, that the original approach with $Post results in nested Times[..., Times[...]] expressions that are cleaned up only when displayed. It's hard to say, whether you'll run into more similar problems for more complicated expressions. Try it out with your actual use case and report back if you run into further problems. $\endgroup$
    – LLlAMnYP
    Commented Nov 3, 2016 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.