CWEB 中的多行代码列表

CWEB 中的多行代码列表

这也许有点疯狂,但我想在 CWEB 程序中包含代码列表,因为该程序正在处理另一种语言的源代码。但我如何在 CWEB 中创建多行逐字代码列表?

@ Let's say we want to write a \.{CWEB} program that manipulates \.{bibtex} files.
How can we do a multiline verbatim code listing? 
This doesn't work:

\.{@Book{Author:2000,
  author={Last,First},
  title={Title},
  year=2000,
}}

This almost outputs the code correctly, but is not verbatim and defeats the purpose:

\halign{#\cr
\.{@@Book\{Author:2000,}\cr
\.{  author= \{Last, First\},}\cr
\.{  title=  \{Title\},}\cr
\.{  year= 2000,}\cr
\.{\}}\cr
}

Using an \.{eplain} macro makes it impossible to compile:

\let\ifpdf\undefined
\input eplain
\pdffalse

\verbatim|code|endverbatim

% only compiles if you enter |endverbatim\bye afterward at command line
% the output is still erroneous

在此处输入图片描述

答案1

在此处输入图片描述

@ Let's say we want to write a \.{CWEB} program that manipulates \.{bibtex} files.
How can we do a multiline verbatim code listing? 

Like this?

\begingroup\tt\obeylines\catcode`\{12 \catcode`\}12
@@Book{Author:2000,
  author={Last,First},
  title={Title},
  year=2000,
}
\endgroup

答案2

根据 David Carlisle 的回答,我定义了这些宏,它们允许几乎逐字输入(我仍然需要转义几个字符)。

\def\beginlisting{%
    \par\vskip\baselineskip%
    \begingroup%
    \tt\obeylines\obeyspaces%
    \catcode`\{12\catcode`\}12%
}
\def\endlisting{%
    \endgroup\par%
    \vskip\baselineskip%
}

@ Let's say we want to write a \.{CWEB} program that manipulates \.{bibtex} files.
How can we do a multiline verbatim code listing? 

\beginlisting
@@Book{Author:2000,
\  author = {Last,First},
\  title  = {Title},
\  year   = 2000,
}
\endlisting

Thank you, David Carlisle!

在此处输入图片描述

相关内容