I'm generating LaTeX code and, as such, am faced with the difficulty to preserve spaces between inserted LaTeX commands and ensuing text. They way I'm doing it now is as suggested by the answer to a similar question, i.e. by always placing a pair of empty braces {} after the command.
For example, assuming that there is a definition \newcommand{\arnold}{Arnold Schwarzenegger}, a source text xxx $foo is a xxx, and $foo stands in for \arnold, I'm translating the source to xxx \arnold{} is a xxx.
I'm aware of the fact that I could use xxx {\arnold} is a xxx which should be much safer (syntactically speaking)—but that would make it impossible to insert a begin-group here and an end-group there, which is an essential must-have for me.
Also, the xspace package is not a solution since I do not control what commands users would want to insert into their scripts, and I'm not even going to think about re-defining/wrapping existing commands—given the mad complexity of TeX's inner workings (which is often compared to digestion or the outcome of it), such an approach is best left for parallel worlds with unlimited resources to attempt. (What's more, Dave Carlisle himself recommends not to use xspace, so thanks, but no, thanks.)
This works to preserve the space (or lack thereof) in this particular case; however, I'm worried that some funky (La)TeX command might happen to be defined so that \foo x and \foo{} x (or \bar{argument} x and \bar{argument}{} x) turn out to produce different things.
In case that should turn out to be impossible or not so by chance, I'm fine to leave it at that; but if there are such critical commands, what alternative device could I use?
Edit Thanks to the info provided by One Well-Known TeXpert, below, I now know that the first option is no more: I want users to be able to insert arbitrary TeX fragments into their scripts, and those should always work as expected provided the entirety of the generated TeX makes sense. You can define macros so that absence vs. presence of ensuing {} does make a difference, so someone will have done that already; therefore, transparently inserting {} might cause unexpected things to happen.
IOW, I'm in need of an alternative solution—preferrably one that works in math mode as well, but I could live with treating math mode specially, because, well, because math mode has always been like a whole other thing in TeX anyways, and I can put suitable restrictions and content-checks in place to take care of that situation.
xspacecan be useful. – JLDiaz Oct 13 '15 at 20:53{}is safe enough in text mode most of the time, don't use it in math mode though. This assumes that you know\foodoesn't take arguments, or doesn't end with\ignorespacesor have other special handling/ – David Carlisle Oct 13 '15 at 21:04\ignorespacesmeans that the command should never render following spaces, that'd be OK, because it's the expected outcome to swallow spaces. But that thing about whether a command takes arguments, that's a huge stumble block. I need something foolproof that will always work. – flow Oct 13 '15 at 21:15\fboxand you change\fbox {abc}to\fbox{} {abc}then you will box the wrong thing, You have to know something about the argument structure, or it is a non-starter. – David Carlisle Oct 13 '15 at 21:24\ignorespacescomment is that{}stops spaces being ignored so\foo{}would see the space even if\fooended with\ignorespacesI'll add a safer alternative to my answer – David Carlisle Oct 13 '15 at 21:29xxx<<yyy>>zzztoXXXyyy{}ZZZ—parts outside the fences get interpreted as Markdown and converted into TeX, escaping all special characters such as{}, while those inside the fences are tunneled through untouched. #2 I see, and yes, that would in fact be a nuisance; however, I think the solution is to require users to never use spaces after a closing fence when they don't want them, and not rely on any\ignorespacesin the command definition. – flow Oct 13 '15 at 21:36