2

This question already has an answer here:

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 Jan 19 '17 at 20:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    \@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
3

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}

Not the answer you're looking for? Browse other questions tagged or ask your own question.