1

I'm trying to create a command that will be able to save plain text to a file (without any TeX commands):

\documentclass{article}
\begin{document}
\newwrite\xwrite
\newcommand\tofile[1]{
  \immediate\openout\xwrite=foo.txt\relax
  \immediate\write\xwrite{#1}
  \immediate\closeout\xwrite
}
\tofile{hello} % Works well
\tofile{\small hello} % Error here!
\end{document}

What is the right way to somehow "escape" commands before saving?

I expect \tofile{\frac{a}{b}} to save ab to the file.

17
  • 1
    you could use the standard filecontents environment. It is not really possible to use a {} command argument for this as spaces and % etc are interpretd while parsing the argument Sep 21, 2022 at 7:06
  • in your example you can use \tofile{\noexpand\small hello} or \tofile{\unexpanded{\small hello}}. Sep 21, 2022 at 8:12
  • Can you please be more specific about your need? Do you need to write (and overwrite) just a single line verbatim?
    – egreg
    Sep 21, 2022 at 8:30
  • @egreg I need a command that will save plain text to file. So, I expect my file to contain hello after I do \tofile{\small hello}
    – yegor256
    Sep 21, 2022 at 8:39
  • 1
    Related recent question: tex.stackexchange.com/questions/655580/…
    – Marijn
    Sep 21, 2022 at 9:57

0

You must log in to answer this question.

Browse other questions tagged .