2
$\begingroup$

I can't get any function defined in $UserBaseDirectory/Kernel/init.m to work. For example:

SinDeg[d_] := Sin[d * Degree]; (* Only line in init.m *)

Output:

SinDeg[90]
(* => SinDeg[90] *)

However when copying the code and executing it inside a Mathematica notebook, it works fine:

SinDeg[d_] := Sin[d * Degree];
SinDeg[90]
(* => 1 *)

The Mathematica front-end can definitely see the function definitions as it displays SinDeg as an autocompletion suggestion, when typing SinD.

I'm using Mathematica 9 Home Edition on OS X.

$\endgroup$
22
  • $\begingroup$ To eliminate the obvious: you have restarted Mathematica after adding this line to init.m? $\endgroup$ Mar 11, 2013 at 23:50
  • $\begingroup$ @SjoerdC.deVries yes. $\endgroup$
    – Tyilo
    Mar 11, 2013 at 23:51
  • $\begingroup$ What do you get when you type ?SinDeg directly after Mathematica starts up? $\endgroup$ Mar 12, 2013 at 0:42
  • $\begingroup$ @SjoerdC.deVries I get this error: Information::notfound: Symbol SinDeg not found. >> $\endgroup$
    – Tyilo
    Mar 12, 2013 at 0:44
  • $\begingroup$ Could you check whether you really have an underscore in your definition in the init.m file? $\endgroup$ Mar 12, 2013 at 0:44

1 Answer 1

5
$\begingroup$

This has to do with the Notebook's default context setting in the evaluation menu. If it isn't set to Global`, the definitions made in init.m are not seen.

As rm-rf says, a good way to put custom definitions in the init.m would be to use Begin and End to create an Init` context and append that context to the context path so that the definitions are available regardless of the notebook's context. (better still, use packages)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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