因此,我正在用 LaTeX 创建一个相当大的项目,并且我们已经定义了自己的 API,所以我当前的工作是为 LaTeX 中的每个 API 函数创建类似 MAN 的页面。我们对每个函数使用的标准格式是:
\bmumansection{NAME}{%<*NAME>
%text here
%</NAME>
}
\bmumansection{SYNOPSIS}{%<*SYNOPSIS>
%text here
%</SYNOPSIS>
}
\bmumansection{DESCRIPTION}{%<*DESCRIPTION>
%text here
%</DESCRIPTION>
}
\bmumansection{RETURN VALUES}{%<*RETURN>
%text here
%</RETURN>
}
\bmumansection{POSSIBLE ERRORS}{%<*ERRORS>
%text here
%</ERRORS>
}
\bmumansection{SEE ALSO}{%<*SEE>
%text here
%</SEE>
}
\bmumansection
是一个处理创建 MAN 类节标题以及处理标签等的函数。
现在,由于 API 函数代码被大量重复使用,我们必须使我们的函数文本也可以重复使用。为此,我们使用了库catchfilebetweentags
,它允许我们定义标签,然后捕获这些标签之间的文件部分。(<*NAME> 是此类标签的一个示例)。
现在,我的问题是:
我想为每个函数创建一个内容条目,但实际上不显示该部分文本。NAME 标记之间的文本非常适合这种内容。我创建了自己的 \include 命令(称为\bmuinclude
),它基本上是 : define 的包装器,\include
如下所示:
\newcommand{\bmuinclude}[1]{
\phantomsection
\addcontentsline{toc}{subsection}{ \ExecuteMetaData[functions/#1]{NAME}}
label{func:#1}
\include{functions/#1}
}
\ExecuteMetaData
是一个函数,它捕获指定的文件functions/#1
,然后捕获 NAME 标记之间的文本。所以我的目标是获取一个内容行,内容行是:Application.Connect
然而,由于错误(?)或功能误用,我得到的是这样的:
[functions/ApplicationConnect]NAME
似乎实际的命令被忽略了,然后本该是该命令参数的文本被读作文本,同时当然忽略了花括号。
那么,我是否错误地使用了此功能,或者这是该特定功能中的错误?最重要的是,是否有其他方法可以创建内容行,而无需在文档中实际显示文本?
编辑:
CatchFileBetweenTags.sty 来源:
%%
%% This is file `catchfilebetweentags.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% catchfilebetweentags.dtx (with options: `package')
%%
%% This is a generated file.
%%
%% catchfilebetweentags : 2011/02/19 v1.1 - catchfilebetweentags : catch file between delimiters or tags
%%
%% 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
%%
%% This work consists of the main source file catchfilebetweentags.dtx
%% and the derived files
%% catchfilebetweentags.sty, catchfilebetweentags.pdf, catchfilebetweentags.ins,
%%
%% catchfilebetweentags : catchfilebetweentags : a new dimen corresponding to the remainder of the line
%% Copyright (C) 2010 by Florent Chervet <[email protected]>
%%
\NeedsTeXFormat{LaTeX2e}% LaTeX 2.09 can't be used (nor non-LaTeX)
[2005/12/01]% LaTeX must be 2005/12/01 or younger
\ProvidesPackage{catchfilebetweentags}
[2011/02/19 v1.1 - Catch file between tags (FC)]
\RequirePackage{etex,etoolbox,ltxcmds}
\RequirePackage{catchfile}
\globtoks\CatchFBT@tok
\newrobustcmd*\CatchFileBetweenDelims[4]{%
\begingroup
\edef\CatchFileBetweenDelims{\endgroup
\noexpand\@testopt
{\CatchFBT@Work{\noexpand#1}{#2}{#3}{#4}}
{\noexpand\makeatletter}%
}\CatchFileBetweenDelims
}% \CatchFileBetweenDelims
\newcommand\CatchFileBetweenTags{}
\begingroup
\@makeother\<%
\@makeother\>%
\@makeother\*%
\catcode`\: 14%
\@makeother\%:
\gdef\CatchFileBetweenTags#1#2#3{:
\CatchFileBetweenDelims\CatchFBT@tok{#2}{%<*#3>}{%</#3>}[\CatchFBT@sanitize]:
\CatchFBT@Final{#1}:
}:% \CatchFileBetweenTags
\endgroup
\newrobustcmd*\ExecuteMetaData[2][\jobname]{%
\CatchFileBetweenTags\CatchFBT@tok{#1}{#2}%
\global\expandafter\CatchFBT@tok\expandafter{%
\expandafter}\the\CatchFBT@tok
}% \ExecuteMetaData
\long\protected\def\CatchFBT@Work#1#2#3#4[#5]{%
\def\CatchFBT@setup{#5%
\long\def\CatchFile@Do####1#3{\CatchFBT@catchthepart}% discard before start-delim
\long\edef\CatchFBT@catchthepart####1#4{% capture until end-delim
\CatchFBT@tok{\endgroup
\CatchFBT@IsAToken#1
{\global\noexpand#1{####1}}
{\xdef\noexpand#1{\noexpand\unexpanded{####1}}}}%
\noexpand\CatchFBT@discardtherest}%
\long\expandafter\def
\expandafter\CatchFBT@discardtherest
\expandafter####\expandafter1\CatchFile@EOF{}%
\everyeof{#3#4}%
\everyeof\expandafter\expandafter\expandafter{%
\expandafter\the\expandafter\everyeof\CatchFile@EOF
\expandafter\the\expandafter\CatchFBT@tok\noexpand}}%
\CatchFileDef#1{#2}\CatchFBT@setup
}% \CatchFBT@Work
\def\CatchFBT@sanitize{%
\@sanitize
\@makeother\{%
\@makeother\}%
\endlinechar=`\^^J%
}% \CatchFBT@sanitize
\newrobustcmd*\CatchFBT@Final[1]{\@testopt
{\CatchFBT@Fin@l{#1}}{}%
}% \CatchFBT@Final
\def\CatchFBT@Fin@l#1[#2]{%
\begingroup
\endlinechar\m@ne \makeatletter #2%
\scantokens\expandafter{%
\expandafter\CatchFBT@tok\expandafter{\the\CatchFBT@tok}}%
\CatchFBT@IsAToken{#1}
{\global#1\expandafter{\the\CatchFBT@tok}}
{\xdef#1{\the\CatchFBT@tok}}%
\ifx\CatchFBT@tok#1\else\global\CatchFBT@tok{}\fi
\endgroup
}% \CatchFBT@Final
\def\CatchFBT@IsAToken#1{%
\expandafter\expandafter
\expandafter\CatchFBT@Is@Token
\expandafter\meaning\expandafter#1\string\toks
\\\\{first}{second}\\\\%
}% \CatchFBT@IsAToken
\expandafter\def\expandafter\CatchFBT@Is@Token
\expandafter#\expandafter1\string\toks#2#3\\#4#5#6\\\\{%
\csname ltx@%
\if\relax\detokenize{#1}\relax#5%
\else second\fi oftwo%
\endcsname
}% \CatchFBT@Is@Token
\endinput
%%
%% End of file `catchfilebetweentags.sty'.
要包含的文件示例:
\bmumansection{NAME}{%<*NAME>
Application.Connect
%</NAME>
}
\bmumansection{SYNOPSIS}{ %<*SYNOPSIS>
Application.Connect(string AuthenticationKey)
%</NAME>
}
\bmumansection{DESCRIPTION}{%<*DESCRIPTION>
This is a description of the Application.Connect function.
%</DESCRIPTION>
}
\bmumansection{RETURN VALUES}{%<*RETURN VALUES>
%unimportant text goes here
%</RETURN VALUES>
}
\bmumansection{POSSIBLE ERRORS}{%<*ERRORS>
%text here
%</ERRORS>
}
\bmumansection{SEE ALSO}{%<*SEE>
%unimportant text goes here
%</SEE>
}
我的“编译”文件(我不知道乳胶的具体术语)
\documentclass[12pt]{article}
\usepackage{bmu}
\setcounter{secnumdepth}{-1} %This hides the number 'header' in the 'mansections'
\bmupreamble{BMU Document}
\begin{document}
\maketitle
\bmuinclude{ApplicationConnect}
\end{document}
最后但并非最不重要的是我的 bmu.sty 包文件,我删除了所有不相关的命令
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{catchfilebetweentags}
\usepackage{verbatim}
\usepackage{hyperref}
%includes a standardized preamble for each document.
%Parameter 1 = title
\newcommand{\bmupreamble}[1]{
\title{{#1}}
\author{Me}
\date{26/01/2012}
}
\newcommand{\bmuref}[1]{
\hyperref[func:#1]{\ExecuteMetaData[functions/#1]{NAME}}
}
\newcommand{\bmuinclude}[1]{
\phantomsection
\addcontentsline{toc}{subsection}{ \ExecuteMetaData[functions/#1]{NAME}}
\label{func:#1}
\include{functions/#1}
}
\newenvironment{indentpar}[1]
{
\begin{list}{}
{\setlength{\leftmargin}{#1}}
\item[]
}
{\end{list}}
% Arguments:
% 1 - section name
% 2 - section indentation
% 3 - section contents
\newcommand{\mansection}[3]
{
% in case bookmarks are used
\subsection*{#1}
\begin{indentpar}{#2}
#3
\end{indentpar}
}
\newcommand{\bmumansection}[2]{
\mansection{#1}{1cm}{#2}
}
函数文件夹是我放置所有 API 函数描述的源文件的文件夹。
答案1
缺少反斜杠。通过添加反斜杠来更正此行:
\label{func:#1}
最小示例,包含一些虚构的代码和您的命令bmuinclude
,之前:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{hyperref}
\newcommand{\ExecuteMetaData}[2][]{<NAME>#1</NAME>}
\newcommand{\bmuinclude}[1]{
\phantomsection
\addcontentsline{toc}{subsection}{\ExecuteMetaData[functions/#1]{NAME}}
label{func:#1}
%\include{functions/#1}
}
\begin{document}
\tableofcontents
\section{Test}
\bmuinclude{ApplicationConnect}
\end{document}
它打印:
您可以在文本中看到一个不想要的标签 func:ApplicationConnect,这是由 引起的label{func:#1}
,因为这将是文本。添加反斜杠来创建\label
命令,然后不会显示文本。
关于\ExecuteMetaData
:也许在这个命令前添加一个\protect
,以防止在写入文件时过早扩展.toc
:
\addcontentsline{toc}{subsection}{\protect\ExecuteMetaData[functions/#1]{NAME}}