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.

yegor256
  • 12,021
  • 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 – David Carlisle Sep 21 '22 at 07:06
  • in your example you can use \tofile{\noexpand\small hello} or \tofile{\unexpanded{\small hello}}. – Ulrike Fischer Sep 21 '22 at 08: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 '22 at 08: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 '22 at 08:39
  • @yegor256 Sorry, but some more details are needed. What kind of commands do you want to ignore? What about \tofile{\textbf{hello}}? – egreg Sep 21 '22 at 08:50
  • also what do you expect from \tofile{ 5% hhhh<newline> second line} – David Carlisle Sep 21 '22 at 09:37
  • "So, I expect my file to contain hello" please put that in the question all previous comments assumed you wanted \small hello but what do you want as text from $\frac{a}{b}$ ? – David Carlisle Sep 21 '22 at 09:42
  • I updated the question (expecting "it's impossible" answer) – yegor256 Sep 21 '22 at 09:51
  • 1
    Related recent question: https://tex.stackexchange.com/questions/655580/need-macro-to-write-content-into-a-file-as-a-string-from-latex – Marijn Sep 21 '22 at 09:57
  • see the post @Marijn linked, also if using luatex you can typeset in a box then iterate over the character nodes in the box – David Carlisle Sep 21 '22 at 10:00
  • A recursive loop could be used for iterating on \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
  • Probably you can define customized commands which behave differently depending on a flag (e.g., an \if..-switch, e.g., a macro that is set to \firstoftwo/\secondoftwo depending on the situation), and where the flag is set right before carrying out \edef and \write. – Ulrich Diez Sep 21 '22 at 14:24
  • It seems you desire a mechanism that removes all formatting instructions, leaving only plain text. This raises the question of what should happen in cases where a TeX command is not used to format text, but to produce text. For example, a command that gets as arguments a number with the value K and a text phrase and typesets the text phrase K times. Or places the box held in a box register several times. Or expands to a long designation for which you have defined an abbreviating macro in the .tex source code. ... – Ulrich Diez Sep 21 '22 at 14:40
  • ... It may be easier to extract the plain text from the typeset pdf after compilation than to parse arguments during compilation. – Ulrich Diez Sep 21 '22 at 14:40
  • Or implement a mechanism that works similar to \texorpdfstring of 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
  • ... It is a pity that there is no extensible "situation management" in (La)TeX to branch out according to what TeX is doing/what kind of thing TeX is currently preparing. – Ulrich Diez Sep 21 '22 at 14:55
  • You might be after \text_purify:n ... – Joseph Wright Sep 21 '22 at 20:49

0 Answers0