从 .dtx 生成 .sty 时执行哪段代码?

从 .dtx 生成 .sty 时执行哪段代码?

我试图了解如何.sty使用 来生成.dtx。假设我希望文件中包含以下代码.sty

\NewDocumentCommand{\foo}{ }{}
\NewDocumentCommand{\bar}{ }{}
\ifluatex
  \let\baz\foo
\else
  \let\baz\bar
\fi

我正在尝试按照以下方式进行操作:

%\begin{macro}{\foo}
%    \begin{macrocode}
\NewDocumentCommand{\foo}{ }{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\bar}
%    \begin{macrocode}
\NewDocumentCommand{\bar}{ }{}
%    \end{macrocode}
%\end{macro}
\ifluatex
  \let\baz\foo
\else
  \let\baz\bar
\fi

! LaTeX cmd Error: Command '\bar' already defined.但我收到两个错误:! Undefined control sequence. l.25 \ifluatex

据我了解,发生这种情况是因为代码被执行了,而我只是希望它出现在.sty.pdf

完成 MWE:

% \iffalse meta-comment
%<*internal>
\iffalse
%</internal>
%<*internal>
\fi
\def\nameofplainTeX{plain}
\expandafter\begingroup
%</internal>
%<*install>
\input docstrip.tex
\keepsilent
\askforoverwritefalse
\usedir{./}
\generate{
  \file{\jobname.sty}{\from{\jobname.dtx}{package}}
}
%</install>
%<install>\endbatchfile
%<*internal>
\expandafter\endgroup
%</internal>
%<*driver>
\documentclass{ltxdoc}
\usepackage{lmodern}
\usepackage{\jobname}
\usepackage[numbered]{hypdoc}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
    \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
%\begin{macro}{\foo}
%    \begin{macrocode}
\NewDocumentCommand{\foo}{ }{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\bar}
%    \begin{macrocode}
\NewDocumentCommand{\bar}{ }{}
%    \end{macrocode}
%\end{macro}
\ifluatex
  \let\baz\foo
\else
  \let\baz\bar
\fi
%\Finale

答案1

看来您想tex mypac.dtx充当安装文件并提取mypac.stypdflatex mypac.dtx排版文档。

% \iffalse
%<*switch>
\ifx\documentclass\undefined
%</switch>
%<*ins>
\input docstrip
\generate{\file{mypac.sty}{\from{mypac.dtx}{package}}}
\expandafter\endbatchfile
%</ins>
%<*switch>
\fi
%</switch>
%<*driver>
\documentclass{article}
\usepackage{doc}
\begin{document}
\title{The \textsf{mypac} package}
\DocInput{mypac.dtx}
\end{document}
%</driver>
% \fi
%
%
%\begin{macro}{\foo}
%    \begin{macrocode}
\NewDocumentCommand{\foo}{ }{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\bar}
%    \begin{macrocode}
\NewDocumentCommand{\bar}{ }{}
%    \end{macrocode}
%\end{macro}
%\begin{macro}{\baz}
%    \begin{macrocode}
\ifluatex
  \let\baz\foo
\else
  \let\baz\bar
\fi
%    \end{macrocode}
%\end{macro}

制作

在此处输入图片描述

pdflatex

%%
%% This is file `mypac.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% mypac.dtx  (with options: `package')
%% 
%% IMPORTANT NOTICE:
%% 
%% For the copyright see the source file.
%% 
%% Any modified versions of this file must be renamed
%% with new filenames distinct from mypac.sty.
%% 
%% For distribution of the original source see the terms
%% for copying and modification in the file mypac.dtx.
%% 
%% This generated file may be distributed as long as the
%% original source files, as listed above, are part of the
%% same distribution. (The sources need not necessarily be
%% in the same archive or directory.)
\NewDocumentCommand{\foo}{ }{}
\NewDocumentCommand{\bar}{ }{}
\ifluatex
  \let\baz\foo
\else
  \let\baz\bar
\fi
\endinput
%%
%% End of file `mypac.sty'.

tex mypac.dtx

相关内容