4

following problem:

\openin\inputfile=#1%
\loop%
    \read\inputfile to \readline%
    \unless\ifeof\inputfile%
        \global\edef\filecontent{\filecontent\readline^^J}%
\repeat%
\closein\inputfile%

.....

\immediate\write\targetfile{\filecontent}

The input files contain special characters like "#", "$" and so on, as well as LaTex commands. I need to expand the LaTex commands on file read, all other special character should remain unchanged.

What I need is something like a

\detokenize{\catcode`\\=0 \readline}

which assigns all tokens except "\" a catcode of 12. Any ideas?

  • 2
    why use detokenize at all? just set the special characters to catcode 12 (eg \let\do\@makeother \dospecials would be enough) then set \\ back with \catcode`\\=0 – David Carlisle Nov 24 '16 at 8:55
  • Change the name \readline into something else, because \readline is a primitive. – egreg Nov 25 '16 at 8:48
1

I think that you're trying to write out a copy of some file, but with commands fully expanded.

You can do away with catchfile:

\begin{filecontents*}{\jobname.in}
\foo{abc} #$% \foo{A}%
xx\foo{#}
\end{filecontents*}

\documentclass{article}
\usepackage{catchfile}

\newcommand{\foo}[1]{--#1--}

\newcommand{\setupexpandfile}{%
  \endlinechar=`^^J % when writing out, newlines will be preserved
  \catcode`\#=12 % so # makes no problem
  \catcode`\%=12 % so % is not a comment
  % maybe others
}

\newcommand{\expandfile}[2]{%
  \CatchFileEdef{\temp}{#1}{\setupexpandfile}%
  \immediate\openout\denisout=#2\relax
  \immediate\write\denisout{\temp}%
  \immediate\closeout\denisout
}

\newwrite\denisout

\expandfile{\jobname.in}{\jobname.out}

\stop

I don't think you really need other characters to be managed specially.

Here is the contents of the .out file after processing.

--abc-- #$% --A--%
xx--#--
| improve this answer | |

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.