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.
filecontentsenvironment. It is not really possible to use a{}command argument for this as spaces and%etc are interpretd while parsing the argument – David Carlisle Sep 21 '22 at 07:06\tofile{\noexpand\small hello}or\tofile{\unexpanded{\small hello}}. – Ulrike Fischer Sep 21 '22 at 08:12helloafter I do\tofile{\small hello}– yegor256 Sep 21 '22 at 08:39\tofile{\textbf{hello}}? – egreg Sep 21 '22 at 08:50\tofile{ 5% hhhh<newline> second line}– David Carlisle Sep 21 '22 at 09:37\small hellobut what do you want as text from$\frac{a}{b}$? – David Carlisle Sep 21 '22 at 09:42\tofile's argument, checking the meaning of each token and removing tokens whose meaning does not indicate that they are character tokens. But this way 1) you cannot reliably detect whether{and}encapsulate macro arguments or are components of the text. 2) you cannot reliably detect whether a character token should be left in place or should be removed as it is just an argument delimiter. 3) distinguishing explicit character tokens from implicit character tokens might be a problem, too. – Ulrich Diez Sep 21 '22 at 14:16\if..-switch, e.g., a macro that is set to\firstoftwo/\secondoftwodepending on the situation), and where the flag is set right before carrying out\edefand\write. – Ulrich Diez Sep 21 '22 at 14:24\texorpdfstringof the hyperref package. The hyperref-package on the one hand allows to specify by means of a flag whether TeX currently is/is not in the situation of creating a pdf-string, e.g., a bookmark or the like, that later is part of the "infrastructure" of the pdf file, and on the other hand allows to configure the way commands behave in the situation of creating a pdf-string. In your case, it would not be about a pdf string, but about a string to be written to external text file. ... – Ulrich Diez Sep 21 '22 at 14:55\text_purify:n... – Joseph Wright Sep 21 '22 at 20:49