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.

  • 1
    Note sure but this answer How can i pull an example out of a dtx file? may be of interest.
    – N.N.
    Sep 28 '11 at 8:41
  • @Pythonee Do you want to extract the text of the documentation or the code which is documented? The two are different, as as Herbert has pointed out getting the code out is what the ideal of a .dtx file is.
    – Joseph Wright
    Sep 28 '11 at 10:18
  • @Joseph Writht, I want to extract the text of documentation to another tex file. I just want to learn how to write manual like that. So I want to focus on tex code not the package implementation.
    – pythonee
    Sep 29 '11 at 7:29

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
% Description
% ...
% Implementation
%   \begin{macrocode}
%   \end{macrocode}
% ...
% END of file



You can and should still use the ltxdoc class.


Usually package.dtx is accompanied by package.ins and running LaTeX on the latter extracts the code (commonly in package.sty, but in some cases other files are created).

In some cases package.ins is not present; in these cases,

pdflatex package.dtx

will typeset the documentation and extract the code; however sometimes it's necessary to run

tex package.dtx

for extracting the code (it's the case with H. Oberdiek's packages).

However, the purpose of package.dtx is not only to provide the package documentation, but also comments to the code.

If running LaTeX on package.dtx doesn't typeset the commented code, you can write a file ltxdoc.cfg in the working directory containing the line


and run LaTeX on package.dtx again.

  • In case e.g. of the dtx-file in the oberdiek bundle tex file.dtx unpacks the code while latex file.dtx generates the documentation. The dtx-files contains at the start more precise instructions. Sep 28 '11 at 8:44
  • I get an error complaining that \AlsoImplementation is undefined when I try the ltxdoc.cfg trick. What am I doing wrong? I was trying to figure out how to extract the documentation for pullquote...
    – cfr
    Mar 17 '14 at 4:32
  • @cfr Difficult to say without knowing where to find the .dtx file for experimenting.
    – egreg
    Mar 17 '14 at 7:55

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.