# Writing a % to a file [duplicate]

I'm writing a macro to write some commands into a text file. This text file is supposed to be read later to execute some macros.

The macro :

\documentclass{article}
\begin{document}%
\newwrite\tmpfile%
\immediate\openout\tmpfile=output.tex%
\immediate\write\tmpfile{some text and a percent sign}%
\immediate\closeout\tmpfile%
\end{document}%


The exported text

some text and a percent sign


To avoid boring blanks, I need to finish my lines with a percent sign : %

I tried this line

\immediate\write\tmpfile{some text and a percent sign\%}%


which gave :

some text and a percent sign\%


and it's not want I want. I would like to have :

some text and a percent sign%


Is it possible to write a single percent sign (%) to a file and how to do ?

## marked as duplicate by Werner macros StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Jan 19 '17 at 20:51

• \@percentchar usage – user31729 Jan 19 '17 at 20:44
• I am not convinced that this is exactly a duplicate as this question is not specifically about \write18. As the system does not let me do otherwise now, I added another answer to that other question. – Ulrich Diez Jan 19 '17 at 23:16

Since % is the comment character, you have to change its meaning from catcode 14 to 12, i.e. a letter. This is done in latex.ltx at the definition of \@percentchar, however, this introduces another 'awkward' \makeatletter...\makeatother usage then, that's why I show the way like latex.ltx does it, with \writepercent but omitting the \makeatletter...\makeatother pair.

\documentclass{article}

\catcode\%=12
\def\writepercent{%}
\catcode\%=14

%Also possible

\makeatletter
\def\writepercentother{\@percentchar}%
\makeatother

\begin{document}%

\newwrite\tmpfile%

\immediate\openout\tmpfile=output.tex%
\makeatletter
\immediate\write\tmpfile{some text and a percent sign\@percentchar}
\makeatother
\immediate\write\tmpfile{Another important text and a percent sign\writepercent}
\immediate\closeout\tmpfile%
\end{document}