我正在尝试编写一个程序包,它接受逐字输入并将其附加到多个文件中。我目前的方法是基于Martin Scharrer 的回答类似的问题。困难在于输入中的所有=>
和都不会输出到所需的文件。也就是说,对于->
\jobname.xtr
\begin{extract}[foo.bel]
a -> b
\end{extract}
我们a b
输出到文件而不是a -> b
。我相信这可能是由于
\lstset{literate=%
{=>}{{$\Rightarrow~$}}2 %
{->}{{$\rightarrow~$}}2 %
}
在文档的前奏中,因为它没有与一起出现\lstset{literate={}}
。除了取消设置literate
选项外lstset
,如何解决这个问题?
整个包包含在下面,因为它仅包含底部有问题的环境所需的宏(例如appendtofile
,apendfiletofile
等等) 。extract
%% This is file 'extract.sty'
%%
%% Copyright (C) 2012 Ryan Kavanagh <[email protected]>
%%
%% 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
答案1
很难理解你在那个包中做什么(请注意,已经有一个extract
包)。
但是,删除全局\lstset
设置并说
\lstinputlisting[literate={=>}{{$\Rightarrow~$}}2%
{->}{{$\rightarrow~$}}2]{\jobname.xtr}
代替
\lstinputlisting{\jobname.xtr}
解决问题。也许你可以从这里开始。
你是真的pstricks
只为了加载\psforeach
?