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}
5
  • 2
    nothing to do with tex, try printf "\cXXX" on the commandline and compare with printf "\eXXX" why use printf rather than just writing the file directly? Nov 30 '21 at 0:31
  • 1
    you could use printf "%s" "\eXXX" etc which prints \eXXX or use echo instead of printf Nov 30 '21 at 0:36
  • Every \OuputToFile instruction generates a write to the log file of the form runsystem(printf 'string = "$\OuterMacro {\cXXX {X}}$"' >> foo.tex )...disabled (restricted). Do I need to enable something?
    – Mico
    Nov 30 '21 at 0:40
  • @Mico: I think you need -shell-escape. Nov 30 '21 at 0:51
  • (there's also tex core - How can I open a file in "append" mode? - TeX - LaTeX Stack Exchange, as well as solutions that handle everything in memory)
    – user202729
    Nov 30 '21 at 1:02
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}}$"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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