GeneralWeird Scoping problem

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
bongotastic
Posts: 2
Joined: Sat Sep 29, 2007 12:28 pm

Weird Scoping problem

Post by bongotastic »

I've run into a weird behavior: Given the minimal example below, only the myenv correctly prints the result of \mysum. The only difference between the two environments is the order in which the variable is printed. Can anyone help me figuring how to access the variable in myenv1 in a cell other than the first one of the row?

I am using MikTex, most recent version.
===========================================================
\documentclass{article}
\usepackage{fp}

% Some command to add to a sum
\newcommand{\addstuff}[2]{#1 & #2 \\ \FPadd{\mysum}{\mysum}{#2}}

% This env doesn't works properly!
\newenvironment{myenv1}
{ \def\mysum{0.001} \begin{tabular}{l l} Item & Qnty \\ }
{ Total & \mysum \\ \end{tabular}}

% This works OK in the end environment statement
\newenvironment{myenv}
{ \def\mysum{0.001} \begin{tabular}{l l} Item & Qnty \\}
{ \mysum & Total \\ \end{tabular}}

\begin{document}

% sum is incorrect
\begin{myenv1}
\addstuff{Apples}{0.1}
\end{myenv1}

% sum is OK
\begin{myenv}
\addstuff{Apples}{0.1}
\end{myenv}

\end{document}

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

Post Reply