# Tag Info

## Hot answers tagged recursion

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

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

26

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

18

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

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

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

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

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

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

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

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

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

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

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

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

7

You have to use more groups ({...}) to localize (re)definition of your macros \ups and rights: \documentclass{article} \usepackage{ifthen,pgf} \parindent=0pt \newcommand{\Populate}[3]% {% \ifthenelse{#2=0}% on the most left border? {% \ifthenelse{#3=0}% on the most top border? {% #1\endgraf% I need to trim the most left comma later! ...

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

@Werner has shown how to do what you want, so I'll answer the other question: Yes you have defined an infinite recursive loop. \newrobustcmd{\TableoftaaBody}{} \newrobustcmd{\addtototaa}[2]{\let\OldContent\TableoftaaBody% \renewrobustcmd{\TableoftaaBody}{\OldContent #1 & #2 \\}} so \TableoftaaBody is eempty then you do \addtototaa{1}{2} so ...

7


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


7

Now, concerning your first question. There is no double recursion going on. You seem to believe the macros recurse-loops to print 1, 2, 3, 4, 5 and the recurse-loops again to print all the TeXs. The truth is, there is only one recursion, but the final TeX are kept on a pile and poped at the end of the recursion. That is, when the final call to \foois made, ...

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

6

As Marc points out, Lindenmayer systems (section 37 "Lindenmayer System Drawing Library" in the 2.10 manual) can do it. However it requires a bit of fiddling to get the node text correct for which I use the base conversion stuff (section 64.4 "Base Conversion"). Note, however, that afew extra spaces creep in to the document from somewhere, and I'm not sure ...

6

As commented, here a solution that uses \@ifnextchar. I also implemented checks against too many or too few arguments (or why are they provided by the user?). The \@ifnextchar(or its “very internal” big brother \kernel@ifnextchar) skips spaces which results in removed spaces in the third and fourth example. Code \documentclass{report} ...

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