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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

Post Reply