8

I use Mathematica for math lectures at my school. I would like to find a way to attach a secondary notebook to a primary notebook using AttachCell[]. The idea is to have picture-in-picture (PIP) functionality where the small picture is locked in place in the big picture.

I want to be able to scroll through the primary notebook (containing the text content of my lectures) while having a smaller secondary notebook (containing code and graphics content that I create on the fly) locked in a small window in the upper right-hand corner. This will allow me to focus on the lecture without having to scroll up and down to find a place to type in the code so it looks nice. This will allow me to create code and show graphics on the fly without messing up the flow of my text content in the primary notebook. Also, there are many times when I simply want to keep the same code and graphics on the screen while I progress through the lecture.

So far, my code below creates a docked cell with a button. When pressed it attaches a cell to the upper right-hand corner of the notebook. I included a button on that cell to delete itself when the PIP is no longer needed. Notice, that I used a rectangle as a placeholder for the secondary notebook. Below the code is an animation of my code illustrating the idea. The black rectangular area is where the secondary notebook should reside.

SetOptions[InputNotebook[], "WholeCellGroupOpener" -> True, 
 DockedCells -> 
  Cell[BoxData@
    ToBoxes@
     Button["Open Quick Notebook", 
      AttachCell[EvaluationNotebook[], 
       Style[
        Grid[{{Button["Close Quick Notebook", 
            NotebookDelete[
             Cells[EvaluationNotebook[], AttachedCell -> True]], 
            Appearance -> {"Palette"}]}, {Graphics[{Rectangle[]}, 
            ImageSize -> 500]}}, Background -> LightGreen], 
        Deployed -> False], {Right, Top}, -20, {Right, Top}]], 
   "DockedCell"]]

enter image description here

The issue with Alexey's solution on macOS 12.3.1 running MM 13.0.1: The primary notebook scrolls upward on each keystroke inside the inputfield. See animation below.

enter image description here

B flat
  • 5,523
  • 2
  • 14
  • 36
  • Why do you not simply open a second window? Why need the second window be inside the first one? – Daniel Huber Aug 21 '22 at 08:40
  • 1
    There are multiple reasons. One of them being I would like to control just one window. With two windows I would have to reposition the second window every time I repositioned the first which is a pain. Also there is an issue keeping one screen on top of the other. Other reasons include aesthetics, convenience,etc. – B flat Aug 21 '22 at 09:07

2 Answers2

5

Here is an approach, which doesn't use AttachCell:

CreateDocument[{
  ExpressionCell[Button["Close Quick Notebook", NotebookClose[EvaluationNotebook[]]], "Print"], 
  ExpressionCell[, "Input"]}, WindowTitle -> "Quick Notebook", WindowSize -> 300,
 WindowMargins -> {
    {Automatic, AbsoluteCurrentValue[EvaluationNotebook[], {WindowMargins, 1, 2}]}, 
    {Automatic, AbsoluteCurrentValue[EvaluationNotebook[], {WindowMargins, 2, 2}]}}, 
 WindowToolbars -> {}, WindowFrame -> "Palette", WindowElements -> {"VerticalScrollBar"}, 
 ScrollingOptions -> {"VerticalScrollRange" -> Fit}, Background -> LightGreen, 
 StyleDefinitions -> 
  Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]], 
            Cell[StyleData[All], CellMargins -> 0, ImageMargins -> 0,
                 CellBracketOptions -> {"Margins" -> {0, -2}}]}, 
   StyleDefinitions -> "StylesheetFormatting.nb"]]

screenshot

It probably would be desirable do not show the title bar, but when I specify WindowFrame -> "Frameless" instead of WindowFrame -> "Palette", the window does not float on top of the other Notebook windows anymore. Addition of the option WindowFloating -> True has no effect in Mathematica versions 12.3.1 and 13.1.0, but works in version 8.0.4. Hence WindowFloating is broken for now (a bug), and we must specify WindowFrame -> "Palette" in order to create a Notebook that floats on top of the other Notebook windows.

UPDATE
As ihojnicki clarifies in comments, there was an intentional change in the behavior of the WindowFloating option in recent versions, and now this option applies only to windows with the option WindowFrame -> "Palette" (for which it is set to True by default):

WindowFloating is really only applicable to palette type frames anymore. Some window managers no longer support arbitrary combinations of frames, floating, etc... With that said, there might come a day when the palette itself is no longer supported. – ihojnicki

Unfortunately, these changes are not reflected in the documentation in any way.

Also, due to this bug, with this approach you won't be able to evaluate any code in any Notebook other than the "Quick Notebook" until you close the latter.

As a workarond for both of these problems, one may try using a third-arty application or system API to make the floating window on top of the other windows, as suggested in the comments to the question (no ready-made solution, so far, however):

If you want to "stick" the floating window to the parent window, you can use the approaches from this topic:

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
4

Here is an approach which employs InputField and AttachCell:

AttachCell[EvaluationNotebook[], 
 DynamicModule[{expr = "Plot[Sin[x],{x,-5,5}]"}, 
  Style[Panel[
    Column[{Button["Close Quick Notebook", NotebookDelete[EvaluationCell[]], 
       Appearance -> "Palette"], InputField[Dynamic[expr], String], 
      Dynamic[ToExpression@expr]}], Background -> LightGreen], 
   Deployed -> False]], {Right, Top}, -20, {Right, Top}]

screenshot

Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • 1
    Great solution! And setting Deployed to False lets you change the size of the window and interact with 3D plots. It would be nice to have an autocomplete feature in the input field so it acts more like a notebook. I'll take a look at the options for InputField[]. Very cool. Thank you. – B flat Aug 21 '22 at 19:59
  • For some reason when I enter a character in the input field the primary notebook scrolls one line up. I just noticed this now when working with a larger notebook. Not sure how to fix this. – B flat Aug 22 '22 at 08:00
  • 1
    Alexey, you should use EvaluationCell[] instead of Cells[_, AttachedCell -> True]. That would delete all of the AttachedCells in the notebook. – ihojnicki Aug 22 '22 at 12:19
  • @ihojnicki Thanks. Is there a workaround to make WindowFrame working (see my second answer here)? – Alexey Popkov Aug 22 '22 at 12:37
  • 1
    @AlexeyPopkov, the WindowFloating bit? No. WindowFloating is really only applicable to palette type frames anymore. – ihojnicki Aug 22 '22 at 13:37
  • @ihojnicki Yes, I meant WindowFloating (sorry for the typo). Does it mean that WindowFloating is deprecated? The Documentation still states that it should work, but it doesn't. And why now it is applicable only for palettes? – Alexey Popkov Aug 22 '22 at 14:17
  • @Bflat I do not observe this on a larger notebook on Windows 10 x64. Which version and OS are you using? – Alexey Popkov Aug 22 '22 at 14:24
  • 1
    @AlexeyPopkov, it hasn't been deprecated (since it is still applicable for palettes). Some window managers no longer support arbitrary combinations of frames, floating, etc... With that said, there might come a day when the palette itself is no longer supported. – ihojnicki Aug 22 '22 at 14:43
  • @ihojnicki It is sad. But on Windows everything is still supported. It is strange that functionally is removed while it is supported by an OS. – Alexey Popkov Aug 22 '22 at 14:47
  • @AlexeyPopkov I posted a video above demonstrating the scrolling issue when typing in the inputfield. Do you not have the same issue on Windows? – B flat Aug 22 '22 at 15:04
  • 1
    @Bflat No, on Windows I do not reproduce the issue. It is worth reporting to the support as a bug. – Alexey Popkov Aug 22 '22 at 19:40