Also use \tikzmark for marking the points where you need rectangle around or use mdframed or something similar to frame it.
You need to make sure that everything communicates with each other, no matter what which looks tedious and unnecessary.