# Using edef'd text with doctex pre/postamble

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

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


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.

}%

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

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,
}%
\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}%
{%
% 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
{%
{%
\usepreamble\thispreamble
\usepostamble\listoffiles
}%
}%


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

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


Complete .ins file (the fragments above joined):

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

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

This is a generated file.

}%

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

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,
}%
\endgroup
%
\generate
{%
\file{\projectname.cls}%
{%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{class}%
}%
}%
%
\generate
{%
{%
\usepreamble\thispreamble
\usepostamble\listoffiles
}%
}%
%
\generate
{%
\file{luacode.lua}%
{%
\usepreamble\thispreamble
\usepostamble\listoffiles
\from{\projectname.dtx}{luacode}%
}%
}%
%
\endbatchfile

• 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

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
======================================================================\newline
\jobname (Version \fileversion - \filedate)              \newline
======================================================================}
\def\expandamble{\declarepreamble\metainfo}
\expandafter\expandamble\metainfo
\endpreamble
\usepreamble\metainfo


The second then overrides the preamble for the readme.

\generate{