The central question is how can i convert a part of a dtx file to straight LaTeX.

In learning more about a package, I want to play with an example from a dtx? The output shows up in the pdf. The dtx, however has the code layed out like:

% \latexcommand

% some text that prints

% \morelatexcode

I cannot determine what packages are needed and why just removing the "% " does not work. The code i want to play with is from longtable.dtx, but there are other dtx files that have interesting examples that i would like to play with.


2 Answers 2


I answered your question about longtable. The reason you can't just pull pieces of code out of a .dtx and expect them to work is the same reason you can't pull pieces of code out of any other TeX file and expect it to work: it can depend on the things before it.

It's impossible to give a general solution, but the trick is to just run TeX and see what error message you get which is almost certainly going to be an undefined control sequence. Then you find the piece of code in the .dtx that defines the control sequence and copy that over. Repeat until it compiles.

  • Also, somewhere at the top of the .dtx there usually is the preamble of the document (enclosed with <*driver>).
    – Caramdir
    Apr 28, 2011 at 16:51

Here are the steps I used.

  • Copy .dtx to .tex. My ex:
cp longtable.dtx longtable_eg.tex

Edit longtable_eg_dtx.tex

  • Replace "^( *)\" with "%%% \0\" and "^( +)" with "%%% \0" This comments all dtx commands.
s/^( *)\\/\%\%\% \0\\/
s/^( +)/\%\%\% \0/
  • Insert lines at the top

for lines like after the initial \if /fi pairs:

%%% \documentclass{ltxdoc} 
%%% \usepackage{longtable} 
%%% \begin{document} 
  • Insert \end{document} at end.

  • Uncomment section not in \if - \fi pair, near the top that each line starts with "% "

  • Compile (at least twice)

  • View results

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.