Is there a way to make some content occupy its usual space and produce the same spacing around it, but not show up in any way? (In particular, putting the content in a \phantom box is a non-solution.)
The closest ad hoc semi-solution I can think of is to set the content color to the background color. However, this will not work if the background color is not uniform, and hidden text will show up when selecting text in the resulting PDF.
I am mostly interested in a solution for XeLaTeX.
Update. I have just realized how hard this can be to achieve. If I want to hide, for example, the left-hand side of an equation, simply adding braces around the left-hand side changes the spacing before the equality sign.

{..}may affect the spacing just as\mbox{one two}may use different space thanone twobut it's not really broken just affected and you can use the same spacing in visible and invisible versions. It depends on the use case.