5

I'm using the technique described in egreg's answer to Efficient way to write backslash to a file to write LaTeX code in an external file:

\newwrite\mywriter
\NewEnviron{writethis}
    {\immediate\write\mywriter{\unexpanded\expandafter{\BODY}}}
\begin{document}

\immediate\openout\mywriter=\jobname-later.tex
\begin{writethis}\textbf{abc}\end{writethis}
\immediate\closeout\mywriter

However, I cannot use it with verbatim text:

\begin{writethis}
\begin{verbatim}
  foo() {
  }
\end{verbatim}
\end{writethis}

The newlines are removed.

How to copy verbatim code with \immediate\write?

  • 1
    the verbatim package has a \verbatimwrite command. does this help? – barbara beeton Jul 1 '15 at 13:57
4

I wouldn't reinvent the wheel. ;-)

\documentclass{article}

\usepackage{fancyvrb}

\begin{document}

\begin{VerbatimOut}{\jobname-later.tex}
\begin{verbatim}
  foo() {
  }
\end{verbatim}
\end{VerbatimOut}

Something

\input{\jobname-later}

\end{document}

One can use the infrastructure of fancyvrb to define VerbatimOutAppend so that the file is closed only when we want to.

\documentclass{article}

\usepackage{fancyvrb}

\makeatletter
\newcommand{\OpenVerbatimOutAppend}[2]{%
  \expandafter\newwrite\csname martin@write@#1\endcsname
  \immediate\openout\csname martin@write@#1\endcsname=#2\relax
}
\newcommand{\CloseVerbatimOutAppend}[1]{%
  \immediate\closeout\csname martin@write@#1\endcsname
}
\def\VerbatimOutAppend{\FV@Environment{}{VerbatimOutAppend}}
\def\FVB@VerbatimOutAppend#1{%
  \@bsphack
  \begingroup
    \FV@UseKeyValues
    \FV@DefineWhiteSpace
    \def\FV@Space{\space}%
    \FV@DefineTabOut
    \expandafter\let\expandafter\FV@OutFile\csname martin@write@#1\endcsname
    \def\FV@ProcessLine{\immediate\write\FV@OutFile}%
    \let\FV@FontScanPrep\relax
    \let\@noligs\relax
    \FV@Scan}
\def\FVE@VerbatimOutAppend{\endgroup\@esphack}
\DefineVerbatimEnvironment{VerbatimOutAppend}{VerbatimOutAppend}{}
\makeatother

\begin{document}

\OpenVerbatimOutAppend{default}{\jobname-later.tex}

\begin{VerbatimOutAppend}{default}
\begin{verbatim}
  foo() {
  }
\end{verbatim}
\end{VerbatimOutAppend}

Something

\begin{VerbatimOutAppend}{default}
\begin{verbatim}
  foo() {
  }
\end{verbatim}
\end{VerbatimOutAppend}

\CloseVerbatimOutAppend{default}

\input{\jobname-later}

\end{document}

Several output files can be active at the same time; to each we assign a symbolic name, in the example it is default. Here's the content of the output file:

\begin{verbatim}
  foo() {
  }
\end{verbatim}
\begin{verbatim}
  foo() {
  }
\end{verbatim}
  • I need to stack in the same generated file, normal text, latex commands and verbatim text with different \begin{writethis} and \begin{VerbatimOut}. Is this possible? – Martin Monperrus Jul 1 '15 at 14:46
  • @MartinMonperrus Sorry, but this is too generic. – egreg Jul 1 '15 at 15:09
  • I want to append something to \jobname-later.tex. AFAIU, VerbatimOut replaces the content, see my test pastebin.ubuntu.com/11808823 – Martin Monperrus Jul 2 '15 at 8:18
  • @MartinMonperrus Code is online. – egreg Jul 2 '15 at 12:52

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.