1

Is it possible to detect if the current run is the first (preferably from the Lua end)?

I tried to read the log file, but it is apparently already erased when the Lua engine starts.

I can of course have a file created which I then delete manually after each complete compilation (or create one which is deleted by the first run), but a solution that works "automatically" would be preferred.

keth-tex
  • 466
  • 2
  • 10
  • 6
    f you define "first" as "first since aux file deleted" you can use \IfFileExists{\jobname.aux}{yes}{no} before the \begin{document} (or an equivalent test from Lua) – David Carlisle Jan 30 '23 at 23:54
  • @DavidCarlisle If I'm not mistaken at least with my latex setup the aux-file is never deleted, only it's content erased at the beginning of each run … !? – keth-tex Jan 31 '23 at 01:10
  • 3
    on the first run it does not exist yet , what mean is, if you delete the aux the next latex run is "first" even if it is the 100th run – David Carlisle Jan 31 '23 at 02:22
  • Might be duplicate of How to Create a Counter for the Number of Builds - TeX - LaTeX Stack Exchange. How to pass the value to Lua should be mostly straightforward. – user202729 Jan 31 '23 at 16:53
  • @DavidCarlisle I guess I should have been more specific. Of course, there is not the slightest difficulty in determining the overall first run or implementing a counter. I can simply have an external file created and (for a counter) update it on each run. I was concerned with the case where I compile a document over and over again and, without deleting files in between, can still determine the first run (of the current compilation, not overall). (By "compilation" I mean the entire process of document generation, that is, until all the necessary runs have been performed.) – keth-tex Jan 31 '23 at 23:27
  • @user202729 That's not what I'm looking for. (As stated in the last paragraph of the question.) – keth-tex Jan 31 '23 at 23:27
  • 1
    In that case, how do you formally define what does "first run" and "process of document generation" mean other than by your "intention" (which computers obviously cannot read)? Once you formally defined it, the solution might or might not the straightforward. ■ if it's defined as "as invoked by latexmk", maybe you can hook into some success_cmd or failure_cmd (it's not automatic), and if it's defined as "when the file changes" you may be able to get away with storing the hash of the file and compares it across runs. I don't think there's an automatic solution however. – user202729 Feb 01 '23 at 00:08
  • I still can not see how that is different to \IfFileExists{\jobname.aux} – David Carlisle Feb 01 '23 at 00:09
  • @user202729 "first run" = first run after content of source file changed; "process of document generation" = everything that happens while the content of the source file is unchanged (all necessary runs) It has nothing to do with indefinable "intensions". After a document change it's the first run. And if further runs are necessary is not really a personal decision of the user. When I make changes to the source file the aux file from the previous compilation is still there in the folder. So it can't tell me if it's the first run after document change. At least not in a straightforward way. – keth-tex Feb 01 '23 at 10:08
  • @user202729 However, "source file" would have to include all dependencies. So comparing the hash of the .tex file would not suffice. – keth-tex Feb 01 '23 at 10:14
  • Maybe implement the hash method, but if you want to include the resources as well then maybe look at the open_read_file hook (LuaTeX only) so you can determine which files are read while compiling the document (of course that way you can only determine the number of the run at the end of the run itself) – user202729 Feb 01 '23 at 12:18
  • 2
    @keth-tex It would useful if you said something about the motivation for needing to detect whether the current run is the first. – John Collins Feb 01 '23 at 15:21

2 Answers2

1

You can test the existence of the pdf output:

\newread\f
\openin\f=\jobname.pdf
\ifeof\f\message{First run}\else\message{Next run}\fi
\closein\f

Text

\bye

Note that another answer here (and the discussion too) is based on LaTeX. But LaTeX was not mentioned in the question by OP. My solution is LaTeX independent.

wipet
  • 74,238
  • Isn't this the same as the comments - the OP wants to know if there's been a run even if there are no auxiliary files about (And the PDF presumably counts in that sense) – Joseph Wright Oct 30 '23 at 12:57
  • 1
    @JosephWright Also, the OP counts it as a 'first run' if the .tex file or any of its dependencies have changed, even if all the expected auxiliary files exist. – cfr Oct 31 '23 at 06:14
0

I believe that the solution, which does not require Lua, can be based on the answer provided here: read and write commands to auxfile

Gist of it: When your document runs, write a custom variable (or counter) to the *aux file, then read it at next compile. Re-write it each time. Pay attention to when the *aux is read first, so that your write command is later.

And be sure to test whether the variable is defined. If not, then it must be the first run.

rallg
  • 2,379
  • That's not what I'm looking for. (As stated in the last paragraph of the question.) Of course, there is not the slightest difficulty in determining the overall first run or implementing a counter. I can simply have an external file created and (for a counter) update it on each run. Also, the solution provided in the linked post results in an "infinite number" of runs when using latexmk. – keth-tex Jan 31 '23 at 23:34
  • @keth-tex Perhaps I misunderstand what you are tying to do. From above comments, it looks like others also misunderstand. Can you modify the original question, and provide a pseudo-code example of what you need? That is, a "minimal fake example". – rallg Feb 01 '23 at 19:02
  • 1
    Looking at the author's comments (https://tex.stackexchange.com/questions/673454/luatex-can-i-determine-if-its-the-first-run#comment1673783_673454 and above it), it looks like the author wants to determine the number of compilation passed since the last time the TeX file or any dependent file changed. – user202729 Mar 02 '23 at 23:55