General\gdef confusion

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

\gdef confusion

Post by robertjlee »

Hi there.

I am writing a package that lets the user type a block of text which may be printed later in the output document than it appears in the source, i.e. re-ordering the text.

Currently, I am passing the block of text to a command, which renews another command to hold the page on which the text should occur, based on a sum (using FP). But I've hit a problem; if my command is inside an environment -- any environment -- then the \renewcommand's effect is local to the environment. After the end of the environment, the calculated value is lost.

I guess that I need to change my renewcommand to \gdef, but with that I get some results that I can't understand. (Is there a latex equivalent to TeX's \gdef? "\global\renewcommand" doesn't seem to do anything different to "\renewcommand")

Here is a minimal example that runs, and illustrates my confusion:

Code: Select all

\documentclass{article}

\usepackage{fp}

\newcommand{\foo}{A}

\newcommand{\setfoo}[1]{%
  \FPadd{\temp}{#1}{2}
  \gdef\foo{\temp}
  Set to: \foo
}

\begin{document}
  
  \foo % \foo yields ``A''

  % Output foo
  \begin{center}
    \setfoo{1}% \foo changes to 3.000...

    \foo % \foo is still 3.000...
  \end{center}
  
  \foo % foo is now ``[(]'' ???

\end{document}
Viewing the dvi file with xdvi, the last line on the output has an open-parenthesis inside square brackets, and not the number 3 that I thought it was globally defined to have. Where on earth does that come from?

Any help much appreciated; things seem very surreal to me just now…

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: \gdef confusion

Post by Stefan Kottwitz »

Hi Robert,

use \xdef.

Stefan
LaTeX.org admin
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

\gdef confusion

Post by Juanjo »

Hi Robert,
Here is your code, slightly modified, with comments explaining what is happening:

Code: Select all

\documentclass{article}

\usepackage{fp}

\newcommand{\foo}{A}

\newcommand{\setfoo}[1]{%
  \FPadd{\temp}{#1}{2}
  \gdef\foo{\temp}
  Set to: \foo
}

\begin{document}
\temp
% Curiously, \temp is defined by fp as [\empty ( \empty ],
% where \empty is a LaTeX macro which does nothing.
% So, \temp yields [(].

\foo
% According with its definition, \foo yields ``A''

\begin{center}
  \setfoo{1}
% Firstly, \temp changes to 3.00, thanks to the \FPadd command. 
% This assignment is local to the center environment. 
% Secondly, \foo is globally defined as \temp.

  \foo 
% \foo is defined as \temp, which, inside center, expands to 3.00.
% So, inside center, \foo yields 3.00.
\end{center}
  
\foo 
% \foo has been globally defined as \temp, which, outside the center
% environment, retains its original definition. So \foo yields [(].

\end{document}
The \xdef command suggested by Stefan_K is a synonym for \global\edef. The difference between \def and \edef is roughly whether or not, when the definition is processed, the replacement text of the macro is completely expanded. For example, in the following code

Code: Select all

\def\temp{A}
\def\foo{\temp}
\foo
\def\temp{B}
\foo
the first \foo yields "A", since \foo is \temp and \temp is "A". However, the second \foo yields "B", since \foo is \temp and \temp has become "B". However, in the code

Code: Select all

\def\temp{A}
\edef\foo{\temp}
\foo
\def\temp{B}
\foo
both \foo yield "A": \foo is defined as the "expansion" of \temp, which, at that moment, is "A". Any ulterior changes in \temp does not affect \foo.

Since you are writing a package, I recommend you to read the document with the commented code of LaTeX, i.e. the file source2e.pdf (in TeX Live, this file is at texmf-dist/doc/latex/base), as well as The TeXbook, by D. Knuth, or TeX by Topic, by Victor Eijkhout. All of them are very useful. For example, I deduced the answer I gave you in other thread by regarding in source2e.pdf the definition of \label and \ref.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Re: \gdef confusion

Post by robertjlee »

Juanjo, Thanks again for your help. Thanks also to Stefan_K.

I guess I'd assumed that this weird string must be something funky to do with LaTeX internals, so I didn't even think to look at fp as the source of the "[(]".

\xdef does exactly what I'm looking for. I still have a couple of bugs to iron out, but am well on the way to a working package now.

I'm really learning a lot here. Mostly what I've been doing so far has been experimentation, which gets me the answer in most cases, but these forums have helped me a great deal when I get really stuck and don't know what to try next.

I have taken a look at source2e.pdf, and will be reading it in more detail later. I may also be buying the TeXbook, since it seems to be a highly recommended reference. I haven't bought it up until now, mostly because I've been using LaTeX quite happily as a writer, and haven't needed to delve into package internals before, but that seems to be changing.

Thanks again for all your help,

Robert Lee.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

\gdef confusion

Post by Juanjo »

The book TeX by Topic is also strongly recomended and may replace the TeXbook. You can freely obtain it or buy a bound copy from the above link or http://eijkhout.net/texbytopic/texbytopic.html. In the URL https://savannah.nongnu.org/projects/texbytopic, you can even get the source files.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

\gdef confusion

Post by Stefan Kottwitz »

Hi Robert,

some links to books and docs concerning plain TeX can be found here.

Stefan
LaTeX.org admin
robertjlee
Posts: 17
Joined: Thu Aug 07, 2008 7:51 pm

Re: \gdef confusion

Post by robertjlee »

Thanks everyone for your help, particularly the book suggestions. I've been reading up a lot on TeX and finding out all sorts of things I didn't know, and I've fixed several problems I've been considering for a while now.

I'll post some of my ideas when I've actually got them working.

Yours,

— Robert J Lee
Post Reply