What is the difference between \write and \immediate\write commands when writing to a text file?
\write command seems to write nothing on the text file...
Here a MWE:
\documentclass{article}
\texttowrite
\newwrite\mytextfile
\immediate\openout\mytextfile=\jobname.txt
\newcommand*{\texttowrite}{Some text}
\begin{document}
\write\mytextfile{\texttowrite}
\immediate\write\mytextfile{\texttowrite}
\immediate\closeout\mytextfile
\end{document}
EDIT (after egreg's answer and comment): one more exemple for beginners (like me) :
I allow myself to share this small test which illustrates an aspect of the operation of \write and \immediate\write:
\documentclass{article}
\newwrite\mytextfile
\immediate\openout\mytextfile=\jobname.txt
\newcommand*{\texttowrite}{Some text}
\begin{document}
\texttowrite
\write\mytextfile{\texttowrite 1}
\immediate\write\mytextfile{\texttowrite 2}
\write\mytextfile{\texttowrite 3}
\immediate\write\mytextfile{\texttowrite 4}
\closeout\mytextfile
\end{document}
Content of \jobname.txt:

\immediate\closeoutby just\closeoutthe\writecommand "works". So I definitly don't understand what the\immediatecommand do... – zetyty Feb 18 '22 at 12:09