First, the effect of an opening Nemeth Code indicator continues until it is terminated by a Nemeth Code terminator. A braille page break does NOT end the effect of an opening Nemeth Code indicator.
Second, it is alright to include your variables in italicized passages. Part of the intent of the Nemeth Code rule to ignore typeface for variables is to cut down on typeform switches. For both of your print examples, your first proposed transcription treats typeforms and variables correctly.
Last but not least, I suspect that "dist" in dist(A, B) is an abbreviated function name, and so in braille a space should be inserted between "dist" and its argument "(A, B)".
Can you shed some light on the rational of Nemeth content continuing across braille and/or print page turns?
In both cases, the page number is in UEB. Additionally, we always use a running head which is, of course, in UEB. It's not that I'm complaining! -- it certainly frees up some valuable real estate; for instance when choices A, B, C, and D are each a full page graph -- but I am curious.
I appreciate your question. Thank you for posting it!
I think the most practical comparison is to a typeform passage. If I were transcribing an italicized title that extended from the middle of line 25 to the middle of line 1, then allowing the effect of the italic passage indicator to continue across the braille page turn would not indicate that the page number(s) or running head were italicized. The same concept applies to the effect of the opening Nemeth Code indicator.
Another way to look at this is to consider braille page numbers, print page numbers, and running heads as framing the content of the braille page and so not affected by or affecting the content (like a simple frame around a photograph).
One last thing that I find helpful is the phrase, "Only a Nemeth Code terminator ends the effect of an opening Nemeth Code indicator."
I hope one or two of the ideas above is helpful in illuminating the rationale of Nemeth content continuing across braille and/or print page turns. If not, please do post again.