# Tag Info

## Hot answers tagged recursion

41

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

24

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

15

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

14

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

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}

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

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

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

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

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

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

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

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

6


6

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

6

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

5

Here's an implementation with xparse and expl3 \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\multibox}{mm} { \multibox_do:nn { #1 } { #2 } } \tl_new:N \l_multibox_tokens_tl \cs_new_protected:Npn \multibox_do:nn #1 #2 { \tl_set:Nn \l_multibox_tokens_tl { #2 } \prg_replicate:nn { #1 } { \tl_set:Nx ...

5

You are doing nothing wrong. Internally, latexmk generates a custom dependency with a single source file, and doesn't have a built-in method of finding other source files. The recursively called latexmk does have this information, but doesn't propagate it back to the caller. But you can obtain this information by a bit of trickery using the internal ...

5

Here's a proof of concept showing that we don't need to use \includegrphics on the PDF generated by the previous compilation. This method works by using \input{\jobname}, after having re-defined the frame environment to produce scaled minipages instead of actual frames. Advantages Although it's much more fragile than the \includegraphics version, it has ...

5

You can test with the macro \IfFileExist{filename}{then-do-this}{else-do-this} whether a file filename exists or not and respond accordingly. If the file filename exists, the then-do-this branch is executed, otherwise the else-do-this branch. Replace then-do-this and else-do-this with your own code. In your new given MWE I would delete thr part to copy the ...

5

Actually some curly braces were wrong and some curly braces are missing around arguments. For analyzing/debugging the setting \tracingmacros=1 is helpful. \documentclass{article} \def\sum#1+#2{% \the\numexpr#1+#2\relax } \def\sumtestX<#1+#2>{% \ifx\empty#2% #1% \else \sum{\sumtestX<#2>}+{#1}% argument braces \fi } ...

4

LuaTeX is an ideal choice for such macros. For example, in ConTeXt, you can define: \def\simplemath<#1>{\ctxlua{context(#1)}} \starttext \simplemath<1+2+3> \simplemath<2*4+5> \stoptext and in (Lua)LaTeX you can define: \documentclass{minimal} \def\simplemath<#1>{\directlua{tex.print(#1)}} \begin{document} ...

4

\catcode@=11 \def\splitarg#1{\@splitarg#1\@nil} \def\@splitarg#1#2\@nil{{#1}\if\relax\detokenize{#2}\relax\else{#2}\fi} \catcode`@=12 \edef\x{\splitarg{{a}{b}{c}}}\show\x \edef\x{\splitarg{{a}}}\show\x Requires e-TeX, so you must process the example with pdftex, xetex, or luatex (not Knuth's tex). The test \if\relax\detokenize{#2}\relax is true when #2 ...

4

Here's a quick expl3 solution (provided that I understand correctly). \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand {\ncommut} { m m } { \n_commut:nn {#1}{#2} } \cs_new:Npn \n_commut:nn #1#2 { \tl_set:Nn \l_tmpa_tl {#1} \clist_map_inline:nn {#2} { \tl_set:Nx ...

4

You can get both behaviors with the same command, just using a *-variant: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \NewDocumentCommand{\tensorkor}{ s m O{} } { \IfBooleanTF{#1} { \gabriel_nonstacked:nn { #2 } { #3 } } { \gabriel_stacked:nn { #2 } { #3 } } } \cs_new_protected:Npn \gabriel_stacked:nn #1 #2 { \mathsf{#1}% ...

3

Given the output you desire, I think I would use matrix of nodes Complete code % arara: pdflatex % !arara: indent: {overwrite: true} \documentclass{standalone} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \begin{tikzpicture}[thick] \matrix (mylattice)[ matrix of nodes, row sep=.5cm, column sep=.5cm, ...

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