2
$\begingroup$

I created a button that temporarily fixes the text alignment bug described here. The code for the button is shown below.

Button["Update", 
 Do[FrontEndExecute[
   FrontEndToken[InputNotebook[], "ToggleDynamicUpdating"]], {2}]]

The gif below illustrates how it works.

enter image description here

I would like to make this better by not using a button at all. Instead, I would like the code to trigger anytime the window size changes which would fix the issue permanently. Seems like something simple but can't figure it out.

Edit 1: I came up with an improvement but still not what I want. I put the following code into my init.m file and now it updates when I move the cursor in the notebook window. Unfortunately, it does not update while I'm changing the size of the window. I have to move the cursor into the notebook for the update the happen.

SetOptions[$FrontEndSession,FrontEndEventActions->{"MouseMoved":>Do[FrontEndExecute[FrontEndToken[InputNotebook[],"ToggleDynamicUpdating"]],{2}],PassEventsDown->True}]

I somehow need to trigger the code when there is a change to CurrentValue[WindowSize]. Any ideas?

$\endgroup$
4
  • 1
    $\begingroup$ You can use something like Dynamic[Echo@AbsoluteCurrentValue@WindowSize] to print the current window size every time it is changed (you might need to open the "Messages" window to see the messages). You can of course add any code instead of Echo[...] $\endgroup$
    – Lukas Lang
    Dec 16, 2022 at 18:12
  • $\begingroup$ @lukasLang thanks Lukas but I don't want to print the code. Rather, I need to execute (trigger) the code when window size is changed. $\endgroup$
    – B flat
    Dec 16, 2022 at 21:02
  • 1
    $\begingroup$ The printing was just an example, as I mentioned. You can put whatever you want there, as long as you keep the AbsoluteCurrentValue[WindowSize] there, the content of the Dynamic will be reevaluated when the window size changes. You should also be able to use NotebookDynamicExpressioninstead if you want the evaluation to happen once for the entire notebook $\endgroup$
    – Lukas Lang
    Dec 17, 2022 at 8:36
  • $\begingroup$ @LukasLang My apologies. That did it! Thank you. I'll accept it if you would like to post an answer. $\endgroup$
    – B flat
    Dec 17, 2022 at 11:50

1 Answer 1

3
$\begingroup$

Posting this as an answer since it seems to fulfil the requirements

You can use NotebookDynamicExpression together with AbsoluteCurrentValue[WindowSize] to get an expression that is dynamically updated when the window size is changed:

SetOptions[
 EvaluationNotebook[],
 NotebookDynamicExpression :> Echo@AbsoluteCurrentValue[WindowSize]
 ]

enter image description here

Note the use of :> (RuleDelayed) for the option to prevent premature evaluation (wrapping the option in Dynamic also works). The Echo is just used to demonstrate that it works, and it can be replaced with any other function that depends on the window size. If you don't care about the actual window size and just want to run code when it changes, something like

NotebookDynamicExpression:>(AbsoluteCurrentValue[WindowSize];(* do stuff... *))

works

$\endgroup$

Your Answer

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

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