How can other programming languages and tools be used to create content for TeX documents?

Org-mode

In Org-mode, which is a mode available for Emacs, you can mix text with output from code blocks in many different languages and export it all to LaTeX. It is possible to use different languages in the same document and also to send the output from one code block to another. This makes possible for interaction between different languages.

Here is a very simple example in which the first code block is shown as well as its output and the second code block uses the output from the first code block. In this example the code blocks are written in Emacs Lisp. The following is the content of an Org-mode buffer:

* Variable passing

** Example 1

In the following code the variable =foo= is set to the value bar. Since the function =setq= returns the last value it will return bar as shown below.
#+NAME: example-one
#+BEGIN_SRC emacs-lisp :exports both
(setq foo "bar")
#+END_SRC

** Example 2

In the following code the value of =foo= is set to the concatenation of the value of =x= and =x=. The value of =x= is set to the value returned in the example above (this happens in Org-mode).
#+NAME: example-two
#+BEGIN_SRC emacs-lisp :var x=example-one :exports both
(setq foo (concat x x))
#+END_SRC

When pressing C-c C-e d Org-mode will export to LaTeX, compile it and show the resulting document. Here is part of the LaTeX output, note that the Lisp code is followed by its returned value:

\section{Variable passing}
\label{sec-1}
\subsection{Example 1}
\label{sec-1-1}


In the following code the variable \texttt{foo} is set to the value bar. Since the function \texttt{setq} returns the last value it will return bar as shown below.

\begin{verbatim}
(setq foo "bar")
\end{verbatim}




\begin{verbatim}
 bar
\end{verbatim}
\subsection{Example 2}
\label{sec-1-2}


In the following code the value of \texttt{foo} is set to the concatenation of the value of \texttt{x} and \texttt{x}. The value of \texttt{x} is set to the value returned in the example above (this happens in Org-mode).

\begin{verbatim}
(setq foo (concat x x))
\end{verbatim}




\begin{verbatim}
 barbar
\end{verbatim}

And here is part of the resulting document:

Output of example of variable passing

This example is very simple and it does not show the full potential of this approach. It is possible to make much more complex interactions between languages and tools. Both of the following articles are written in Org-mode and exported to LaTeX and they show complex examples of how it can be used to analyze data and such:

  • A Multi-Language Computing Environment for Literate Programming and Reproducible Research. Contains three examples. The first example gets the values of Pascal's triangle using Python, draws it using dot and tests if it is correct using Emacs Lisp. The second is an example of literate programming for C. The third example collects temperatures using shell commands, creates a database using sqlite, analyzes the data using R and draws a graph of it.

  • Active Documents with Org-Mode. Contains an example that collects baseball data using shell commands, analyzes it using Python, awk and R and draws a diagram. The source for this article is available at https://raw.github.com/eschulte/CiSE/master/org-mode-active-doc.org.


Bashful

The bashful package is specifically designed for including command line session output in documents, so can be used to display the output of any other program that produces command line output.

Quoting from the user manual:

...bashful provides a convenient interface to TEX’s primitive \write18—the execution of shell commands from within your input files, also known as shell escape. Text between \bash and \END is executed by bash, a popular Unix command line interpreter. Various flags control whether the executed commands and their output show up in the printed document, and whether they are saved to files.

Although provisions are made for using shells other than bash, this package may not operate without modifications on Microsoft’s operating systems.

Note that this requires -shell-escape option to be specified.

Example:

Here is a demonstration using basic bash commands. The documentation provides more elaborate examples.

enter image description here

The first line of the bash script file begins with a %, so I started the listings form line 2, and left the first line blank. When you initiate the \bash command, you provide a file name for the .sh bash file and the output file. Then use lstinputlisting to reincorporate the contents of that file back into the .tex file.

\documentclass{standalone}
\usepackage{xcolor}
\usepackage{bashful}
\usepackage{listings}

\lstdefinestyle{BashInputStyle}{
  language=bash,
  firstline=2,% Supress the first line that begins with `%`
  basicstyle=\small\sffamily,
  numbers=left,
  numberstyle=\tiny,
  numbersep=3pt,
  frame=tb,
  columns=fullflexible,
  backgroundcolor=\color{yellow!20},
  linewidth=0.9\linewidth,
  xleftmargin=0.1\linewidth
}

\lstdefinestyle{BashOutputStyle}{
  basicstyle=\small\ttfamily,
  numbers=none,
  frame=tblr,
  columns=fullflexible,
  backgroundcolor=\color{blue!10},
  linewidth=0.9\linewidth,
  xleftmargin=0.1\linewidth
}


\begin{document}
\bash[verbose,scriptFile=hello.sh,stdoutFile=hello.tex]

echo "Hello World!"
echo "Today is" `date`
echo ""
echo "Disk usage is:"
df
\END
\par\noindent
Executing the following code in \textbf{bash}
\lstinputlisting[style=BashInputStyle]{hello.sh}
%
yields the following output:
\lstinputlisting[style=BashOutputStyle]{hello.tex}
\end{document}

For ConTeXt, I have written a module filter that allows you to easily call external programs and include the result back in TeX. The main feature of this module, as opposed to manual \write18 calls are:

  • It provides a nice key-value driven syntax. See the README file on github for details.
  • It caches the results and reruns the external program only if the input has changed.

Using this module, you can use any external program to create content to be included in TeX.

For example, you can use the filter module to show the input and output of programs side by side.

\usemodule[filter,vim]                                                                                                                 

\definevimtyping[prettyRUBY][syntax=ruby]
\defineexternalfilter
  [RUBY]
  [
    filter={ruby \externalfilterinputfile\space > \externalfilteroutputfile},
    cache=yes, % Do not rerun the program if the file has not changed
    readcommand=\TypesetAndPrint,
  ]

\def\TypesetAndPrint#1%
    % The filter module expects #1 to be the 
    % file to read. But since we want to prettyprint
    % the input as well, I ignore #1 and use
    % `\externalfilterinputfile` and `\externalfilteroutputfile`
    % instead
    %
    % A simple way to typeset the output is as follows
    % {\typeprettyRUBYfile{\externalfilterinputfile}%
    %  \blank
    %  \typefile{\externalfilteroutputfile}}
    % but we use a slightly more elaborate scheme and show the code
    % and output side by side
    {\startlinecorrection
     \startcombination[2]
        {\typeprettyRUBYfile{\externalfilterinputfile}}{Ruby program}
        {\typefile{\externalfilteroutputfile}}{Output}
     \stopcombination
     \stoplinecorrection}

which can be used as

\starttext
A ruby program
\startRUBY
puts "Hello World"
\stopRUBY
\stoptext          

and gives

enter image description here

Note that I am using the vim module to pretty-print the source code. The same approach can be used to, for example write content in markdown and use pandoc to convert it to TeX (see this example) or use R to generate graphics (see this example).