Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 http://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
share|improve this question

closed as unclear what you're asking by Jubobs, Jesse, cmhughes, Werner, Claudio Fiandrino Jan 16 at 7:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Please provide a minimal example instead of a whole package. –  Marco Daniel Jun 2 '12 at 11:15
    
I added a mini-example. As stated above, the whole package was provided because it only consists of the required macros for the extract environment. –  Ryan Kavanagh Jun 4 '12 at 15:21
    
Sorry but this isn't a MWE. Please read: minimal working example (MWE) –  Marco Daniel Jun 4 '12 at 17:14
add comment

1 Answer 1

It's quite difficult to understand what you're doing in that package (note that there already is an extract package).

However, removing the global \lstset setting and saying

\lstinputlisting[literate={=>}{{$\Rightarrow~$}}2%
  {->}{{$\rightarrow~$}}2]{\jobname.xtr}

instead of

\lstinputlisting{\jobname.xtr}

cures the problem. Probably you can start from here.

Are you really loading pstricks just for \psforeach?

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.