0

I want to know if it's possible to write out the line \def\TEMP{ to a file, and then later write } to close the brace-pair in the written file? Essentially I'd like to do;

\documentclass{article}
\newwrite\TEMP
\immediate\openout\TEMP=Temp.tex
\begin{document}
\immediate\write\TEMP{
   \def\writefoo{% I don't want to close this brace at this point 
   }% This is suppose to close the \write command, not the \writefoo{ brace
\newcommand{\foo}{This is some text}
\immediate\write\TEMP{\foo}
\immediate\write\TEMP{
   }% This is the closing brace to the original write-out.
   }
\end{document}

I know the problem with the above code is that the \def\writefood{ closes on the } that I want to be the closing brace on the \write command, thus the \write command doesn't actually get closed.

My question is, is there a way to trick latex into writing out the unbalanced brace to the file? Since it's being written out to a file it seems like this would be plausible (if you can convince latex to close the brace artificially everything else would compile fine).

I tried to use \begingroup and \endgroup but that, predictably, did not work correctly as the resulting \def\writefoo's contents don't persist correctly.

Edited to answer questions below thoroughly:

I currently have a complex series of macros that output randomized large blocks of text, containing complex macros as well. I have managed to write them out as they are selected/generated in sequence thanks to the unexpanded command, you can see more about the specifics here.

However, (as it says at the bottom of that link) I want to be able to output more than one selection set. The best way that comes to mind is to define dynamically named macros that are just shells containing all the contents from a single large selection call. That way I can input the file and call the macros I want to use, and just not call the ones I want to regenerate.

The problem is, since the content is generated sequentially and randomly, the only way I can think to do this is to put a \def at the start, then generate and export all the content, then close the def. But that means writing out to the file unbalanced braces which LaTeX doesn't like.

Jason
  • 939
  • Have you tried \char123 ? Also what are you really trying to do, ultimately? – ShreevatsaR Sep 10 '17 at 04:11
  • Edited with (vastly) more detail as to the end goal. – Jason Sep 10 '17 at 04:19
  • The \char123 seems to just get exported as \char 123 which is not ideal heh. – Jason Sep 10 '17 at 04:24
  • Why don't you just open the file at the start, write stuff as you need to and then close it at the end? Why does it all need to be in a \def\writefoo inside the file? – cfr Sep 10 '17 at 04:45

0 Answers0