# Replacing Tan[x] with Sin[x]/Cos[x]

Why doesn't this work?

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x]


I get back:

(* (1 - Tan[x])/(-Cos[x] + Sin[x]) *)


I also tried:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. {Tan[x] -> Sin[x]/Cos[x]}


I also tried:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> (Sin[x]/Cos[x])


And though this is an incorrect substitution, it works:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x] + Cos[x]


Update: Ok, here is what I tried using suggestions from folks below. I started with:

(1 - tan[x])/(sin[x] - cos[x]) /. tan[x] -> sin[x]/cos[x]


Which gave me:

(* (1 - sin[x]/cos[x])/(-cos[x] + sin[x]) *)


Then:

List @@ %


Which gave me:

(* {1/(-cos[x] + sin[x]), 1 - sin[x]/cos[x]} *)


Then I multiplied numerator and denominator by cos[x]:

%*{1/cos[x], cos[x]}


Which gave me:

(* {1/(cos[x] (-cos[x] + sin[x])), cos[x] (1 - sin[x]/cos[x])} *)


Then I did an expand:

% // Expand


Which gave me:

(* {1/(cos[x] (-cos[x] + sin[x])), cos[x] - sin[x]} *)


I was surprised that the denominator (first element in list) did not expand. Next:

Times @@ %


Which gave me:

(* (cos[x] - sin[x])/(cos[x] (-cos[x] + sin[x])) *)


Then:

% // Cancel


Which gave me:

(* -(1/cos[x]) *)


Then:

% /. cos[x] -> Cos[x]


Which gave me a final answer:

(* -Sec[x] *)


I actually didn't use the % sign. Rather, I used Shift-Cmd-L (Shift-Ctrl-L on Windows) to replay the output of the previous step, but thought that would crowd things a bit if I put it in here.

Interesting. How would other folks handle this process? It would be interesting to hear.

• Try Tan -> (Sin[#]/Cos[#] &). – J. M.'s discontentment Dec 13 '15 at 5:09
• I don't know if this is what you're looking for, but (1 - Tan[x])/(Sin[x] - Cos[x]) // Simplify gives -Sec[x]. – user484 Dec 13 '15 at 5:11
• @Rahul I am looking at how to apply pencil and paper type steps to simplify the expression. I am aware of the //Simplify expression. – David Dec 13 '15 at 5:13
• Your replacement is working, I think. The problem is that Mathematica automatically simplifies Sin[x]/Cos[x] to Tan[x]. If you're going to do step-by-step math, perhaps consider using undefined symbols like sin[x] and cos[x]. – march Dec 13 '15 at 5:18
• Ah, I forgot the auto-simplification! (@march, that's the answer I believe.) You'll probably want to use Activate[]/Inactivate[] here. – J. M.'s discontentment Dec 13 '15 at 5:21

Mathematica auto simplifies simple trig expressions like these, but you can turn off this setting via SystemOptions:

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


Now we see your change is left untouched without the need of HoldForm and friends.

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Sin[x]/Cos[x]

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

• Very nice. I seem to recall other questions where this option would be useful. (Just not sure how to find them.) – Michael E2 Dec 13 '15 at 10:50
• @MichaelE2 One example is: Mathematica Sec and Csc, but J.M. has already given an answer :). – Jacob Akkerboom Dec 15 '15 at 9:10

Mathematica automatically simplifies the Sin[x]/Cos[x] after ReplaceAll (/.). Defer is a useful function for preventing that:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> Defer[Sin[x]/Cos[x]]

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


HoldForm would also work:

(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> HoldForm[Sin[x]/Cos[x]]

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


Note that the head HoldForm stays in the expression (but not displayed).

InputForm[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] -> HoldForm[Sin[x]/Cos[x]]]

(* (1 - HoldForm[Sin[x]/Cos[x]])/(-Cos[x] + Sin[x]) *)

• Defer[] or HoldForm[] would be the pre-version 10 approach, so it's good to have this here. – J. M.'s discontentment Dec 13 '15 at 5:31

Mathematica automatically simplifies Sin[x]/Cos[x] to Tan[x]. Consider:

rules = Tan[x] -> Sin[x]/Cos[x]
(* Tan[x]


Using RuleDelayed doesn't help:

Tan[x] /. Tan[x] :> Sin[x]/Cos[x]
(* Tan[x] *)


Instead, since you are doing step-by-step manipulations anyway, just use non-built-in symbols:

(1 - tan[x])/(sin[x] - cos[x]) /. {tan[x] -> sin[x]/cos[x]} // Simplify
(* -(1/cos[x]) *)


Alternatively, as suggested by J.M., we can use Inactivate:

expr = Inactivate[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x], Tan | Sin | Cos] // Simplify
expr // Activate
(* -(1/Cos[x]) *)
(* -Sec[x] *)


We specify that only Tan, Sin, and Cos should be Inactivated by using the pattern Tan | Sin | Cos (Alternatives[Tan, Sin, Cos]).

• @JHM. It depends, I think. The nice thing about Inactivate here is that you can continue to use all of Mathematica's simplification functions (like Simplify) on the expression. The only thing that changes is the fact that Tan, Sin, and Cos are essentially treated as non-built-in symbols. (That said, I've never used Defer, so I don't know its power.) – march Dec 13 '15 at 5:32
• With respect to Defer[]: it renders whatever it's enclosing inert to evaluation, up until you copy and paste it into another cell. So, Defer[Sin[x]/Cos[x]] will give Sin[x]/Cos[x] as output, but if you copy this output into a new cell and evaluate, you get Tan[x]. In contrast, trying the same experiment with HoldForm[] will not give Tan[x]; you need ReleaseHold[] for that. (It admittedly is all becoming a bit confusing at this point.) – J. M.'s discontentment Dec 13 '15 at 5:40
• @march. Oh, I didn't realize that! Defer simply does not evaluate the expression altogether, so you cannot apply simplifications inside it; it may not be useful in some cases. – JungHwan Min Dec 13 '15 at 5:50