I'm trying to write a package that takes verbatim input and appends it to several files. My current approach is based of off an answer by Martin Scharrer to a similar problem. The difficulty arises in that all occurrences of =>
and ->
in the input are not output to \jobname.xtr
nor to the desired files. That's to say, for
\begin{extract}[foo.bel]
a -> b
\end{extract}
we have a b
output to the files instead of a -> b
. I believe this may be due to an occurrence of
\lstset{literate=%
{=>}{{$\Rightarrow~$}}2 %
{->}{{$\rightarrow~$}}2 %
}
in the document's prelude, since it does not occur with \lstset{literate={}}
. Short of unsetting the literate
option for lstset
, how does one fix this?
The whole package is included below, since it only consists of the required macros (e.g. appendtofile
, apendfiletofile
, etc.) for the problematic extract
environment at the bottom.
%% This is file 'extract.sty'
%%
%% Copyright (C) 2012 Ryan Kavanagh <rak@debian.org>
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%% http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%%
%% This work has the LPPL maintenance status ``author-maintained''.
%%
%% \appendfile is by Martin Scharrer and was obtained from https://tex.stackexchange.com/a/12414/6128
%% It too is distributed under the LLPL 1.3c or later.
%
% Usage:
%
% \begin{extract}[a,b,c,d]
% Some verbatim text to be displayed using whichever lstlisting configurations
% are active. This text will be appended to the files a, b, c, and d.
%
% To set the listings options precede `\begin{extract}[files]' with a
% '\lstset{listings options here}'.
% \end{extract}
%
% \begin{extract*}
% Some text to be appended to the files a, b, c, and d, but not displayed.
% \end{extract*}
%
\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{extract}[2012/05/29 v0.1 extracts lstlistings to files]
\RequirePackage{catchfile}
\RequirePackage{ifthen}
\RequirePackage{listings}
\RequirePackage{pstricks}
\def\recordfile#1{appendtofile@seen@#1}
\def\srecordfile#1{\expandafter\csname appendtofile@seen@#1\endcsname}
\def\defrecordfile#1{%
\expandafter\def\csname appendtofile@seen@#1\endcsname{defined}
}
\newwrite\appendwrite
\newcommand*\appendtofile[2]{%
\begingroup
\PackageWarning{extract}{\recordfile{#1}}
% ifundefined takes command name *without the slash*
\ifthenelse{\equal{\srecordfile{#1}}{defined}}{%
% Keep existing EOL
\CatchFileDef{\filecontent}{#1}{\endlinechar=`^^J\catcode\endlinechar=12\obeyspaces\relax}
}{%
% If we've not yet output to this file from this run, empty it
% Prevents having files with multiple concatenations of the same thing, one
% for each LaTeX run.
\global\defrecordfile{#1}
\let\filecontent\empty
}
\immediate\openout\appendwrite=#1\relax
\immediate\write\appendwrite{\filecontent #2}%
\immediate\closeout\appendwrite
\endgroup
}
\newread\appendingfile
\newcommand*\appendfiletofile[2]{%
\begingroup
% ifundefined takes command name *without the slash*
\ifthenelse{\equal{\srecordfile{#1}}{defined}}{%
% Keep existing EOL
\CatchFileDef{\filecontent}{#1}{\endlinechar=`^^J\catcode\endlinechar=12\obeyspaces\relax}
}{%
%\expandafter\ifcsundef{recordfile{#1}}{%
% If we've not yet output to this file from this run, empty it
% Prevents having files with multiple concatenations of the same thing, one
% for each LaTeX run.
\global\defrecordfile{#1}
\let\filecontent\empty
}
\openin\appendingfile=#2
\immediate\openout\appendwrite=#1\relax
\endlinechar-1
\immediate\write\appendwrite{\filecontent }
\loop \unless\ifeof\appendingfile
\readline\appendingfile to\line
\immediate\write\appendwrite{\line}
\repeat
\immediate\closeout\appendwrite
\immediate\closein\appendingfile
\endgroup
}
\newcommand\firstunempty[3]{%
\ifthenelse{\equal{#2}{}}{% Do nothing if empty
}{
#1#2#3
}
}
\newcommand\appendtofilewrapper[2]{\firstunempty{\appendtofile}{#1}{#2}}
\newcommand\appendfiletofilewrapper[2]{\firstunempty{\appendfiletofile}{#1}{#2}}
\lst@RequireAspects{writefile}
\lstnewenvironment{extract}[1][]{%
\ifthenelse{\equal{#1}{}}{%
\PackageError{extract}{extract with no filenames passed!}
}{}
\lst@BeginWriteFile{\jobname.xtr}%
}{%
\lst@EndWriteFile
\psforeach{\F}{#1,}{\appendfiletofilewrapper{\F}{{\jobname.xtr}}}
\lstinputlisting{\jobname.xtr}
}
\lstnewenvironment{extract*}[1][]{%
\ifthenelse{\equal{#1}{}}{%
\PackageError{extract}{extract* with no filenames passed!}
}{}
\lst@BeginWriteFile{\jobname.xtr}%
}{%
\lst@EndWriteFile
\psforeach{\F}{#1,}{\appendfiletofilewrapper{\F}{{\jobname.xtr}}}
}
\makeatother