This question applies to any package, but I encountered this problem while working with graphs.
There are symbols in the Combinatorica package (such as Graph, IncidenceMatrix, EdgeStyle, and others) that have the same name as analogous symbols in System. If I execute Needs[Combinatorica`], then I can access Combinatorica`Graph by the name Graph, but if I want to access System`Graph, I have to write System`Graph.

I want to use the Combinatorica` prefix to access all the symbols in Combinatorica`, and I want to access System` symbols without using a prefix. And I don't want to have the symbols Graph, IncidenceMatrix, and so on, in red in Mathematica because of the naming conflict.

Is there a way to use the Combinatorica` package without introducing naming conflicts?

3 Answers
3

Shadowing occurs only when there are two functions with the same name that are in $ContextPath. So right after you do <<Combinatorica`, do the following:

$ContextPath = Rest@$ContextPath;

What this does is that it removes Combinatorica (which is the package you just loaded). Now the only Graph function that's on the path is System`Graph and you can call it simply by the name, without the prefix. To access any functions from the package, use the prefix, as Combinatorica`Graph.

If Combinatorica` was loaded a while ago and you have loaded other packages in between, Rest@... is not going to be helpful. In that case, use:

The answer of @R.M. already explains the essence of the problem. You can streamline the process of removing the Combinatorica from the $ContextPath by loading it via

Block[{$ContextPath}, Needs["Combinatorica`"]]

(or use Get intead of Needs, although Needs is a preferred way to load a package). In this way, you don't have to do anything afterwards, since, once the scope of Block is left, the $ContextPath automatically is reset to its previous value.

This is a generally useful trick to load packages without adding them to the $ContextPath. I found it quite useful on many occasions.

One way is to use something like Graph=System`Graph; and use Graph which refers to System`Graph thereafter. You can use any other name such as g=System`Graph as well. The downside is that you have to do it for any function.

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.