41

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.

14
  • 1
    \includepdf[]{} starts a new page. figure cannot include a page break. So, yes, they are incompatible. And essentially so. You'd need \includegraphics[]{} instead...
    – cfr
    Mar 22, 2015 at 0:18
  • 1
    \let\newpage=\relax before the \includepdf kills the new page (best be inside a group at that point). Mar 22, 2015 at 7:13
  • 1
    @cfr I find \includepdf easier when I want a full page inclusion, ie I want the PDF page as the background to the current page, possibly for further annotation. With \includegraphics then TeX complains if the graphic is larger than the available text space on the page. Mar 22, 2015 at 20:53
  • 1
    @cfr No, you don't need the =. \let\a\b and \let\a=\b are the same. I find the second easier to see at a glance what it's doing so tend to put the equals sign in place. I understand that in the Good Old Days, tokens were precious and so the = was frowned upon. Nowadays, we're gluttonous about tokens so the difference is in the mind of the beholder. Mar 23, 2015 at 22:51
  • 1
    You might have wanted to post this on codegolf.stackexchange.com ... Mar 24, 2015 at 7:53

5 Answers 5

27

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}
40

Here is a possible start: (code streamlined in edit)

The idea is to use the box model of TeX. In a \vbox we construct the page. Then the \vbox self-includes itself with a scale of .7 thanks to \scalebox of package \graphicx. The box includes an hyperref link, and all reduced copies up to the tiniest work.

\documentclass[a4paper]{article}
\usepackage[margin=0pt]{geometry}
\usepackage{graphicx}
\usepackage{picture}
\usepackage{hyperref}% tous les liens marchent même le plus petit!
\usepackage{url}
\parindent0pt
\topskip0pt
\parskip0pt
\newbox\ouroboros
\begin{document}

\setbox\ouroboros 
\vbox{\hsize\dimexpr\paperwidth-1cm\relax
\begin{picture}(\paperwidth-1cm,\paperheight-1cm)
\put(0,0){\framebox(\paperwidth-1cm,\paperheight-1cm){}}
\end{picture}}


