如何让noweb和listings合作?

如何让noweb和listings合作?

Linux 子系统在 Windows 中的引入使得 noweb 的使用成为可能 (https://www.cs.tufts.edu/~nr/noweb/) 在 Windows 下。我想漂亮地打印生成的代码,对此可以说 listings 包是首选方法。

% -*- TeX:UK -*-
\NeedsTeXFormat{LaTeX2e}
\documentclass[british,pdftex,twoside,12pt,notitlepage]{article}
\usepackage[utf8]{inputenc}%suitable for most modern operating systems
\usepackage[T1]{fontenc}
\usepackage{babel,%foreign language support
            xcolor,%handle colours
            css-colors,%defines named web-safe colors
            listings,%typeset computer code
            noweb}%support for literate programming
\frenchspacing
\selectlanguage{british}
\lstloadlanguages{Pascal}
\lstset{backgroundcolor=\color{Cornsilk},
        language=[Borland6]Pascal,
        numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,
        }
\title{Example}
\author{Engelbert Buxbaum }
\date{\today}
\begin{document}
@
\maketitle
\abstract{Noweb is a simple to use system for literate programming. It 
is suitable for all programming languages, but has no
pretty-printing capabilities. Listings can pretty-print, but has no 
literate programming ability. A combination of both would be ideal. }
The example code is:
\begin{lstlisting}
<<*>>=
program HelloWorld;
begin
  write('Hello, World');
  readln;
end
@
\end{lstlisting}
Unfortunately, the output is not what was intended.
\end{document}

noweave -delay test.nw > test.tex生成以下 LaTeX 代码(仅关键部分):

\begin{lstlisting}
\nwenddocs{}\nwbegincode{2}\moddef{*}\endmoddef\nwstartdeflinemarkup
\nwenddeflinemarkup
program HelloWorld;
begin
  write('Hello, World');
  readln;
end
\nwendcode{}\nwbegindocs{3}\nwdocspar
\end{lstlisting}

也就是说,一些代码noweb.sty最终出现在 listings 环境中,并逐字打印出来而不是执行。将\begin{lstlisting}and放在and\end{lstlisting}之间当然不是解决方案。<<*>>@

我该怎么做?提前致谢。

恩格尔伯特

答案1

我认为使用过滤器可以实现您想要的功能。下面您将找到一个经过破解的awk脚本,它至少在经过一些小修改后能够处理您的文件test.nw(见下文)。它可以按如下方式使用:

noweave -delay -filter 'gawk -f lstfilter.awk' test.nw > test.tex

并生产

PDF 截图

这是修改后的文件test.nw(我刚刚删除了它们\begin{lstlisting}\end{lstlisting}现在将它们插入到过滤器中):

% -*- TeX:UK -*-
\NeedsTeXFormat{LaTeX2e}
\documentclass[british,pdftex,twoside,12pt,notitlepage]{article}
\usepackage[utf8]{inputenc}%suitable for most modern operating systems
\usepackage[T1]{fontenc}
\usepackage{babel,%foreign language support
            xcolor,%handle colours
            css-colors,%defines named web-safe colors
            listings,%typeset computer code
            noweb}%support for literate programming
\frenchspacing
\selectlanguage{british}
\lstloadlanguages{Pascal}
\lstset{backgroundcolor=\color{Cornsilk},
        language=[Borland6]Pascal,
        numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,
        }
\title{Example}
\author{Engelbert Buxbaum }
\date{\today}

\begin{document}
@
\maketitle
\abstract{Noweb is a simple to use system for literate programming. It
is suitable for all programming languages, but has no
pretty-printing capabilities. Listings can pretty-print, but has no
literate programming ability. A combination of both would be ideal. }
@
The example code is:
<<*>>=
program HelloWorld;
begin
  write('Hello, World');
  readln;
end
@
Unfortunately, the output is not what was intended.
\end{document}

这是你想要的吗?以下是代码lstfilter.awk

BEGIN {
    skip_nl = 0
    chunk = -1
}

/^@begin code/ {
    chunk = $3
}

/^@defn/ {
    print $0
    print "@nl"
    print "@end code " chunk
    print "@text \\begin{lstlisting}"
    skip_nl = 1
    next
}

/@end code/ {
    if ($3 == chunk) {
        print "@text \\end{lstlisting}"
        chunk = -1
        next
    }
}

/^@nl/ {
    if (skip_nl) {
        skip_nl = 0
        next
    }
}

{
    print $0
}

如果您保存lstlistings.awk在与上面相同的目录中,则test.nw可以调用test.nw。但是,我还没有在更复杂的示例中测试过此脚本。我只是在学习了解如何使用noweb以及它如何工作。我不确定使用过滤器是否是正确的方法。根本无法理解noweb.sty(可以notangle使用src/tex/support.nw,这里是 pdf支持.pdf)。

一些一般说明

我本周才开始使用noweb。当我读到“黑客指南”。我非常有兴趣与其他刚接触 noweb 和文学编程的人取得联系。(我没有找到邮件列表,还是我错过了什么?)

相关内容