Tag Info

Hot answers tagged

47

A long time ago, in a country far far away, under the influence of Hoefstader's Godel, Escher, Bach, I spent a merry few minutes playing with programs that would print out themselves. One goal was to make a minimal such program in a particular language, another was to have a general scheme that could be added to make any program (in that language) do this ...


38

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


32

Save as quine.tex and compile with tex (or pdftex for PDF output): \def\T{ \tt \hsize 32.5em\parindent 0pt\def \S {\def \S ##1>{}}\S \string \def \string \T \string {\par \expandafter \S \meaning \T \string }\par \expandafter \S \meaning \T \footline {} \end } \tt \hsize 32.5em\parindent 0pt\def \S {\def \S ##1>{}}\S \string \def \string \T \string ...


28

Here is a simple example: \documentclass{article} \pagestyle{empty} \usepackage{listings} \begin{document} \lstinputlisting{\jobname} \end{document} The result looks as the original: But if you want to be able to copy from the PDF, you must use this code: \documentclass{article} \pagestyle{empty} \usepackage{listings} ...


24

EDIT ... include a more sinister version... 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 ...


19

If inputting the file itself is allowed, here's a shorter version (Plain TeX): \def\q{\par\begingroup\tt \obeylines \catcode`\\=12 \catcode`{12\catcode`}=12 \obeyspaces\input\jobname \endgroup}\q\bye Alternative version: \let~=\catcode\def\q{{~`\\12 ~`{12 ~`}12 ~`~12\tt \obeyspaces\obeylines\input\jobname\relax}}\q\bye


18

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}


17

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


17

It is not any different than the examples given by other languages. Only a few places where expansion needs to be taken care of. I didn't really go for the code golf but it seems working. And it gets fainter as the recursion depth increases. \documentclass[tikz]{standalone} \newcount\recurdepth \begin{document} \begin{tikzpicture}[scale=2] \draw[style=help ...


16

Without numbers: \documentclass[tikz]{standalone} \begin{document} \begin{tikzpicture} \draw [ultra thick] (-8,0) -- (8,0); \draw [ultra thick] (0,0) circle (8); \foreach \i in {0,1,2,3} {% \draw [ultra thick] (90*\i:8) arc (270+90*\i:180+90*\i:8);} \foreach \i in {0,1,...,7} {% \draw [very thick] ...


16

Much impressed and motivated by Andrew Stacey's beautiful answer, I obtained another way to implement his idea about how can go about this. It is a bit different as I use active characters and delimited macros and less of \char although I did use it. Update: I am adding another shorter method. It is less analogous to Andrew's solution. LaTeX Update: I am ...


15

Code A (preferred) \documentclass[tikz]{standalone} \tikzset{if/.code n args={3}{\pgfmathparse{#1}% \ifnum\pgfmathresult=1\pgfkeysalso{#2}\else\pgfkeysalso{#3}\fi}} \begin{document} \begin{tikzpicture}[nodes=draw, thick, H/.style n args={3}{% #1 = direction, #2=initial length, #3=levels if={0<=#3}{ append after ...


12

I was wondering the same thing yesterday in the comments of this answer on a close subject. For recursive drawings as this one are (relatively!) easy to do with languages closely related to (La)TeX, but external to it, as MetaPost or Asymptote. For example, here is my "quick and dirty" attempt with MetaPost on the Farey diagram illustrated in the original ...


12

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


11

Nothing wrong with the the answers so far but they all use big heavyweight packages, this version doesn't use any package at all and needs rather less code. \documentclass{article} \def\Fbox#1#2{\ifnum#1=0\mbox{#2}\else\fbox{\Fbox{\numexpr#1-1\relax}{#2}}\fi} \begin{document} \Fbox{0}{hello} \Fbox{1}{hello} \Fbox{2}{hello} \Fbox{3}{hello} ...


10

\documentclass{article} \newcommand{\commut}[2]{\left[{#1},{#2}\right]} \makeatletter \def\qcommut#1{\xcommut#1,\relax,} \def\xcommut#1,{\xxcommut{#1}} \def\xxcommut#1#2,{% \ifx\relax#2% #1% \expandafter\@gobbletwo \fi \xxcommut{\commut{#1}{#2}}} \begin{document} $\qcommut{1,2,3,4,5,6}$ \end{document}


10

Can I offer you a forest? Code \documentclass[tikz]{standalone} \usepackage{forest} \begin{document} \begin{forest} Stern Brocot/.style n args={5}{% content=$\frac{\number\numexpr#1+#3\relax}{\number\numexpr#2+#4\relax}$, if={#5>0}{% true append={[,Stern Brocot={#1}{#2}{#1+#3}{#2+#4}{#5-1}]}, append={[,Stern ...


9

You can use the etoolbox package to append content to an existing macro using \gappto (works similar to \g@addto@macro). I've simplified your code snippets so I can make a MWE, but it would most likely be easy to extend to your usage: \documentclass{article} \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \usepackage{tabularx}% ...


8

For now, I'll answer only your second question, about the example from e-TeX. The \expandafter are used to force TeX to actually do the computation before calling \foo recursively, and to clear the finale \fi so that the recursion is terminal. This isn't strictly necessary but is an optimisation. Here is a way to visually check my first assertion: ...


8

\documentclass{article} \usepackage{xparse} \NewDocumentCommand{\tensorkor}{m >{\SplitList{,}}O{}} {\begingroup \mathsf{#1}% \newcommand\object{\vphantom{\mathsf{#1}}}% \ProcessList{#2}{\dotensorkor}% \endgroup} \NewDocumentCommand{\dotensorkor}{m} {% {\object}#1% } \begin{document} $\tensorkor{T}[^a,_b,_c,^r,^f]$ \end{document}


8

As David Carlisle says there is nothing wrong with the other answers. That is, as long as you are satisfied with just a plain box. But when it comes to drawing boxes, nothing beats the tikz way: The extra spacing is achieved by using a white line for two of the boxes. Code: \documentclass{article} \usepackage{tikz} \usepackage{xstring} \tikzset{Dotted ...


8

A token register can collect the nested \fbox commands. \global is needed, because the body of \foreach is executed in a group. \documentclass{article} \usepackage{pgffor} \newtoks\ToksMultiBoxed \newcommand*{\multiboxed}[2]{% \global\ToksMultiBoxed{#2}% \ifnum#1>0 % \foreach \index in {1,...,#1} {% \global\ToksMultiBoxed\expandafter{% ...


8

^^5clet~^^5ccatcode~`76~`A13~`F1~`j00~`P2jdefA71F~`7113jdef PALLFPAXX71F7171P A33FilPA44Fe PA55Fg/PA667172F.7271PA99FaP A887172F7271P8hT4f348 si 9v939bl4fr8mo hXt8:pX /wXw6tc9n6 ro5pk5xXijbye


8

A straightforward recursive Asymptote solution: % % htree.tex : % \documentclass[10pt,a4paper]{article} \usepackage{lmodern} \usepackage{subcaption} \usepackage[inline]{asymptote} \begin{asydef} pen nodeBG=palered; pen nodeBorder=deepblue+0.3bp; pen linePen=nodeBorder; guide H0=scale(1)*shift(-0.5,-0.5)*unitsquare; void drawH0(pair o){ ...


8

If you're willing to peek ahead, you can check whether there's "another argument" and keep gobbling them on the fly: \documentclass{article} \usepackage{etoolbox}% http://ctan.org/pkg/etoolbox \makeatletter \newcommand{\newdecl}[2]{\csgdef{decl@#1}{#2}}% Creates a declaration \newcommand{\csvdel}{}% Delimiter used in CSV representation ...


8

To explain what is happening, we need to consider a couple of things: the \romannumeral business and how we can deal with an unknown number of case tests. (I've also looked the the \romannumeral trick in my blog.) The \romannumeral primitive expands the input stream until it finds an integer: this may be a literal value followed by a non-numerical token or ...


7

The opening and closing braces of the argument to \fbox need to be in the same scope. Here's a possible solution: \documentclass{article} \usepackage{pgffor} \newtoks\boxcontenttok \def\multiboxed#1#2{% \boxcontenttok{#2}% \foreach \index in {1, ..., #1} {% \edef\boxcontent{\noexpand\fbox{\the\boxcontenttok}}% ...


7

And a solution with kvsetkeys for parsing the comma separated list. \qcommut defines \commut@list containing the nested \commut calls with unexpanded arguments: \documentclass{article} \usepackage{kvsetkeys} \usepackage{etexcmds} \makeatletter \newcommand*{\commut}[2]{\left[{#1},{#2}\right]} \newcommand*{\qcommut}[1]{% \let\commut@list\relax ...


7

Here's a different LaTeX3 implementation that allows also for nested calls: \documentclass{article} \usepackage{xparse,mleftright} \ExplSyntaxOn \NewDocumentCommand{\commut}{ m } { \group_begin: \mactay_commut:n { #1 } \group_end: } \cs_new_protected:Npn \mactay_commut:n #1 { \seq_set_from_clist:Nn \l__mactay_list_seq { #1 } \int_compare:nTF { ...


7

Without using xparse. (Edited to provide) two versions, depending on whether the user actually wanted subscripts of subscripts (\recursa) or not (\recursb): \documentclass{article} \usepackage{readarray} \makeatletter \newcounter{index} \newcommand\recursa[1]{% \def\theresult{}% \getargsC{#1}% \setcounter{index}{0}% \whiledo{\value{index} < ...



Only top voted, non community-wiki answers of a minimum length are eligible