% Loop
\count255 15
\loop
\setbox\ouroboros
\vbox{\hsize\dimexpr\paperwidth-1cm\relax
\begin{picture}(\paperwidth-1cm,\paperheight-1cm)
% frame
\put(0,0){\framebox(\paperwidth-1cm,\paperheight-1cm){}}% or \dashbox
% top
\put(0,.85\paperheight){\makebox(\paperwidth-1cm,3cm){\huge\bfseries  HERE IS
    THE SOLUTION TO ALL MYSTERIES:}}
\put(.5\paperwidth-.5cm,.85\paperheight){\makebox(0,0){\parbox{.9\paperwidth}
{The ouroboros often symbolizes self-reflexivity or cyclicality, especially in
the sense of something constantly re-creating itself, the eternal return, and
other things such as the phoenix which operate in cycles that begin anew as
soon as they end.}}}
% SELF INCLUSION:
\put(.5\paperwidth-.5cm,.5\paperheight-.5cm){\makebox(0,0){\scalebox{.7}{\box\ouroboros}}}
% bottom
\put(.5\paperwidth-.5cm,.1\paperheight){\makebox(0,0){\parbox{.9\paperwidth}
{It can also represent the idea of primordial unity related to something
existing in or persisting from the beginning with such force or qualities it
cannot be extinguished. While first emerging in Ancient Egypt, the ouroboros
has been important in religious and mythological symbolism, but has also been
frequently used in alchemical illustrations, where it symbolizes the circular
nature of the alchemist's opus. It is also often associated with Gnosticism,
and Hermeticism.

\url{http://en.wikipedia.org/wiki/Ouroboros}}}}
\end{picture}}
\ifnum\count255 > 1
\advance\count255 - 1
\repeat

\hrule height 0pt
\kern0.5cm
\noindent\kern.5cm\box\ouroboros
\end{document}

ouroboros

and with 15 iterations:

ouroboros15

3
  • 12
    if you compile with \usepackage{hyperref} all wikipedia links even the tiniest work !
    – user4686
    Mar 21, 2015 at 22:18
  • the reason for using the picture environment was because I wanted easy complete control on where on the page things would end up. However this is not crucial at all to the code. The important thing is the possibility for the \vbox in construction to use itself in the just prior incarnation (like one can do with macros too, in an \edef; the \vbox is a bit analogous in the sens that everything gets fully expanded including its previous self).
    – user4686
    Mar 21, 2015 at 23:27
  • in my previous comment "fully expanded" should be replaced for clarity by "fully typeset" (expansion in an \edef, typesetting in a \vbox).
    – user4686
    Mar 22, 2015 at 7:20
22

Apologies to David Gilmour

\documentclass[tikz,border=4mm]{standalone}
\begin{document}
\def\dsotm{\node[scale around={\x:(0,-1)},
                 draw=white,ultra thin] 
                 at(0,0){\includegraphics[height=4cm]{dsotm}};}
\tikz{\foreach\x in{2,1.75,...,0.25}{\dsotm}}
\end{document}

enter image description here

3
  • Oh I forgot to write the rerun number to aux file then you modify foreach range with it...
    – percusse
    Mar 21, 2015 at 23:40
  • Bah I also forgot the externalization of the image. Nevermind.
    – percusse
    Mar 21, 2015 at 23:51
  • To simulate the effect, an exponential series may be better than a linear one ;-)
    – yo'
    Mar 21, 2015 at 23:57
18

A bit out of topic, since it is a MetaPost example and it is not strictly speaking a picture including itself recursively but I could not resist to have a go at it, however clumsy, the visual effects being addictive!

I've borrowed the original heart-shaped picture's coding from the documentation of Stephan Hennig's wonderful texpath package, and slightly adapted it.


Edit  More in the topic's spirit, now the current picture includes itself (rescaled) repeatedly, instead of repeatedly including the same rescaled lone figure. More precisely, I've replaced

picture fullheart;
fullheart = textpathFont(f, t, heart, 0.5) withcolor red+0.1green; 
for i = 0 upto 20:
    draw fullheart scaled (.75**i);
endfor

by

draw textpathFont(f, t, heart, 0.5) withcolor red+0.1green; 
for i = 1 upto 5:
    draw currentpicture scaled (0.75**(2**(i-1)));
endfor

The complete code:

input latexmp
    setupLaTeXMP(packages="textpathmp", mode=rerun);
input textpath
beginfig(1);
    string f, t;
    path heart;
    f := "\usefont{T1}{pzc}{m}{it}\Huge";
    t := "Love";
    heart := (right{dir 115} .. left{down}) scaled .5 
        shifted (-0.5bp, .5bp)..{dir-50}(0bp, -1bp);
    heart := heart--reverse heart reflectedabout ((0,0),(0,1))--cycle;
    heart := heart scaled 10cm;
    interim textpathRotation := 90;
    interim textpathLetterSpace := 1pt;
    interim textpathRepeat := 33;
    draw textpathFont(f, t, heart, 0.5) withcolor red+0.1green;
    for i = 1 upto 5:
        draw currentpicture scaled (0.8**(2**(i-1)));
    endfor
endfig;
end.

The output:

enter image description here

14

It occurred to me that all of the answers given so far – while in themselves valiant and skillful efforts – fail to fulfill a requirement clearly specified in the question's title: The document should include itself recursively ad infinitum.

There's an obvious problem with this: as soon as the document contains a single element, say the letter “x”, it has to contain infintely many “x”s. As a consequence, the document processing takes forever, it generates an infinitely large pdf file, and rendering this file would never finish.

I believe I have found a viable workaround for this problem:

\documentclass{article}
\pagestyle{empty}
\begin{document}
~
\end{document}

which renders as

A critic may point out that my code actually includes nothing, which may seem to be in violation of the requirement for the document to “include itself”. However, since the document consists of nothing, including nothing is exactly the right thing to do.

8
  • 6
    Another critic may point out that in order for something to occur recursively ad infinitum, it must take an infinite amount of time to occur. ;-) Mar 23, 2015 at 23:48
  • 1
    A further critic may point out that the text of the question clearly states one inclusion per LaTeX run: ...in each run of the file through LaTeX, it would present an image, which is included as a figure in the next run. The ad infinitum in the subject clearly refers to a situation in which compilation is repeated ad infinitum i.e. one in which a single compilation includes neither something nor nothing ad infinitum. (Careful with the scope of the quantifiers here: the inclusion of nothing is not the same as a failure to include anything.)
    – cfr
    Mar 24, 2015 at 1:33
  • 1
    @cfr, I think you overlooked the invisible <irony> tags. ;-)
    – A. Donda
    Mar 24, 2015 at 1:41
  • 1
    @A.Donda No. I think you overlooked mine.
    – cfr
    Mar 24, 2015 at 2:34
  • 3
    @cfr, no, you don't understand. My answer actually does contain invisible <irony> tags. You can only see them when you edit the source. And I know you can't have such invisible tags in a comment, so I haven't overlooked yours. :-) To be honest though, I only added mine after you made your comment.
    – A. Donda
    Mar 25, 2015 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .