1

I'm updating my DocTeX template(s) given the model DTX file by Joseph Wright, who includes a readme guard from which the readme.txt is generated.

The \delcarepre/postamble macros from DocTeX prefix \MetaPrefix (Simply two % signs) to each line in the pre/postamble. One may get around this by \edef'ing their own pre/postamble. I was wondering if I could \edef my pre-amble the pass it to \declarepreamble\BLOB\endpreamble there after, allowing two versions one with \MetaPrefixed lines and one without.

\edef\metadata{%
======================================================================^^J%
\space\space\space\space\space\space\space\space\space%
\space\space\space\space\space\space\space\space\space%
    \jobname\space(Version\space\fileversion\space-\space\filedate)   ^^J%
----------------------------------------------------------------------^^J%
Author \space: <AUTHOR(S)> <E-MAIL>                                   ^^J%
Licence : LaTeX Project Public Licence (LPPL) v1.2 or later           ^^J%
\space\space\space\space\space\space\space\space\space\space\space\space%
           (See https://www.latex-project.org/lppl.txt)               ^^J%
======================================================================^^J}
\def\expamble{\declarepreamble\metainfo}
\expandafter\expamble\metadata
\endpreamble
\usepreamble\metainfo

That is can I split \metadata at the ^^Js and pass the list of lines into the \declarepreamble\metainfo...\endpreamble block for it to process as it sees fit ?

1

I don't know if this answer will be useful for because they deviate from the path you chose. But, if I understood your question correctly, I think this is easier than replacing text inside a macro. This is how I set things up in a class I wrote.

First some preliminaries. Nothing interesting here:

\catcode`\@=11%
\def\projectname{test}%
\input docstrip.tex
\keepsilent
\askforoverwritefalse

Now we'll set up the environment to make the definition of the preambles/postambles. We'll make \^^M active so when we pass that to docstrip's preamble/postamble system it will manage to add the \MetaPrefix:

\begingroup
\catcode`\^^M=\active%
\obeyspaces%

(I used \obeyspaces to allow alignment of text spanning multiple lines, like in the \listoffiles macro below)

Now we (g)define whichever preamble/postamble text we want:

\gdef\copyright{%
\ds@heading% This is file... generated with the docstrip utility.
\ReferenceLines% The original source files were: ...

This is a generated file.

Copyright 2019 by Phelype Oleinik
}%
\gdef\lppllicense{%

This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c 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

and version 1.3c or later is part of all distributions of LaTeX version
2005/12/01 or later.
}%
\gdef\listoffiles{%

This work consists of the files \projectname.dtx, and
                                \projectname.ins,
and the derived files           \projectname.cls,
                                README.txt.%
}%
\endgroup

You can split these in multiple parts, and later you can concatenate them. For example, I have a \copyright and a \lppllicense, later I do \def\thispreamble{\copyright\lppllicense} to join them for a given file.

Now that we defined the pieces of text we'll use we can \generate the files using that. For instance, the class (or package, or whatever you're doing) file can be generated with:

\generate
  {%
    \file{\projectname.cls}%
      {%
        % Joining \copyright and \lppllicense
        \def\thispreamble{\copyright\lppllicense}%
        % Getting docstrip to use that
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{class}%
      }%
  }%

here the \MetaPrefix (currently %%) is inserted at every line, so everything will go commented to your class/package file.

What if I want a readme file without %%? You can simply \def\MetaPrefix{}. But this inserts, by default, a leading space, so we can make \MetaPrefix remove a space defining it with an argument: \def\MetaPrefix#1{}.

\generate
  {%
    \file{README.txt}%
      {%
        \def\MetaPrefix#1{}% Remove MetaPrefix for readme
        \def\thispreamble{\copyright\lppllicense}%
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{readme}%
      }%
  }%

If you had, for instance, some Lua code as well you could \def\MetaPrefix{--}:

\generate
  {%
    \file{luacode.lua}%
      {%
        \def\MetaPrefix{--}%
        \def\thispreamble{\copyright\lppllicense}%
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{luacode}%
      }%
  }%

Complete .ins file (the fragments above joined):

\catcode`\@=11%
\def\projectname{test}%
\input docstrip.tex
\keepsilent
\askforoverwritefalse

\begingroup
\catcode`\^^M=\active%
\obeyspaces%
\gdef\copyright{%
\ds@heading% This is file... generated with the docstrip utility.
\ReferenceLines% The original source files were: ...

This is a generated file.

Copyright 2019 by Phelype Oleinik
}%
\gdef\lppllicense{%

This work may be distributed and/or modified under the conditions of the
LaTeX Project Public License, either version 1.3c 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

and version 1.3c or later is part of all distributions of LaTeX version
2005/12/01 or later.
}%
\gdef\listoffiles{%

This work consists of the files \projectname.dtx, and
                                \projectname.ins,
and the derived files           \projectname.cls,
                                README.txt.%
}%
\endgroup
%
\generate
  {%
    \file{\projectname.cls}%
      {%
        \def\thispreamble{\copyright\lppllicense}%
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{class}%
      }%
  }%
%
\generate
  {%
    \file{README.txt}%
      {%
        \def\MetaPrefix{}% Remove MetaPrefix for readme
        \def\thispreamble{\copyright\lppllicense}%
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{readme}%
      }%
  }%
%
\generate
  {%
    \file{luacode.lua}%
      {%
        \def\MetaPrefix{--}% Remove MetaPrefix for readme
        \def\thispreamble{\copyright\lppllicense}%
        \usepreamble\thispreamble
        \usepostamble\listoffiles
        \from{\projectname.dtx}{luacode}%
      }%
  }%
%
\endbatchfile
  • 1
    I tried adapting your solution to my style files and it worked quite well, the style files would include %% LINES and the read me normal LINES, solving my original problem. However, since my *.ins code is embedded within the *.dtx file I did have some trouble running the *.dtxitself through PDFLaTeX which would complain that the ===...=== line did not appear after \begin{document}. Using the answer from a the related question I could stitch everything together and have included this as another answer. Thanks for the help !!! – Carel Jan 15 at 18:45
1

Combining the answer of Phelype Oleinik and that of David Carlisle and their helpful comments I was able to do the following.

The first block sets the preamble universally.

\obeyspaces
\def\metadata{%
======================================================================\newline
             \jobname (Version \fileversion - \filedate)              \newline
======================================================================}
\def\newline{^^J\@percentchar\@percentchar\space}\xdef\metainfo{^^J\@percentchar\@percentchar\space\metadata}
\def\expandamble{\declarepreamble\metainfo}
\expandafter\expandamble\metainfo
\endpreamble
\usepreamble\metainfo

The second then overrides the preamble for the readme.

\generate{
 \def\newline{^^J}\xdef\metainfo{\metadata^^J^^J}
 \usepreamble\metainfo
 \nopostamble
 \file{\jobname/README.txt}{\from{\jobname.dtx}{readme}}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.