7

I need to write to a file the unexpanded LaTeX. The macro \OuputToFile is bascially an \immediate\write18 and works fine with the following:

\OuputToFile{\detokenize{$\OuterMacro{\cXXX{X}}$}}%
\OuputToFile{\detokenize{$\OuterMacro{\dXXX{X}}$}}%

where the inner macros begin with c and d. BUT, has problems if they being with e or f. That is, the following does not yield the desired results:

\OuputToFile{\detokenize{$\OuterMacro{\eXXX{X}}$}}%
\OuputToFile{\detokenize{$\OuterMacro{\fXXX{X}}$}}%

Why is this, but more importantly how do I remedy this situation?

The output of the MWE below is

enter image description here

but the desired result is

enter image description here

Code:

\documentclass{article}
\usepackage{xparse}

\immediate\write18{printf "\n" > foo.tex }% Initialize File

\NewDocumentCommand{\OuputToFile}{% m% string to output }{% \immediate\write18{printf 'string = "#1"' >> foo.tex }% \immediate\write18{printf "\n" >> foo.tex }% }

\def\MyString{$\OuterMacro{\InnerMacro{X}}$}

\begin{document} Output to file: \OuputToFile{\detokenize{$\OuterMacro{\cXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\dXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\eXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\fXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\gXXX{X}}$}}% \end{document}

Peter Grill
  • 223,288

1 Answers1

12

This is nothing to do with TeX, \e is a control character to printf.

If you try

printf "\cXXX"

on the command line you get \cXXX

But if you try

printf "\eXXX"

you get nothing.

You could use

echo -n "\eXXX"

or

printf "%s" "\eXXX"

both of which yield \eXXX, so ...

\documentclass{article}
\usepackage{xparse}

\immediate\write18{printf "\n" > foo.tex }% Initialize File

\makeatletter \NewDocumentCommand{\OuputToFile}{% m% string to output }{% \immediate\write18{printf '@percentchar s' 'string = "#1"' >> foo.tex }% \immediate\write18{printf "\n" >> foo.tex }% } \makeatother

\def\MyString{$\OuterMacro{\InnerMacro{X}}$}

\begin{document} Output to file: \OuputToFile{\detokenize{$\OuterMacro{\cXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\dXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\eXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\fXXX{X}}$}}% \OuputToFile{\detokenize{$\OuterMacro{\gXXX{X}}$}}% \end{document}

produces a foo.tex


string = "$\OuterMacro {\cXXX {X}}$"
string = "$\OuterMacro {\dXXX {X}}$"
string = "$\OuterMacro {\eXXX {X}}$"
string = "$\OuterMacro {\fXXX {X}}$"
string = "$\OuterMacro {\gXXX {X}}$"
Sebastiano
  • 54,118
David Carlisle
  • 757,742