1

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.

5
  • 1
    This is a clear infinite loop. What's your precise goal? Commented Jan 17, 2018 at 23:15
  • I would like to prepend ../ to \A. Commented Jan 17, 2018 at 23:16
  • 1
    @Charlie: You can do \newcommand{\A}{..} \edef\A{../\A} ... \edef\A{../\A} ... Commented Jan 17, 2018 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/… Commented Jan 17, 2018 at 23:34
  • Thank you @Werner, that's precisely what I'm going with. Commented Jan 17, 2018 at 23:35

2 Answers 2

2

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.

1

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.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.