[Tex/LaTex] How to other programming languages and tools be used to create content for TeX documents

big-listscriptsworkflow

When writing about programming languages or tools one often wants to include output from them in the document. Then it is useful if such output can be created as the document is compiled. For instance, if one describes a Lua function, then one may want the returned value of that function to be shown in the document. In this case it would be useful if the Lua function could be evaluated as the document is compiled so that the returned value is automatically included in the text.

Another situation of when one wants to use functionality external to TeX and friends is when they are not fit to create or manipulate the content one wants in the document. For example, TeX and friends might not be ideal for analyzing data.

These are two examples of when content for TeX documents are created by other programming languages and tools. How can such approaches be effectively implemented?

Best Answer

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: