Is it possible to automatically change keyboard layouts after $$?

So, I’ve been writing my math lecture notes in obsidian for sometime. That made me realize that if keyboard layout was changing automatically to English every time I open $ $ or $$ $$, it would faster my typing so much.

What I’m trying to do

Is there any plugin or way to make keyboard layout change automatically to en after $$ signs?

Things I have tried

I tried to find this feature in Quick Latex and Latex Suite plugins, but didn’t succeed.

Karabiner-Elements has select_input_source. I guess you can configure from‑events where input source is changed after $ is typed. I’m not sure if you can trigger actions based on written text (only based on what keys are pressed alone or together).