[Tex/LaTex] convert a dtx file to tex file

dtx

I know dtx file is literate programming style file. I also know that I can compile it with pdflatex to generate pdf file. But I wonder could I just extract the tex code to a independent file? I think I can learn some thing from the tex source.

Best Answer

A DTX file is already a LaTeX file (sometimes called .tex file), however it uses some special things. It contains the description as comment lines and the package source code. It contains a "driver" preamble and body which includes the same file again with the % comment character disabled, so that the originally commented-out part is now read as normal LaTeX code. You can have a look at the description part simply by looking at the file, if you don't mind the comment style.

If you want to turn it into a normal LaTeX file, simply remove the comment % characters from the code and completely delete the implementation part. Then I would change the "driver" head part to not include the file but place the description body directly there by removing the \DocInput{..} and moving the \end{document} to the end of the file.

So basically change:

% \iffalse
%<*driver>
\documentclass{ltxdoc}
\usepackage{mypkg}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{mypkg.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
%\fi
% Description
% ...
% Implementation
%   \begin{macrocode}
%<*package>
code
%</package>
%   \end{macrocode}
% ...
% END of file

to:

\documentclass{ltxdoc}
\usepackage{mypkg}
\begin{document}
  Description
  ...
\end{document}

You can and should still use the ltxdoc class.

Related Question