[Tex/LaTex] Amusing exercise – ouroboros file – Output of TeX includes itself recursively ad infinitum

bashfulfungraphicspdfpagesrecursion

Clarification: The question may be related to the the quine fun question, but IMHO it is quite distinct.

The question here to write an "ouroboros" in TeX, i.e., a file that includes itself as a figure. In other words, the output should include in it a figure which presents the figure. In a sense, this is like a fractal, since the figure would show a the entire document, which hopefully is only one page. Now, in this figure, you would see a scaled down image of the output, which would have in it a figure.

In other words, the "ouroboros" is not a "quinn", but rather something like this image:

enter image description here

or this one:

this one

Here is my attempt to do that:

\documentclass[twocolumn,10pt]{article}
\usepackage{pdfpages}
\usepackage{lipsum}
\title{The Ouroboros}
\author{U. Roboros}
\begin{document}
  \maketitle
  \lipsum[1]
  \par
  \begin{figure}
    \includepdf[frame,scale=0.4]{ouroboros.pdf} %
    \end{figure}
  \par
  \lipsum[3]
  \par
  \lipsum[2]
\end{document}

The idea is that in each run of the file through LaTeX, it would present an image, which is included as a figure in the next run.

This can be thought of as an amusing exercise, but a solution could be useful in writing documentation for TeX files; the idea being that you generate a LaTeX file,
e.g, using bashful, compile it, and then include it as a figure.

The above solution cannot work, since the moment you run latex, the previous PDF file is erased. Perhaps a solution would be to have two files, each including the other. Another option is to copy the input into a temporary file, compile it, and then include it.

Another issue with the above is that pdfpags seems, at least for me, to be in conflict with the figure environment.

Note that the some of the answers to Self-replicating (La)TeX document
do similar things, let the file include itself in some sense.
Here, this should be done at the rendered level, rather than on textual basis.

Best Answer

EDIT

... include a more sinister version...

One File To Rule Them All...

I'm not sure if this is what you want. Strictly speaking, this file does not include itself. It includes its own output. This is what your images suggest you want but your description is ambiguous.

Anyway, since you mention using this with bashful, I thought I might as well use bashful. This needs to be compiled with shell escape for obvious reasons. But, if you are using bashful, then you obviously have to compile in a way which bashful requires and will therefore be using shell escape anyway.

A simple example:

\documentclass{article}
\usepackage{graphicx,bashful}
\pagestyle{empty}
\begin{document}

\bash
[[ -f "prawf3.pdf" ]] && mv -f prawf3.pdf ouroboros.pdf
\END

\centering This Is The File To End All Files

\IfFileExists{ouroboros.pdf}{\frame{\includegraphics[width=.9\textwidth]{ouroboros}}}{\relax}

\end{document}

A more sinister example using TiKZ:

\documentclass[a4paper]{article}
\usepackage{tikz,graphicx,bashful}
\usepackage[scale=.95]{geometry}
\usetikzlibrary{decorations.text,calc}
\pagestyle{empty}
\begin{document}
\bash
[[ -f "prawf3.pdf" ]] && mv -f prawf3.pdf ouroboros.pdf
\END
\noindent
\centering
\vfill
\begin{tikzpicture}
  \path (.05\textwidth,.05\textheight) coordinate (o) rectangle +(.95\textwidth,.95\textheight) coordinate (f);
  \node [outer sep=0pt, inner sep=0pt, anchor=center] at (current page.center) {%
    \IfFileExists{ouroboros.pdf}{%
      \includegraphics[scale=.9]{ouroboros}%
    }{}};
  \path [decorate, decoration={text effects along path, text={One File To Rule Them All\dots }, text effects/.cd, repeat text, characters={text along path, anchor=center}}] (o |- f) [out=0, in=180] to (f) [out=-90, in=90] to (o -| f) [out=-180, in=0] to (o) [out=90, in=-90] to cycle;
\end{tikzpicture}
\vfill
\clearpage
\end{document}
Related Question