1

I'm trying to write in some external files using the filecontents environment of LaTeX. For this purpose, I created an environment which writes the contents and numbers the file with a counter.

However the code does not compile. I get the error File ended while scanning use of ^^M

MWE

\documentclass[11pt]{book}

\usepackage{currfile}

\newcounter{Qcours}
\setcounter{Qcours}{0}
\newenvironment{qcours}{%
    \refstepcounter{Qcours}
    \begin{filecontents}{\theQcours.tex}}{\end{filecontents}}

%
\begin{document}

It's working fine.
\begin{filecontents}{\theQcours.tex}
ABCDE   
\end{filecontents}

But the following don't.

\begin{qcours}
    FGHI
\end{qcours}

\end{document}
1
  • verbatim-like environments (including filecontents) can not be used in the definition of other environments Feb 5, 2021 at 14:36

2 Answers 2

1

Use fancyvrb:

\documentclass[11pt]{book}
\usepackage{fancyvrb}

\newcounter{Qcours}
%\setcounter{Qcours}{0}

\newenvironment{qcours}{%
  \VerbatimEnvironment\stepcounter{Qcours}%
  \begin{VerbatimOut}{\theQcours.tex}%
}{\end{VerbatimOut}}


\begin{document}

\begin{qcours}
    FGHI
\end{qcours}

\end{document}
0

Try putting the following (untested) code in qcours.sty and doing \usepackage{qcours}:

\RequirePackage{verbatim}
\newcounter{Qcours}
\def\Qcours{%
  \@bsphack
  \refstepcounter{Qcours}
  \immediate\openout\qcours@out=\theQcours.tex
  \catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\qcours@out
       {\the\verbatim@line}}%
  \verbatim@start}
\def\endQcours{%
  \immediate\qcours@out
  \@esphack
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .