LuaLaTex - how to use number, computed later in the document
Why did the AvroCar fail to fly above 3 feet?
Can Mage Hand be used to indirectly trigger an attack?
What publication claimed that Michael Jackson died in a nuclear holocaust?
Why did the Death Eaters wait to reopen the Chamber of Secrets?
Is fission/fusion to iron the most efficient way to convert mass to energy?
Is pointing finger in meeting consider bad?
Is it true that "only photographers care about noise"?
What do I need to do, tax-wise, for a sudden windfall?
Why are backslashes included in this shell script?
How can this shape perfectly cover a cube?
Is there a term for when fiction refers to fiction
What are the advantages of using TLRs to rangefinders?
Keeping track of theme when improvising
I sent an angry e-mail to my interviewers about a conflict at my home institution. Could this affect my application?
Is it a good security practice to force employees hide their employer to avoid being targeted?
Purpose of cylindrical attachments on Power Transmission towers
Parallelized for loop in Bash
I received a gift from my sister who just got back from
Can I get a photo of an Ancient Arrow?
Can a 40amp breaker be used safely and without issue with a 40amp device on 6AWG wire?
Someone who is granted access to information but not expected to read it
Opposite of "Concerto Grosso"?
Why does this Apple //e drops into system monitor when booting?
How to search for Android apps without ads?
LuaLaTex - how to use number, computed later in the document
I am trying to print a number at the beginning of my document, say x. The problem is, that this number gets computed at a later part of my document, for example x = 2 + 3.
How do I use the result of this calculation at the beginning of my document? Commands like tableofcontents are also able to do similar things by multiple compile-steps, so i think this should be possible, but I am stuck here. Do you have any advice?
luatex number
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I am trying to print a number at the beginning of my document, say x. The problem is, that this number gets computed at a later part of my document, for example x = 2 + 3.
How do I use the result of this calculation at the beginning of my document? Commands like tableofcontents are also able to do similar things by multiple compile-steps, so i think this should be possible, but I am stuck here. Do you have any advice?
luatex number
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19
add a comment |
I am trying to print a number at the beginning of my document, say x. The problem is, that this number gets computed at a later part of my document, for example x = 2 + 3.
How do I use the result of this calculation at the beginning of my document? Commands like tableofcontents are also able to do similar things by multiple compile-steps, so i think this should be possible, but I am stuck here. Do you have any advice?
luatex number
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I am trying to print a number at the beginning of my document, say x. The problem is, that this number gets computed at a later part of my document, for example x = 2 + 3.
How do I use the result of this calculation at the beginning of my document? Commands like tableofcontents are also able to do similar things by multiple compile-steps, so i think this should be possible, but I am stuck here. Do you have any advice?
luatex number
luatex number
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited Jun 6 at 18:51
Lemonbonbon
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked Jun 6 at 14:13
LemonbonbonLemonbonbon
1586
1586
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Lemonbonbon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19
add a comment |
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19
add a comment |
2 Answers
2
active
oldest
votes

as in classic tex, you can use the aux file
documentclassarticle
gdefmynumber0% first time
begindocument
The number will be mynumber.
makeatletter
immediatewrite@auxoutgdefstringmynumberdirectlua
n=1+2+3
tex.print(n)
makeatother
enddocument
add a comment |
Welcome to TeX.SX! Here is a LaTeX3 implementation. It is independent of the TeX engine (LuaTeX, pdfTeX, XeTeX...). Moreover, as for LaTeX references, you will be warned if the result has changed since the last compilation run (meaning you should recompile to get correct output). The arithmetic expression passed to writeresult uses the int_eval:n function described in interface3.pdf (cf. part titled The l3int package).
documentclassarticle
usepackagexparse
ExplSyntaxOn
str_new:N g_lemonbonbon_result_str
str_new:N g_lemonbonbon_previous_result_str
msg_new:nnn lemonbonbon additional-compilation-run-may-be-needed
The~computed~result~has~changed;~another~compilation~run~may~be~needed.
cs_new_protected:Npn lemonbonbon_write_result_to_aux_file:n #1
cs_set_eq:cN lemonbonbon@set@result relax % will not expand
iow_now:cx @auxout
use:c lemonbonbon@set@result int_eval:n #1
NewDocumentCommand writeresult m
lemonbonbon_write_result_to_aux_file:n #1
NewDocumentCommand useresult
str_use:N g_lemonbonbon_result_str
AtBeginDocument
% Save the previous result (this is run after the .aux file has been read)
str_gset_eq:NN g_lemonbonbon_previous_result_str g_lemonbonbon_result_str
% Command run when the .aux file is reread, after the AtEndDocument hook.
cs_new_protected:Npn lemonbonbon_warn_if_result_changed:n #1
str_if_eq:VnF g_lemonbonbon_previous_result_str #1
msg_warning:nn lemonbonbon
additional-compilation-run-may-be-needed
% Reading the .aux file at end of document triggers comparison with the
% previous result in order to warn the user if it has changed.
AtEndDocument
cs_gset_eq:cN lemonbonbon@set@result
lemonbonbon_warn_if_result_changed:n
makeatletter
% Command used in the .aux file (uses LaTeX2e naming conventions)
NewDocumentCommand lemonbonbon@set@result m
str_gset:Nn g_lemonbonbon_result_str #1
makeatother
ExplSyntaxOff
begindocument
The result is useresult.
writeresult3+4*(5-3)
enddocument
Output after two compilation runs:

2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "85"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Lemonbonbon is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f494488%2flualatex-how-to-use-number-computed-later-in-the-document%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes

as in classic tex, you can use the aux file
documentclassarticle
gdefmynumber0% first time
begindocument
The number will be mynumber.
makeatletter
immediatewrite@auxoutgdefstringmynumberdirectlua
n=1+2+3
tex.print(n)
makeatother
enddocument
add a comment |

as in classic tex, you can use the aux file
documentclassarticle
gdefmynumber0% first time
begindocument
The number will be mynumber.
makeatletter
immediatewrite@auxoutgdefstringmynumberdirectlua
n=1+2+3
tex.print(n)
makeatother
enddocument
add a comment |

as in classic tex, you can use the aux file
documentclassarticle
gdefmynumber0% first time
begindocument
The number will be mynumber.
makeatletter
immediatewrite@auxoutgdefstringmynumberdirectlua
n=1+2+3
tex.print(n)
makeatother
enddocument

as in classic tex, you can use the aux file
documentclassarticle
gdefmynumber0% first time
begindocument
The number will be mynumber.
makeatletter
immediatewrite@auxoutgdefstringmynumberdirectlua
n=1+2+3
tex.print(n)
makeatother
enddocument
answered Jun 6 at 14:20
David CarlisleDavid Carlisle
510k4311591915
510k4311591915
add a comment |
add a comment |
Welcome to TeX.SX! Here is a LaTeX3 implementation. It is independent of the TeX engine (LuaTeX, pdfTeX, XeTeX...). Moreover, as for LaTeX references, you will be warned if the result has changed since the last compilation run (meaning you should recompile to get correct output). The arithmetic expression passed to writeresult uses the int_eval:n function described in interface3.pdf (cf. part titled The l3int package).
documentclassarticle
usepackagexparse
ExplSyntaxOn
str_new:N g_lemonbonbon_result_str
str_new:N g_lemonbonbon_previous_result_str
msg_new:nnn lemonbonbon additional-compilation-run-may-be-needed
The~computed~result~has~changed;~another~compilation~run~may~be~needed.
cs_new_protected:Npn lemonbonbon_write_result_to_aux_file:n #1
cs_set_eq:cN lemonbonbon@set@result relax % will not expand
iow_now:cx @auxout
use:c lemonbonbon@set@result int_eval:n #1
NewDocumentCommand writeresult m
lemonbonbon_write_result_to_aux_file:n #1
NewDocumentCommand useresult
str_use:N g_lemonbonbon_result_str
AtBeginDocument
% Save the previous result (this is run after the .aux file has been read)
str_gset_eq:NN g_lemonbonbon_previous_result_str g_lemonbonbon_result_str
% Command run when the .aux file is reread, after the AtEndDocument hook.
cs_new_protected:Npn lemonbonbon_warn_if_result_changed:n #1
str_if_eq:VnF g_lemonbonbon_previous_result_str #1
msg_warning:nn lemonbonbon
additional-compilation-run-may-be-needed
% Reading the .aux file at end of document triggers comparison with the
% previous result in order to warn the user if it has changed.
AtEndDocument
cs_gset_eq:cN lemonbonbon@set@result
lemonbonbon_warn_if_result_changed:n
makeatletter
% Command used in the .aux file (uses LaTeX2e naming conventions)
NewDocumentCommand lemonbonbon@set@result m
str_gset:Nn g_lemonbonbon_result_str #1
makeatother
ExplSyntaxOff
begindocument
The result is useresult.
writeresult3+4*(5-3)
enddocument
Output after two compilation runs:

2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
add a comment |
Welcome to TeX.SX! Here is a LaTeX3 implementation. It is independent of the TeX engine (LuaTeX, pdfTeX, XeTeX...). Moreover, as for LaTeX references, you will be warned if the result has changed since the last compilation run (meaning you should recompile to get correct output). The arithmetic expression passed to writeresult uses the int_eval:n function described in interface3.pdf (cf. part titled The l3int package).
documentclassarticle
usepackagexparse
ExplSyntaxOn
str_new:N g_lemonbonbon_result_str
str_new:N g_lemonbonbon_previous_result_str
msg_new:nnn lemonbonbon additional-compilation-run-may-be-needed
The~computed~result~has~changed;~another~compilation~run~may~be~needed.
cs_new_protected:Npn lemonbonbon_write_result_to_aux_file:n #1
cs_set_eq:cN lemonbonbon@set@result relax % will not expand
iow_now:cx @auxout
use:c lemonbonbon@set@result int_eval:n #1
NewDocumentCommand writeresult m
lemonbonbon_write_result_to_aux_file:n #1
NewDocumentCommand useresult
str_use:N g_lemonbonbon_result_str
AtBeginDocument
% Save the previous result (this is run after the .aux file has been read)
str_gset_eq:NN g_lemonbonbon_previous_result_str g_lemonbonbon_result_str
% Command run when the .aux file is reread, after the AtEndDocument hook.
cs_new_protected:Npn lemonbonbon_warn_if_result_changed:n #1
str_if_eq:VnF g_lemonbonbon_previous_result_str #1
msg_warning:nn lemonbonbon
additional-compilation-run-may-be-needed
% Reading the .aux file at end of document triggers comparison with the
% previous result in order to warn the user if it has changed.
AtEndDocument
cs_gset_eq:cN lemonbonbon@set@result
lemonbonbon_warn_if_result_changed:n
makeatletter
% Command used in the .aux file (uses LaTeX2e naming conventions)
NewDocumentCommand lemonbonbon@set@result m
str_gset:Nn g_lemonbonbon_result_str #1
makeatother
ExplSyntaxOff
begindocument
The result is useresult.
writeresult3+4*(5-3)
enddocument
Output after two compilation runs:

2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
add a comment |
Welcome to TeX.SX! Here is a LaTeX3 implementation. It is independent of the TeX engine (LuaTeX, pdfTeX, XeTeX...). Moreover, as for LaTeX references, you will be warned if the result has changed since the last compilation run (meaning you should recompile to get correct output). The arithmetic expression passed to writeresult uses the int_eval:n function described in interface3.pdf (cf. part titled The l3int package).
documentclassarticle
usepackagexparse
ExplSyntaxOn
str_new:N g_lemonbonbon_result_str
str_new:N g_lemonbonbon_previous_result_str
msg_new:nnn lemonbonbon additional-compilation-run-may-be-needed
The~computed~result~has~changed;~another~compilation~run~may~be~needed.
cs_new_protected:Npn lemonbonbon_write_result_to_aux_file:n #1
cs_set_eq:cN lemonbonbon@set@result relax % will not expand
iow_now:cx @auxout
use:c lemonbonbon@set@result int_eval:n #1
NewDocumentCommand writeresult m
lemonbonbon_write_result_to_aux_file:n #1
NewDocumentCommand useresult
str_use:N g_lemonbonbon_result_str
AtBeginDocument
% Save the previous result (this is run after the .aux file has been read)
str_gset_eq:NN g_lemonbonbon_previous_result_str g_lemonbonbon_result_str
% Command run when the .aux file is reread, after the AtEndDocument hook.
cs_new_protected:Npn lemonbonbon_warn_if_result_changed:n #1
str_if_eq:VnF g_lemonbonbon_previous_result_str #1
msg_warning:nn lemonbonbon
additional-compilation-run-may-be-needed
% Reading the .aux file at end of document triggers comparison with the
% previous result in order to warn the user if it has changed.
AtEndDocument
cs_gset_eq:cN lemonbonbon@set@result
lemonbonbon_warn_if_result_changed:n
makeatletter
% Command used in the .aux file (uses LaTeX2e naming conventions)
NewDocumentCommand lemonbonbon@set@result m
str_gset:Nn g_lemonbonbon_result_str #1
makeatother
ExplSyntaxOff
begindocument
The result is useresult.
writeresult3+4*(5-3)
enddocument
Output after two compilation runs:

Welcome to TeX.SX! Here is a LaTeX3 implementation. It is independent of the TeX engine (LuaTeX, pdfTeX, XeTeX...). Moreover, as for LaTeX references, you will be warned if the result has changed since the last compilation run (meaning you should recompile to get correct output). The arithmetic expression passed to writeresult uses the int_eval:n function described in interface3.pdf (cf. part titled The l3int package).
documentclassarticle
usepackagexparse
ExplSyntaxOn
str_new:N g_lemonbonbon_result_str
str_new:N g_lemonbonbon_previous_result_str
msg_new:nnn lemonbonbon additional-compilation-run-may-be-needed
The~computed~result~has~changed;~another~compilation~run~may~be~needed.
cs_new_protected:Npn lemonbonbon_write_result_to_aux_file:n #1
cs_set_eq:cN lemonbonbon@set@result relax % will not expand
iow_now:cx @auxout
use:c lemonbonbon@set@result int_eval:n #1
NewDocumentCommand writeresult m
lemonbonbon_write_result_to_aux_file:n #1
NewDocumentCommand useresult
str_use:N g_lemonbonbon_result_str
AtBeginDocument
% Save the previous result (this is run after the .aux file has been read)
str_gset_eq:NN g_lemonbonbon_previous_result_str g_lemonbonbon_result_str
% Command run when the .aux file is reread, after the AtEndDocument hook.
cs_new_protected:Npn lemonbonbon_warn_if_result_changed:n #1
str_if_eq:VnF g_lemonbonbon_previous_result_str #1
msg_warning:nn lemonbonbon
additional-compilation-run-may-be-needed
% Reading the .aux file at end of document triggers comparison with the
% previous result in order to warn the user if it has changed.
AtEndDocument
cs_gset_eq:cN lemonbonbon@set@result
lemonbonbon_warn_if_result_changed:n
makeatletter
% Command used in the .aux file (uses LaTeX2e naming conventions)
NewDocumentCommand lemonbonbon@set@result m
str_gset:Nn g_lemonbonbon_result_str #1
makeatother
ExplSyntaxOff
begindocument
The result is useresult.
writeresult3+4*(5-3)
enddocument
Output after two compilation runs:

edited Jun 6 at 17:06
answered Jun 6 at 16:09
frougonfrougon
3,280917
3,280917
2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
add a comment |
2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
2
2
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
oh no, green square trying to steal my tick by application of expl3 code, you are not by any chance related to @egreg are you? :-)
– David Carlisle
Jun 6 at 16:11
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
@DavidCarlisle Not to my knowledge. I'm just an insignificant apprentice ;-)
– frougon
Jun 6 at 16:14
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
Note that this could be extended to store an arbitrary number of (key, value) pairs. Not sure anyone cares, though...
– frougon
Jun 6 at 19:55
add a comment |
Lemonbonbon is a new contributor. Be nice, and check out our Code of Conduct.
Lemonbonbon is a new contributor. Be nice, and check out our Code of Conduct.
Lemonbonbon is a new contributor. Be nice, and check out our Code of Conduct.
Lemonbonbon is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to TeX - LaTeX Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f494488%2flualatex-how-to-use-number-computed-later-in-the-document%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome to TeX.SX! Could you already share a minimal working example (MWE) with what you have so far? So do you already have code that calculates something in Lua. Because up to now it is not totally clear how and where the calculation is happening/should happen.
– Stefan Pinnow
Jun 6 at 14:19