GeneralSharing calculations between subfiles

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Gulzt
Posts: 4
Joined: Tue Feb 02, 2016 2:47 pm

Sharing calculations between subfiles

Post by Gulzt »

Goal:
I wish to update variables (created with the calculator-package) in subfiles.

Question:
How can I set a number in a main file, and perform calculations on the value in a subfile, in such a way that it affects the variable in the main file

Problem:
changes to the variables in subfiles do not affect the variable in the global file.

Consider I write this code in the main file:
\COPY{830}{\timeInit}

I include a subfile in the main file, and in it I write the following:
\ADD{\timeInit}{60}{\timeInit}

However, while it is valid code, the +60 is not affecting the original variable.

Workarounds that failed:
I tried using the memory package and use \newdata*{timeInit} to share the variable. Although I can share the variable now that was created in a subfile, I can't perform calculations on the variable now. I suppose it is formatted differently?
file.

Usaging Packages:
-memory
-calculator
-subfiles

Recommended reading 2024:

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

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

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Sharing calculations between subfiles

Post by Johannes_B »

Welcome, unfortunately i don't have the slightes idea what you are trying to achieve and what you already got could look like.

Can you prepare a Minimal working example to show us the current setup? You can attach it as a zip to your post.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Gulzt
Posts: 4
Joined: Tue Feb 02, 2016 2:47 pm

Sharing calculations between subfiles

Post by Gulzt »

Thank you Johannes_B, I will try and clarify with a working example:
\documentclass[12pt, dutch]{report} % to allow chapter tags
\usepackage{subfiles} % for splitting the project into seperate files
\usepackage{calculator} % perform calculations within LaTeX
\usepackage{memory} % use global variables

%first I enter a value in the main file:
%using the calulator package, I add the value 550 to the variable 'timeInit'
\COPY{550}{\timeInit}

%now I link a subfile that manipulates the timeInit value:
\chapter{logboek - februari}
\label{cha:logboek-februari}
\subfile{chapter/logboek-februari.tex}

========================
%in the subfile the following functions are executed:
[color=#4000FF]% -*- root: rootfile.tex -*-
\documentclass[../rootfile.tex]{subfiles}
%the timeInit variable should be 550+310 = 860
\ADD{\timeInitiatie}{310}{\timeInitiatie}

%if I print the result in the subfile the output is indeed 860
timeInit: \timeInit %output 860[/color]
================================

%if I check in the mainfile after including the subfile, the result is still 550, while I was expecting the value 860.
timeInit: \timeInit %output 550


I hope this makes more sense?
I like to manipulate the value in a subfile, and I want the result to have effect on the original variable.

I suppose this is a scope issue. The package memory seems to offer a solution, but declaring a variable using the memory functions, the values are stored in a different format and can't be manipulated by the calculator package.

My calculations are very basis, just add, subtract, divide, multiply, round. If there are other ways to use calculations without the calculator package I hope you could point me to other libraries to get the job done.

The goal of this document is a logfile to document all the tasks performed and how long they took to complete. Since the end-format should be LaTeX, I thought I might just sum up all the times registered in the logfile, and have them added up too show a time tracking sheet on the end page.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Sharing calculations between subfiles

Post by rais »

Hi there,
AFAICS, the subfile package puts its sub-document in a group, and \ADD's result remains local---so yes, it's basically a scope issue.
You could globalize the result in your sub file, e.g.

Code: Select all

\documentclass[12pt, dutch]{report} % to allow chapter tags
\usepackage{subfiles} % for splitting the project into seperate files
\usepackage{calculator} % perform calculations within LaTeX
\usepackage{memory} % use global variables
\begin{filecontents}{chapter/logboek-februari.tex}
% -*- root: rootfile.tex -*-
\documentclass[../rootfile.tex]{subfiles}
%the timeInit variable should be 550+310 = 860
\ADD{\timeInit}{310}{\timeInit}%<-- it doesn't help to use a different variable name here, or where was `\timeInitiatie' defined?
\xdef\timeInit{\timeInit}%<-- makes the change to \timeInit global
timeInit (sub): \timeInit
\end{filecontents}
%first I enter a value in the main file:
%using the calulator package, I add the value 550 to the variable 'timeInit'
\COPY{550}{\timeInit}

\begin{document}%<-- missing
%now I link a subfile that manipulates the timeInit value:
\chapter{logboek - februari}
\label{cha:logboek-februari}
\subfile{chapter/logboek-februari.tex}

timeInit (main): \timeInit
\end{document}
or you could use \input instead of \subfile

Code: Select all

\documentclass[12pt, dutch]{report} % to allow chapter tags
\usepackage{calculator} % perform calculations within LaTeX
\usepackage{memory} % use global variables
\begin{filecontents}{chapter/logboek-februari.tex}
% -*- root: rootfile.tex -*-
%the timeInit variable should be 550+310 = 860
\ADD{\timeInit}{310}{\timeInit}%<-- it doesn't help to use a different variable name here, or where was `\timeInitiatie' defined?
timeInit (sub): \timeInit
\end{filecontents}
%first I enter a value in the main file:
%using the calulator package, I add the value 550 to the variable 'timeInit'
\COPY{550}{\timeInit}

\begin{document}%<-- missing
%now I link a subfile that manipulates the timeInit value:
\chapter{logboek - februari}
\label{cha:logboek-februari}
\input{chapter/logboek-februari.tex}

timeInit (main): \timeInit
\end{document}
(you may need to delete `chapter/logboek-februari.tex' and/or you may need to create a sub-folder `chapter' before compiling this code)

KR
Rainer
Gulzt
Posts: 4
Joined: Tue Feb 02, 2016 2:47 pm

Sharing calculations between subfiles

Post by Gulzt »

Hey Rainer! Thank you very much:
\xdef\timeInit{\timeInit}
did the trick!
Post Reply