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

\tofile{hello} % Works well
\tofile{\small hello} % Error here!

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

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

  • 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


You must log in to answer this question.

Browse other questions tagged .