Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How can I write % into an auxiliary file? % will not work, because LaTeX thinks that I start a comment with it (in the main file, not in the auxiliary file!), and \% will write \% into the file. How can it be accomplished?

\documentclass{article}
\begin{document}
\newwrite\outfile
\immediate\openout\outfile=example.dat
\immediate\write\outfile{%}% will not work, of course, but neither \%
\immediate\closeout\outfile
\end{document}
share|improve this question
1  
Have a look at my answer to How can I provide a verbatim (unescaped) commandline for executing with \write18? which should also answer this question. –  Martin Scharrer Nov 10 '11 at 20:33
    
@MartinScharrer: Please see additional answer. You could have posted that as answer yourself. +1 to your comment. –  Stephen Nov 14 '11 at 18:04
add comment

3 Answers

up vote 8 down vote accepted

There is \@percentchar that expands to a literal % character. You need to enclose your writing operation in a \makeatletter \makeatother pair

\makeatletter
\newwrite\outfile
\immediate\openout\outfile=example.dat
\immediate\write\outfile{\@percentchar}
\immediate\closeout\outfile
\makeatother
share|improve this answer
add comment

use \@percentchar or \charxxx where xxx is the decimal number of %

share|improve this answer
1  
\symbol{37} won't write %, but rather \char37. –  egreg Nov 10 '11 at 19:03
    
true, forgot about the write –  Herbert Nov 10 '11 at 19:20
1  
Also \char is unexpandable –  egreg Nov 10 '11 at 20:43
1  
And being unexpandable I can't use it. –  Stephen Nov 14 '11 at 18:04
add comment

Following the comment of Martin Scharrer I read up on his newverbs package. With this the following is possible:

\documentclass{article}
\usepackage{newverbs}
\Verbdef\verbpercent{%}
\begin{document}
\newwrite\outfile
\immediate\openout\outfile=example.dat
\immediate\write\outfile{\verbpercent}
\immediate\closeout\outfile
\end{document}

Comparison with the use of \@percentchar:

  • Advantage: \Verbdef can generally be used for any verbatim text, and doesn't require \makeatletter \makeatother.

  • Disadvantage: It uses more of TeX's memory and more time to compile (both: slightly), and theoretically there could be an incompatibility between package newverbs and some other package.

(Therefore I use \@percentchar, but reference to the newverbs package surely is useful for the general case.)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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