Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "switex"

Title:Literate programming with LaTeX and Prolog
Rating:Not rated. Create the first rating!
Latest version:0.0.7
SHA1 sum:d4c2bbd692ea1d67178f7252ea9b50cfb04ccf70
Author:Samer Abdallah <s.abdallah@ucl.ac.uk>
Download URL:https://github.com/samer--/switex/raw/master/release/switex-0.0.7.tgz


No reviews. Create the first review!.

Details by download location



LaTex and Prolog code to allow calling Prolog from LaTeX

This package allows LaTeX code to call Prolog to generate LaTeX content which is inserted into the document at the point where Prolog is called. It works by setting up a conversation between two running processes: pdflatex on one side and SWI Prolog on the other, communicating via their standard input and output streams. This system was inspired by Jonathan Fine's QATEX system: http://qatex.sourceforge.net/


  • SWI Prolog installation
  • TeX installation

Implementation details

The Prolog side of the system is implemented in prolog/switex.pl, with latex.pl supplying a DCG for writing out LaTeX code. The TeX side is implemented in tex/switex.sty, which writes requests for content on its standard output stream, and expects an answer on standard input. Currenty, the Prolog side also communicates on standard input and output streams. The two process are setup up with communicating pipes using a bash coprocess (see coproc in bash documentation). This setup is a little clumsy and fragile at the moment, so at some point, it will be replaced with Prolog code to start the pdflatex process.

In use, pdflatex produces a fair amount of output, which is ignored. When a Prolog computation is requested from the LaTeX side using the \swi macro, the request is written to standard output in the form


with <goal> replaced by a valid Prolog goal terminated by a period as is normal in Prolog. The Prolog process recognises this an runs <goal>, collecting anything written to current\_output Prolog stream. It then replies to LaTeX with

{<collected output>}<return>

LaTeX receives this inserts <collected output> at the point where the \swi macro was called, almost as if it were written there in there in the first place, though there are certain limitations. One of these is that <collected output> cannot involve any of the verbatim macros that various LaTeX packages provide.


If you install the Prolog side of things as an SWI package, then switex.pl and latex.pl should be available as Prolog modules.

The shell script in bin/switex should be copied or linked to somewhere on your PATH.

The TeX file tex/switex.sty should be copied or linked to somewhere in your TeX tree.


Go into the test directory and run the switex command against test.tex:

$ switex test.tex

This should produce test.pdf, which should look similar to http://github.com/samer--/switex/raw/master/test/test-sample.pdf


The communication between TeX and Prolog is somewhat fragile. In particular, if the Prolog query written out by \swi or \swiverb appears to be an incomplete Prolog term, then the Prolog side will hang waiting for its completion, leading to a deadlock. Similarly, if the Prolog side writes back an incomplete TeX group, then TeX will hang waiting for its completion. Currently, there is no way to recover from this situation, but typing Ctrl-C at the terminal or sending a SIGINT will kill both processes.

Contents of pack "switex"

Pack contains 7 files holding a total of 11.6K bytes.