8
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Why do you not simply open a second window? Why need the second window be inside the first one? $\endgroup$ Aug 21, 2022 at 8:40
  • 1
    $\begingroup$ 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. $\endgroup$
    – B flat
    Aug 21, 2022 at 9:07

2 Answers 2

4
$\begingroup$

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

$\endgroup$
11
  • 1
    $\begingroup$ 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. $\endgroup$
    – B flat
    Aug 21, 2022 at 19:59
  • 1
    $\begingroup$ Alexey, you should use EvaluationCell[] instead of Cells[_, AttachedCell -> True]. That would delete all of the AttachedCells in the notebook. $\endgroup$
    – ihojnicki
    Aug 22, 2022 at 12:19
  • 1
    $\begingroup$ @AlexeyPopkov, the WindowFloating bit? No. WindowFloating is really only applicable to palette type frames anymore. $\endgroup$
    – ihojnicki
    Aug 22, 2022 at 13:37
  • 1
    $\begingroup$ @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. $\endgroup$
    – ihojnicki
    Aug 22, 2022 at 14:43
  • 1
    $\begingroup$ @Bflat No, on Windows I do not reproduce the issue. It is worth reporting to the support as a bug. $\endgroup$ Aug 22, 2022 at 19:40
5
$\begingroup$

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:

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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