TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The docstrip package mentions that it can be used to strip files of computer languages other than (La)TeX. I am looking to find a way to document other languages, for example JavaScript in a literate way.

    Some comment lines
    here and here.
function Apple (type) {
    this.type = type;
    this.color = "red";
    this.getInfo = function() {
        return this.color + ' ' + this.type + ' apple';

Can someone post a minimal example (MWE) for such usage? Any other strategy one can use?

share|improve this question
Another strategy for many languages is Doxygen, but you probably already know it. – Andrey Vihrov Jan 20 '12 at 10:06
If you are on a Unix system I can generally recommend the NoWeb literate programming system that is geared towards TeX and LaTeX and otherwise language agnostic. – Christian Lindig Jan 20 '12 at 10:12
@AndreyVihrov Thanks, I am familiar with the Package but experimenting to see if I could get better results with (La)TeX. – Yiannis Lazarides Jan 20 '12 at 14:56
@ChristianLindig I am on a windows system at work and will need an armoured tank to convince anyone to move to Unix;) – Yiannis Lazarides Jan 20 '12 at 14:57
up vote 7 down vote accepted

Perhaps the easiest demo to give here is one that is actually in use: using DocStrip and the .dtx format to contain both TeX and Lua code. I will use the 'all-in-one' .dtx format which I use for my own packages: this is optional, but makes it a little easier to write a demonstration.

The key idea here is to alter what is used as a start-comment string, so that LaTeX comments start %% while Lua comments start --. The comment string is stored as \MetaPrefix, and this is included in the pre- and post-amble via an \edef. So what you need to do is \let\MetaPrefix\relax, then define the pre- and post-amble you want, and only then set it to the appropriate strings. There is one set up for LaTeX: \DoubleperCent. For Lua, it's easy enough to type things in directly.

% \iffalse meta-comment
Some information here, perhaps :-)
\input docstrip.tex


Some preamble text
Some postamble text

\def\MetaPrefix{-- }

% \fi
%    \begin{macrocode}
demo        = { }
demo.module = { name = "demo" }
%    \end{macrocode}
%    \begin{macrocode}
%    \end{macrocode}

If you run tex <filename>.dtx, you will extract out the .lua and .sty files from the above, and can then see that the extraction has worked as required. Running latex <filename>.dtx will also create a .ins file, whcih you might want to use separately. That I know of, multi-line comments are not possible with DocStrip, so you have to use the single-line comment method for the target language.

share|improve this answer
This is fine if one needs a short file with Lua Code. What I had in mind was to produce the Lua Code documentantion. Say you had a two page module and you want to split the code in the write-up and then produce the single file for .lua. Pretty much, like the .sty except for it to be .lua. You think this is possible? – Yiannis Lazarides Jan 20 '12 at 10:50
@YiannisLazarides Splitting up the code works whatever language your are using, provided that you have the appropriate guards. I've put them at the start and end of the code block, but they could be at the start and end of the entire implementation part of the .dtx (as I normally do for guards for latex). – Joseph Wright Jan 20 '12 at 10:57
Oops! I didn't realize. I will give it another try. – Yiannis Lazarides Jan 20 '12 at 11:01

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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