Message Boards Message Boards

1 Reply
1 Total Likes
View groups...
Share this post:

Notebook files not being translated when inserted into a new notebook.

I have .nb files saved on my Desktop on a MAC Powerbook G4 with OS10.5.8. When I double click on the icon the files are inserted into a new window correctly. But when I use <<file.nb to insert the file into an already created window, the file is not translated. Only the lines seen with the command "more file.nb" are displayed.  Why is this so, and is there a way to insert such files into an existing window translated correctly?
POSTED BY: Stanton Peale
Although I am not exactly sure what you are trying to do, let me offer an interpretation and then a solution to that.  

You have an open notebook in Mathematica and you are wanting to insert into that notebook the contents of another notebook that you have saved on disk.  And you want to do this via a command that you execute in the original open Mathematica notebook. 

The command <<, is a shorthand for the function Get.  Here is its documentation

This function is used to import Mathematica code from a text file (typically from a .m file but it  can be a notebook which is a .nb file, and the latter is the case you appear to be interested in), to execute all of that code, and then to return the final value that is the result of executing the code in that file.  In the case where the file is a notebook, the code is in fact the internal code from the .nb file that defines the notebook and so the expression that is returned is the .nb's Notebook[...] expression.  The cells in that expression are in the first argument of that Notebook expression.  (See for documentation on the Notebook function.)

To insert the contents of a .nb file programatically in to an already open notebook then requires Getting the notebook and then printing out the cells that are in it.  Here is an example of a set of commands (embodied in a function) that could take care of that.

InsertNotebookHere[file_String] :=
Module[{nb, notebook, cells},
  nb = EvaluationNotebook[];
  notebook = Get[file];
  cells = Flatten[{First[notebook]}];
  SelectionMove[nb, After, Cell];
  NotebookWrite[nb, cells]

Much has been left out of this (such as checking if the file actually exists on disk and so on).  But it seems to do the trick.

There is of course the option to open the other file, select all of its cells, copy them, and then past them into the target notebook.  

Another option is to use the menu command "File..."  which is found under the "Insert" menu.  If you select the desired .nb file on disk using the dialog that opens when you use this menu command, then its cells will be inserted at the location in your target notebook where your cursor is currently located,  It is likely that this menu command uses code similar to that which is above (at least conceptually). 

I hope that this helps.
POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract