Question: I wrote this sentence which is too long with "but"

> 
> To illustrate this, suppose we want to eliminate the navigation bar from the page sidebar. We may create a rule in which the link density is employed to distinguish such a part, **but** when this rule is applied to the page, all such elements are removed regardless of their locations in the page, even if they are within the main content (e.g. Table of Contents)
> 
> 
> 

can I write it with "however"? I just feel **but** in this sentence makes more direct contrast. 

Are there cases in which we'd better use "but" even in formal sentences?

Answer: 
*But* and *however*, though they both imply a contrast between what goes before and what comes after, focus the relationship very differently.

* *but* itself takes a low emphasis: it is unstressed in speech, and in writing accommodates low-disjunction pointing—it may follow a mere comma or in some circumstances no point at all. Even in high-disjunction contexts it throws the emphasis on the following proposition rather than on the contrast between the two propositions. *But* is typically used to introduce a clause which qualifies, mitigates or dismisses the previous clause.
* *however* at the beginning of a clause takes high emphasis. In speech it takes a primary stress, which is usually not matched until considerably later in the following clause. In writing it must be preceded by high-disjunction pointing—a semicolon, dash or full stop. It emphasizes the contrast between the two propositions it lies between, and typically signals that your discourse is going in a new direction.

This emphasis is somewhat neutralized when *however* appears as a parenthetical at some point after the beginning of the clause. It usually appears as a 'drop-off' after the clause's first primary stress and marks the stressed element as the source or occasion of contrast.

In your sentence, then, the use of *but* or *however* will depend on how you regard the contrast between the propositions to either side. ~~Since we don't know what point you are illustrating, or how the contrast within your example bears on it, it's impossible to say which you should use.~~

ADDED:
Your comment adds that "when the rule is applied, unexpectedly, the user see many other elements are also erased from the page." This situation, where your key point is the unexpected difference between what was intended and what actually happened, seems to me to be tailor-made for *however*; and I'd rewrite a little to make the contrast even clearer:

> 
> To illustrate this, suppose we want to eliminate the navigation bar from the page sidebar. We create a rule in which the link density is employed to distinguish that piece. That will certainly remove the navigation bar; **however**, when the rule is applied to the entire page **all** such elements will be removed, regardless of location on the page—even elements (for example, a Table of Contents) within the main content.
> 
> 
> 

The pieces in boldface are those which receive primary stress in speech.
In technical and scientific writing, prefer '*however*' over '*but*'. Their meaning is the same, and yet the reading seems smoother with the former, while the latter creates a harder point.

The use of '*but*' is more suited for idioms like "*all **but** one*".