Scenario
I have a bunch of TeX files which may be processed by an external filter. The filter is (unfortunately) immutable: it does a string replacement where, for example, %d is expanded to a certain date. All the "primitives" are of the form %X where X is a letter. The date, for example, will need to be inserted in various places in the document.
However, I also want the document to still be compilable (by LaTeX) if the user has forgotten to run the filter; in this case the date references should be left as %d.
One obvious solution
Define a string which contains the objects that will be changed by the filter. At time of definition switch the catcode for the % character.
\documentclass{article}
\catcode`\%=12
\gdef\datestring{ %d }
\catcode`\%=14
\begin{document}
\datestring
\end{document}
This seems to do what I want it to do. Is there something that I am overlooking? Is there a better solution?
For clarification, after running the filter the TeX test file above gets transformed to (for example)
\documentclass{article}
\catcode`\%=12
\gdef\datestring{ 2015-06-10 }
\catcode`\%=14
\begin{document}
\datestring
\end{document}
which is why I cannot just use some other form of the %; the string has to appear exactly as %d in the TeX file.
write18? Or alternatively run a script usingsedto replace%with\%in the unfiltered file. Given your response to Manuel I guess this would meaning compiling a parent file -- maybe you do that anyway. Otherwise what's stopping you compiling from a script (including a custom macro attached to a button in your editor if necessary). – Chris H Jun 12 '15 at 09:05%an active character then%dcould easily be defined to be a command that does the same thing, or invokes the filter automatically. – Jun 12 '15 at 09:09write18is a security risk, and if I cannot count on the filter being run [which will moot the entire problem], I cannot count on thesedscript being run either.) – Willie Wong Jun 12 '15 at 09:13write18, which at the moment is not possible. – Willie Wong Jun 12 '15 at 09:17\catcode%=12 ... \catcode\%=14so everything is localised and easy to modify? – Jun 12 '15 at 09:48