# Changing case when writing in auxiliary file

in order to improve reledmac, I would like to write in an auxiliary file, but changing the case when writing in this file.

I have tried

\documentclass{article}
\begin{document}
s
\newwrite\sncf
\openout\sncf=sncf.tex

\makeatletter
\expandafter\write\expandafter\sncf\expandafter{\lowercase{TOTO}}
\write\sncf{\lowercase{TOTO}}
\protected@write{\sncf}{}{\lowercase{SNCF}}
\closeout\sncf
\end{document}


But, in the sncf.tex I get

\lowercase {TOTO}
\lowercase {TOTO}
\lowercase {SNCF}


With a little help from David Carlisles answer on this question.

The \lowercase changes argument #2 into lowercase letters, before \@tmp is defined.

\documentclass{article}

\makeatletter
% #1: stream
% #2: text
\newcommand{\WriteAsLowerCase}[2]{%
\lowercase{\def\@tmp{#2}}%
\expandafter\write\expandafter#1\expandafter{\@tmp}%
}
\makeatother

\begin{document}
s
\newwrite\sncf
\openout\sncf=sncf.tex
\WriteAsLowerCase{\sncf}{TOTO}
\WriteAsLowerCase{\sncf}{TATA}
\WriteAsLowerCase{\sncf}{SNCF}
\closeout\sncf
\end{document}


With this sncf.tex looks like this:

toto
tata
sncf


Remark: without all the \expandafters the text gets converted to lowercase too. But since \write is delayed until the next page is shiped out, the last meaning of \@tmp would be used. Thus you would get three lines, all containing sncf.