47

Is there a way to open one notebook in two (or more) different windows? This can be helpful when working with a long notebook and one wants to edit one part of it while looking at a different part. Otherwise, you have to jump back and forth...

Edit: Per @Guillochon comment, splitting the window where the notebook is open is indeed a nice option.

Edit 2: Having a secondary "read-only" window can also be a nice compromise.

Ajasja
  • 13,634
  • 2
  • 46
  • 104
Dror
  • 1,841
  • 17
  • 19
  • 3
    I like this question. I would expect the behavior to mirror (clone) the Notebook, such that a change in one is made to the other; optionally only one would be directly editable. I don't know how to do this but I'd like to find out. – Mr.Wizard Aug 10 '12 at 08:10
  • 3
    Isn't @Dror just asking for a split-screen window? Almost all modern text editors have that feature... – Guillochon Aug 10 '12 at 08:20
  • @Guillochon Yes, but the mathematica interface is not a modern text editor:) All modern text editors have tab-indent as well, but try doing that in the notebook interface. I guess using Workbench is always an option, although I never got used to it... – Ajasja Aug 10 '12 at 09:23
  • 4
    I asked this split screen question to Wolfram in 1994! He tried and convince me I didn't need it. – chris Aug 10 '12 at 09:28
  • Deleting my comments, since they don't apply to a window split. – Dr. belisarius Aug 10 '12 at 12:24
  • @chris And what arguments did Wolfram give you? – a06e Nov 27 '13 at 13:50
  • 1
    Tell us what you want and we ll explain to you why you don't need it! :-) The answer bellow works fine for me though. – chris Nov 28 '13 at 10:44
  • If anyone is still interested, I gave an answer for copies where either are editable here. – obsolesced Jul 15 '16 at 14:22

1 Answers1

35

Here's a rather unsophisticated approach. The code below creates a toolbar in the current notebook, with buttons to create, refresh and close a replica notebook. The replica is given a gray background and cannot be edited or evaluated.

I tried to do something with Dynamic to automatically refresh the replica notebook, but without success.

Update

Here is a neater implementation. The code below creates a palette (which can be installed using the menu: Palettes/Install Palette) containing a single button to create a duplicate of the active notebook. The update button for the duplicate is now located in the duplicate itself, leaving the original notebook unchanged. The duplicate is closed using the normal window close button, but it has "ClosingSaveDialog" set to False so you won't get a save prompt.

CreatePalette[Button["Duplicate Active Notebook",
   NotebookPut[NotebookGet[InputNotebook[]] /. 
   {Rule[DockedCells, _] :> Sequence[], 
    Rule[WindowMargins, _] :> Rule[WindowMargins, {{0, Automatic}, {0, Automatic}}], 
    Cell[x___] :> Cell[x, Evaluatable -> False]}, 
    Background -> GrayLevel[0.95], Editable -> False, "ClosingSaveDialog" -> False, 
    DockedCells -> With[{sourcenb = InputNotebook[]}, 
      Cell[BoxData[ToBoxes[Button["Update", 
          SelectionMove[InputNotebook[], All, Notebook]; 
          NotebookWrite[InputNotebook[], 
           NotebookGet[sourcenb] /. Cell[x___] :> Cell[x, Evaluatable -> False]]]]],
       "DockedCell", CellContext -> Cell]],
    WindowTitle -> "Duplicate of " <> AbsoluteOptions[InputNotebook[], WindowTitle][[1, 2]]];
   SetSelectedNotebook[InputNotebook[]]], WindowTitle -> "Duplicate"];
Simon Woods
  • 84,945
  • 8
  • 175
  • 324
  • +1 Great idea. I did notice that syntax highlighting doesn't work, which is a big minus. – Ajasja Aug 10 '12 at 13:58
  • @Ajasja: That's not quite right; There is some degree of highlighting. For example internal variables of modules - do get colored in green. – Dror Aug 10 '12 at 14:41
  • @SimonWoods: The Close toolbar doesn't work for me - I get SetOptions::optnf: DockedCells is not a known option for thisnb. >> error. Any idea? – Dror Aug 10 '12 at 14:44
  • @Dror Well most of the built in functions are not highlighted. example, which makes the code really hard to read. This is probably realted to the Evaluator -> None option. Setting it to Evaluator -> "Local" fixes the problem (but not in a general way since local may not exist) – Ajasja Aug 10 '12 at 14:47
  • @Ajasja, I've replaced the Evaluator->None option with explicit Evaluator->False options in each cell, so the syntax highlighting should now work. – Simon Woods Aug 10 '12 at 15:26
  • @Dror, I've replaced thisnb with explicit references to EvaluationNotebook[] so hopefully that error has gone away. – Simon Woods Aug 10 '12 at 15:27
  • Instead of Dynamic, you could set up the "Refresh replica" function to trigger on specific keystrokes or mouse clicks using NotebookEventActions. – dws Aug 12 '12 at 02:58
  • @dws, the way I've done it, the replica notebook will scroll to the bottom every time it is refreshed, so I thought it should only happen when explicity requested. There's also a performance issue - I copy the entire notebook contents on refresh so it could be a slow process for large notebooks. – Simon Woods Aug 12 '12 at 17:01
  • @SimonWoods Is there a way to make your hack into a global one? That is, I want to be able to evaluate duplicate[] for instance in a notebook which doesn't contain your code snippet. Is it possible? – Dror Aug 21 '12 at 08:47
  • 1
    @Dror, see update. – Simon Woods Aug 21 '12 at 21:56
  • @SimonWoods: That looks great! I failed to install it as a palette. I tried both using the "Install Palette" menu and manually copying the notebook where I saved the snippet to ~/Library/Mathematica/SystemFiles/FrontEnd/Palettes- I managed to have a new palette item, but when clicking it, it simply opens the notebook. Can you please add specific instructions how to install it? – Dror Aug 22 '12 at 07:04
  • 3
    @Dror, It sounds like you have installed the notebook containing the CreatePalette code, rather than the palette it creates. Open a fresh notebook, paste my code into it and execute it. This should create a palette window titled "Duplicate" at the top right of the screen. You can now close the notebook window - you don't need to save it, the code has done its job. Now go to Palettes/Install Palette on the menu, select "Duplicate" as the Source and provide an install name (e.g. Duplicate). Then click Finish. You should now have the new item in the Palette menu. – Simon Woods Aug 22 '12 at 07:58
  • I came back to this answer from a Wolfram Community post and decided to give it a shot to tweak this to make it automatic. Care to give it a shot? I haven't tested it much and we know how weird Dynamic can be – Rojo Jul 29 '13 at 01:45
  • @Rojo, looks good but Cells is new in version 9, which I don't have yet :-( – Simon Woods Jul 29 '13 at 19:58
  • @Rojo That is fantastic! I love it. A useful addition would be to add a "jump to" feature. So a Ctrl-click (or whatever combination) on a cell in the master notebook will automatically take you to the corresponding cell in the duplicate. Developer`CellInformation might come in handy here – rm -rf Jul 29 '13 at 20:16
  • +1 Any way to make this work to display one screen environment's "Screen Environment" from another? I would like to edit the notebook in a window using the "Working" environment and simultaneously view the "SlideShow" environment. Manually editing the setting in the new window does not seem to work in 10.3.0, nor does creating the slideshow and then changing the environment in the original window. – Ghersic Jan 21 '16 at 23:46
  • Very useful. Many thanks! – mikemtnbikes May 13 '16 at 15:43
  • This is great, thank you! – Filipe Miguel May 10 '23 at 19:02