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:

\title{The Ouroboros}
\author{U. Roboros}
    \includepdf[frame,scale=0.4]{ouroboros.pdf} %

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.

  • 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
    Commented 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). Commented 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. Commented 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. Commented Mar 23, 2015 at 22:51
  • 1
    You might have wanted to post this on codegolf.stackexchange.com ... Commented Mar 24, 2015 at 7:53

5 Answers 5



... 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:


[[ -f "prawf3.pdf" ]] && mv -f prawf3.pdf ouroboros.pdf

\centering This Is The File To End All Files



A more sinister example using TiKZ:

[[ -f "prawf3.pdf" ]] && mv -f prawf3.pdf ouroboros.pdf
  \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) {%
  \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;

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.

\usepackage{hyperref}% tous les liens marchent même le plus petit!


% Loop
\count255 15
% 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 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.}}}
% bottom
{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.

\ifnum\count255 > 1
\advance\count255 - 1

\hrule height 0pt


and with 15 iterations:


  • 12
    if you compile with \usepackage{hyperref} all wikipedia links even the tiniest work !
    – user4686
    Commented 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
    Commented 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
    Commented Mar 22, 2015 at 7:20

Apologies to David Gilmour

\def\dsotm{\node[scale around={\x:(0,-1)},
                 draw=white,ultra thin] 
\tikz{\foreach\x in{2,1.75,...,0.25}{\dsotm}}

enter image description here

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

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);


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

The complete code:

input latexmp
    setupLaTeXMP(packages="textpathmp", mode=rerun);
input textpath
    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)));

The output:

enter image description here


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:


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.

  • 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. ;-) Commented 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
    Commented Mar 24, 2015 at 1:33
  • 1
    @cfr, I think you overlooked the invisible <irony> tags. ;-)
    – A. Donda
    Commented Mar 24, 2015 at 1:41
  • 1
    @A.Donda No. I think you overlooked mine.
    – cfr
    Commented 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
    Commented 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 .