1

I am trying to understand how to generate .sty using .dtx. Suppose I want the following code to be in the .sty file:

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

I am trying to do it in the following way:

%\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

But I get two errors: ! LaTeX cmd Error: Command '\bar' already defined. and ! Undefined control sequence. l.25 \ifluatex

As I understand, this happens because the code is executed, while I just want it to appear in the .sty in .pdf.

Complete 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
2
  • presumably \ifluatex \let\baz\foo \else \let\baz\bar \fi is intended for the package so should be in macrocode Commented Oct 23, 2022 at 21:40
  • what is the intention of the group here? \expandafter\endgroup the expandafter is acting on \documentclass ?? Commented Oct 23, 2022 at 21:43

1 Answer 1

4

It appears you want tex mypac.dtx to act as an install file and extract mypac.sty and pdflatex mypac.dtx to typeset the documentation.

% \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}

Producing

enter image description here

with pdflatex and

%%
%% 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'.

with tex mypac.dtx

7
  • 1
    I think there was a copy-paste mistake for the .dtx code? It is now the same as the .sty.
    – Marijn
    Commented Oct 24, 2022 at 7:44
  • @Marijn oops, thanks for the note:-) I'll fix Commented Oct 24, 2022 at 8:18
  • Can you please explain what does <switch> tag do? Commented Oct 24, 2022 at 10:13
  • 2
    @MichaelFraiman it is an arbitrary name that is not package (I forgot that guard with the first post) You can remove the four switch guards and it still works but the unguarded \ifx\documentclass\undefined\fi would "fall through" and end up in the generated .sty which is harmless but pointless test. Guarding it with anything means it is not copied by the \generate{\file{mypac.sty}{\from{mypac.dtx}{package}}} Note this (and the ifx tricks) are only needed so that the same dtx can be used (with plain tex) as a .ins file, rather than say tools.ins as we do in latex-tools Commented Oct 24, 2022 at 10:25
  • @DavidCarlisle ok, I see. So everything that is unguarded or guarded by <package> ends up in the sty? Also, I have a problem with generating readme file: I get the code of the package itself in the file. Would you be king to help with that? Commented Oct 24, 2022 at 10:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .