Re-defining a variable w.r.t itself a finite number of times

Is there any way to re-define a variable a finite number of times? Here's MWE of what I'm looking for to work:

\documentclass[11pt]{article}
\newcommand{\A}{..}
\newcommand{\B}{../\A{}}
% \renewcommand{\A}{\B{}}    % Breaks the build
\newcommand{\SUCCESS}{Success!}
\begin{document}
\section{Test}
\A
\B
\SS
\end{document}


When I uncomment line 4, I get an error saying "TeX capacity exceeded, sorry [input stack size=5000]. [\A]".

I was hoping to do this in a simple way, without packages. If I remove the curly-braces in the definition for \B and re-definition for \A, the build hangs and never seems to finish.

I appreciate any help/suggestions.

• This is a clear infinite loop. What's your precise goal? – egreg Jan 17 '18 at 23:15
• I would like to prepend ../ to \A. – Charles Jan 17 '18 at 23:16
• @Charlie: You can do \newcommand{\A}{..} \edef\A{../\A} ... \edef\A{../\A} ... – Werner Jan 17 '18 at 23:34
• Well, I had been searching for a while, but I just now found this solution, which seems most appropriate... tex.stackexchange.com/questions/8163/… – Charles Jan 17 '18 at 23:34
• Thank you @Werner, that's precisely what I'm going with. – Charles Jan 17 '18 at 23:35

If your aim is to prepend ../ to whatever \A expands to, the answer is with etoolbox:

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\A}{..whatever..}

\begin{document}

\A %% prints ..whatever..

\preto{\A}{../}

\A %% prints ../..whatever..

\end{document}


Without etoolbox, the standard way is

\toks0={../}
\toks2=\expandafter{\A}
\edef\A{\the\toks0 \the\toks2 }


About the error you get if you do

\newcommand{\A}{..}
\newcommand{\B}{../\A}
\renewcommand{\A}{\B}


here's the explanation. When you eventually call \A, TeX will replace it with \B, which will be replaced with ../\A. Now ../ are processed and macro expansion restarts from \A. Infinite loop.

This is the way but I don't know what it supposed to do.

\documentclass{article}

\newcommand{\A}{..}
\let\oldA\A
\newcommand{\B}{../\oldA{}}
\renewcommand{\A}{\B{}}    % Now DOESN'T break the build
\newcommand{\SUCCESS}{Success!}

\begin{document}
\section{Test}
\A
\B
\SUCCESS

\end{document}


The solution is to keep the old definition of the command with the help of a \let command and use this instead of the original , so that redefined command (\A) doen't call itself.