5

Consider the following example, in which the sentence Ceci est un résumé. is written into the file \jobname.mytmp and then being read back.

\documentclass{article}

\usepackage{fancyvrb}

\begin{document}

\begin{VerbatimOut}{\jobname.mytmp} Ceci est un résumé. \end{VerbatimOut}

\input{\jobname.mytmp}

\end{document}

However, this cannot be compiled with pdflatex, as one would get Package inputenc: Invalid UTF-8 byte sequence. Further investigation shows that \jobname.mytmp is not coded in UTF-8 but rather in "Windows 1252" (this is guessed by vscode, personally I don't know what this is). Is there a fix to this?

Jinwen
  • 8,518
  • Is something stopping you from using either XeLaTeX or LuaLaTeX? – Mico May 09 '21 at 07:22
  • @Mico Personally I usually use XeLaTeX. But this code is in a package that suppose to compile with any major TeX engine, so I'm looking for a fix for the pdfLaTeX case. – Jinwen May 09 '21 at 07:26

1 Answers1

4

The fancyvrb package uses \immediate\write, but this has the consequence that active characters not explicitly inactivated (such as ~) are expanded.

If you patch the code to use a “protected” immediate write, it works.

\documentclass{article}

\usepackage{fancyvrb} \usepackage{etoolbox}

\makeatletter % define the same as \protected@write, but with \immediate \providecommand*\protected@iwrite[3]{% \begingroup \let\thepage\relax #2% \let\protect@unexpandable@protect \edef\reserved@a{\immediate\write#1{#3}}\reserved@a \endgroup \if@nobreak\ifvmode\nobreak\fi\fi } \patchcmd{\FVB@VerbatimOut} {\immediate\write\FV@OutFile} {\protected@iwrite\FV@OutFile{}} {}{} \makeatother

\begin{document}

\begin{VerbatimOut}{\jobname.mytmp} Ceci est un résumé. \end{VerbatimOut}

\input{\jobname.mytmp}

\end{document}

The contents of the written file is

    Ceci est un résumé.
egreg
  • 1,121,712
  • Thank you for this! May I ask one question: will this patch possibly cause any other undesirable new errors in some case? – Jinwen May 09 '21 at 08:38
  • @Jinwen I don't think so, because macros are already “neutralized” because the backslash has been made a printable character. – egreg May 09 '21 at 09:16