Generalrenewcommand not working backwards?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
randomlatex
Posts: 4
Joined: Wed Mar 16, 2011 10:14 pm

renewcommand not working backwards?

Post by randomlatex »

How do I "save" a word in latex' memory? I.e. a word that is given later in the document, how can I use it at the beginning of the document e.g. with newcommand and renewcommand:

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\begin{document}
\newcommand{\aWord}{a word}

Testing \aWord

\renewcommand{\aWord}{another word}
\end{document}
The above just writes "Testing a word" and not what i want it to write: "Testing another word", it would have written the right word if I put the commands in the preamble, but this can't be done in my case...

The reason I need this is because I have a main .tex file with a section that is at the topmost of the document, where I want to write some of the text that I have in a later part of the document...

Is this even possible to do?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

renewcommand not working backwards?

Post by localghost »

Somehow I can't comprehend the problem.

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\begin{document}
  \newcommand{\aWord}{a word}
  Testing \aWord

  \renewcommand{\aWord}{another word}
  Testing \aWord
\end{document}
But perhaps I misunderstood.


Best regards and welcome to the board
Thorsten
randomlatex
Posts: 4
Joined: Wed Mar 16, 2011 10:14 pm

renewcommand not working backwards?

Post by randomlatex »

Clarification: In the code below:

Code: Select all

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\begin{document}
  \newcommand{\aWord}{a word}
  Testing \aWord
% -- a long document later -- %
  \renewcommand{\aWord}{another word}
  Testing \aWord
\end{document}
I don't want it to write "Testing a word Testing another word", I want it to write "Testing another word Testing another word", so that when the command \aWord is called the first time, it is the same as when it's called the second time... - the \newcommand/\renewcommand perhaps isn't the right way to do this, it was just a thought...
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

renewcommand not working backwards?

Post by josephwright »

You misunderstand how TeX works. The meaning of a macro can be different in different parts of the document. When TeX reads the document, it does not replace all of '\aWord' in one go. Instead, it reads the file a line at a time. So first it finds

Code: Select all

\newcommand{\aWord}{a word}
which defines \aWord to be replaces by 'a word',
so when it comes to \aWord it inserts the replacement text 'a word'. Later, you change the definition to

Code: Select all

\newcommand{\aWord}{another word}
and so TeX will replace any later occurrences of \aWord by 'another word'. But text that has already been processed is unaffected.
Joseph Wright
randomlatex
Posts: 4
Joined: Wed Mar 16, 2011 10:14 pm

renewcommand not working backwards?

Post by randomlatex »

josephwright wrote:You misunderstand how TeX works. The meaning of a macro can be different in different parts of the document. When TeX reads the document, it does not replace all of '\aWord' in one go. Instead, it reads the file a line at a time. So first it finds

Code: Select all

\newcommand{\aWord}{a word}
which defines \aWord to be replaces by 'a word',
so when it comes to \aWord it inserts the replacement text 'a word'. Later, you change the definition to

Code: Select all

\newcommand{\aWord}{another word}
and so TeX will replace any later occurrences of \aWord by 'another word'. But text that has already been processed is unaffected.
But is there no way to do what I what it to do, e.g. change the definition in all previous places. I've tried looking at \@starttoc{aWord}, and tried to "save" the later word with a \addcontentsline{aWord}{aWord}{another word}. But when I print the contents I can't get rid of the page number at the end... Ex.:

Code: Select all

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\begin{document}
  \makeatletter
    \@writefile{aWord}{\select@language {english}}
    \@starttoc{aWord}
  \makeatother
  % long document later %
  Testing
  \addcontentsline{aWord}{aWord}{another word}
\end{document}
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

renewcommand not working backwards?

Post by josephwright »

randomlatex wrote: But is there no way to do what I what it to do, e.g. change the definition in all previous places.
No: TeX is a macro-replacement language where the definitions may change during parsing of the input file. That is how TeX works, as I said.
randomlatex wrote: I've tried looking at \@starttoc{aWord}, and tried to "save" the later word with a \addcontentsline{aWord}{aWord}{another word}.
These macros are meant for constructing tables of contents and similar things. I have no idea why you think they'll enable you to do what you seem to want, unless I've completely misunderstood the aim here.
Joseph Wright
Post Reply