I'm writing a tool to streamline some typesetting jobs, and I am having trouble distinguishing between plain Tex and ConTeXt.

LaTeX is straightforward to recognise by the \documentclass command (or \documentstyle for old documents), however I am struggling to find a "smoking gun" to differentiate ConTeXt source from plain TeX source.

It seems that the \starttext . . . \stoptext pair is common in ConTeXt, but not guaranteed. Is there something better, or a combination of things that are better?

I understand that due to TeX's nature it is impossible write a filter to do this 100% accurately, but 90-95% accuracy will still be a great help.


Any \start... is good enough to identify something as ConTeXt and not Plain, if you do not care about old-school AMSTeX files.

If you want complete commands, there are quite a few top-level document delimiters in ConTeXt:

The usual document delimiters

Special page environments

These create individual pages, but of course such a page may constitute an entire document

  • \startTEXpage — a page that exactly fits the TeX output it contains
  • \startMPpage — a page that exactly fits the MetaPost graphic it contains

Parts of the project system

Other auxiliary files

  • \startmodule — modules and environments are the same, really. In general, though, environments are written ad hoc, while modules are written to be reused (by others).
  • \startxmlsetups — ConTeXt can take XML as input, and a file with XML setups that tell it how to translate the XML to TeX.

Incidentally, LaTeX documents need not contain a {document} environment. LaTeX will generate an error if any output is generated before \begin{document}, which Context does not; but a document without output can still be valid (compile without error), of course. If you access the primitive \end command with \makeatletter\@@end, you can end the document without resorting to LaTeX's \end{document}.

Note from Charles I've made this CW, since by now most of the knowledge it contains comes from the comments below.

  • 3
    Not quite true. There are more environments that can delimit the body text from the setup area. These are valid ConTeXt documents: \startdocument FooBar \stopdocument, \startcomponent * FooBar \stopcomponent or \startproduct * FooBar \stopproduct. – Marco May 22 '13 at 7:53
  • @Marco: I'll delete this answer shortly, and maybe undelete it if I fix it. – Charles Stewart May 22 '13 at 7:54
  • 6
    Any '\start...' is good enough to differentiate between plain and ConTeXt, if you do not care about old-school amsTeX files. – Taco Hoekwater May 22 '13 at 7:57
  • 1
    Apparently he introduced a new command with the same name as the old one a few years later (see beta announcement). – Marco May 22 '13 at 8:47
  • 1
    Keep in mind that ConTeXt has a multi-lingual interface. So, if I use the Dutch or the German or the French interface, none of the above commands will be there! – Aditya Jul 3 '13 at 23:11

FWIW, vim uses the following logic to classify files that have a .tex extension.

  • Ignore all lines at the beginning of the file that start with a comment.
  • Search the next 1000 lines for the following patterns:

    let lpat = 'documentclass\>\|usepackage\>\|begin{\|newcommand\>\|renewcommand\>'
    let cpat = 'start\a\+\|setup\a\+\|usemodule\|enablemode\|enableregime\|setvariables\|useencoding\|usesymbols\|stelle\a\+\|verwende\a\+\|stel\a\+\|gebruik\a\+\|usa\a\+\|imposta\a\+\|regle\a\+\|utilisemodule\>'
  • If the file contains either of the patterns, classify it with LaTeX or ConTeXt.

  • Otherwise, classify it as g:tex_flavor (which defaults to plain TeX, but can be overridden by the user).

Source: s:FtTex() function in filetypes.vim

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.