在编写dtx
文件时,是否可以macrocode
使用以下方式排版代码示例(即,环境中包含的所有内容)列表包裹?
% \iffalse meta-comment
% !TEX program = pdfLaTeX
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
demopkg --- description text
E-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
Some text about the package: probably the same as the abstract.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
demopkg --- description text
E-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
\endpreamble
\postamble
Copyright (C) 2009 by You <[email protected]>
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version. The latest version of this license is in the file:
http://www.latex-project.org/lppl.txt
This work is "maintained" (as per LPPL maintenance status) by
You.
This work consists of the file demopkg.dtx
and the derived files demopkg.ins,
demopkg.pdf and
demopkg.sty.
\endpostamble
\usedir{tex/latex/demopkg}
\generate{
\file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/demopkg}
\generate{
\file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/demopkg}
\generate{
\file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demopkg}[2009/10/06 v1.0 description text]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{\jobname}
\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\GetFileInfo{\jobname.sty}
%
%\title{^^A
% \textsf{demopkg} --- description text\thanks{^^A
% This file describes version \fileversion, last revised \filedate.^^A
% }^^A
%}
%\author{^^A
% You\thanks{E-mail: [email protected]}^^A
%}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v1.0}{2009/10/06}{First public release}
%
%\DescribeMacro{\examplemacro}
% Some text about an example macro called \cs{examplemacro}, which
% might have an optional argument \oarg{arg1} and mandatory one
% \marg{arg2}.
%
%\StopEventually{^^A
% \PrintChanges
% \PrintIndex
%}
%
% \begin{macrocode}
%<*package>
% \end{macrocode}
%
%\begin{macro}{\examplemacro}
%\changes{v1.0}{2009/10/06}{Some change from the previous version}
% \begin{macrocode}
\newcommand*\examplemacro[2][]{%
Some code here, probably
}
% \end{macrocode}
%\end{macro}
%
% \begin{macrocode}
%</package>
% \end{macrocode}
%\Finale
答案1
环境的macrocode
作用远不止是逐字环境。用以下代码替换它lstlisting
并不是一件容易的事:
- 宏名称的索引丢失。
- 有问题的是最后一行带有结束标记(
\end{macrocode}
或\end{lstlisting}
,因为第一列中的百分比字符。 - 文档条标记的格式不同。此外,第一列的百分比字符会自动被吞噬
macrocode
。
第二个问题可以通过使用选项排除带有结束环境标记的行来解决lastline
,以排除否则将包含百分号的行。的值lastline
可以通过脚本自动设置。
第三个问题可以通过将文档条标记放在环境主体中(不包含其他代码行)来解决。然后可以通过选项吞噬起始百分比字符gobble
。可以将字体设置为无衬线字体,并且可以通过功能将尖括号\langle
替换\rangle
为literate
。
文档驱动文件前言的一些样式定义:
\usepackage{listings}
\lstdefinestyle{macrocode}{
name=macrocode,
language=[LaTeX]TeX,
%basicstyle=\fontfamily{lmvtt}\selectfont\small,
basicstyle=\ttfamily\small,
columns=fullflexible,
numbers=left,
numberfirstline=1,
firstnumber=auto,
numberstyle=\scriptsize,
numbersep=5pt,
}
\lstdefinestyle{docstrip}{
style=macrocode,
basicstyle=\sffamily\small,
literate=<{$\langle$}1>{$\rangle$}1,
}
在代码段中使用时,不再需要四个空格的缩进:
% \begin{lstlisting}[style=docstrip, lastline=1, gobble=1]
%<*package>
% \end{lstlisting}
%\begin{lstlisting}[style=macrocode, lastline=3]
\newcommand*{\hello}[1]{%
Hello #1!%
}
%\end{lstlisting}
% Some text.
% \begin{lstlisting}[style=macrocode, lastline=7]
\newcommand*{\lorem}{%
Lorem
ipsum
dolor
sit
amet.%
}
% \end{lstlisting}
% \begin{lstlisting}[style=docstrip, lastline=1, gobble=1]
%</package>
% \end{lstlisting}
% End.
%\Finale
完整示例:
% \iffalse meta-comment
% !TEX program = pdfLaTeX
%<*internal>
\iffalse
%</internal>
%<*readme>
----------------------------------------------------------------
demopkg --- description text
E-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
Some text about the package: probably the same as the abstract.
%</readme>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\ifx\fmtname\nameofplainTeX\else
\expandafter\begingroup
\fi
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\preamble
----------------------------------------------------------------
demopkg --- description text
E-mail: [email protected]
Released under the LaTeX Project Public License v1.3c or later
See http://www.latex-project.org/lppl.txt
----------------------------------------------------------------
\endpreamble
\postamble
Copyright (C) 2009 by You <[email protected]>
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License (LPPL), either
version 1.3c of this license or (at your option) any later
version. The latest version of this license is in the file:
http://www.latex-project.org/lppl.txt
This work is "maintained" (as per LPPL maintenance status) by
You.
This work consists of the file demopkg.dtx
and the derived files demopkg.ins,
demopkg.pdf and
demopkg.sty.
\endpostamble
\usedir{tex/latex/demopkg}
\generate{
\file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\usedir{source/latex/demopkg}
\generate{
\file{\jobname.ins}{\from{\jobname.dtx}{install}}
}
\nopreamble\nopostamble
\usedir{doc/latex/demopkg}
\generate{
\file{README.txt}{\from{\jobname.dtx}{readme}}
}
\ifx\fmtname\nameofplainTeX
\expandafter\endbatchfile
\else
\expandafter\endgroup
\fi
%</internal>
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{demopkg}[2009/10/06 v1.0 description text]
%</package>
%<*driver>
\documentclass{ltxdoc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\lstdefinestyle{macrocode}{
name=macrocode,
language=[LaTeX]TeX,
%basicstyle=\fontfamily{lmvtt}\selectfont\small,
basicstyle=\ttfamily\small,
columns=fullflexible,
numbers=left,
numberfirstline=1,
firstnumber=auto,
numberstyle=\scriptsize,
numbersep=5pt,
}
\lstdefinestyle{docstrip}{
style=macrocode,
basicstyle=\sffamily\small,
literate=<{$\langle$}1>{$\rangle$}1,
}
%\let\macrocode\relax
\lstnewenvironment{macrocodeX}[1][]{%
\lstset{style=macrocode,#1}%
}{}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{\jobname}
\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
\DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\GetFileInfo{\jobname.sty}
%
%\title{^^A
% \textsf{demopkg} --- description text\thanks{^^A
% This file describes version \fileversion, last revised \filedate.^^A
% }^^A
%}
%\author{^^A
% You\thanks{E-mail: [email protected]}^^A
%}
%\date{Released \filedate}
%
%\maketitle
%
%\changes{v1.0}{2009/10/06}{First public release}
%
%\DescribeMacro{\examplemacro}
% Some text about an example macro called \cs{examplemacro}, which
% might have an optional argument \oarg{arg1} and mandatory one
% \marg{arg2}.
%
%\StopEventually{^^A
% \PrintChanges
% \PrintIndex
%}
%
% \begin{macrocode}
%<*package>
% \end{macrocode}
%
%\begin{macro}{\examplemacro}
%\changes{v1.0}{2009/10/06}{Some change from the previous version}
% \begin{macrocode}
\newcommand*\examplemacro[2][]{%
Some code here, probably.
Lorem
ipsum
dolor
sit
amet.%
}
% \end{macrocode}
%\end{macro}
%
% \begin{macrocode}
%</package>
% \end{macrocode}
%
% \begin{lstlisting}[style=docstrip, lastline=1, gobble=1]
%<*package>
% \end{lstlisting}
%\begin{lstlisting}[style=macrocode, lastline=3]
\newcommand*{\hello}[1]{%
Hello #1!%
}
%\end{lstlisting}
% Some text.
% \begin{lstlisting}[style=macrocode, lastline=7]
\newcommand*{\lorem}{%
Lorem
ipsum
dolor
sit
amet.%
}
% \end{lstlisting}
% \begin{lstlisting}[style=docstrip, lastline=1, gobble=1]
%</package>
% \end{lstlisting}
% End.
%\Finale
结果显示原始版本macrocode
和带有 的版本lstlisting
:
macrocode
也可以重新定义环境:
% Preamble of documentation driver:
\let\macrocode\relax
\lstnewenvironment{macrocode}[1][]{%
\lstset{style=macrocode,#1}%
}{}
style
可选参数采用和的选项lastline
,例如:
% \begin{macrocode}[style=docstrip, lastline=1, gobble=1]
%<*package>
% \end{macrocode}
%\begin{macrocode}[style=macrocode, lastline=3]
\newcommand*{\hello}[1]{%
Hello #1!%
}
%\end{macrocode}
% Some text.
% \begin{macrocode}[style=macrocode, lastline=7]
\newcommand*{\lorem}{%
Lorem
ipsum
dolor
sit
amet.%
}
% \end{macrocode}
% \begin{macrocode}[style=docstrip, lastline=1, gobble=1]
%</package>
% \end{macrocode}
%