1

There are different types of “registers” in LuaTeX. With totcount package, you can save counter (number) from any place in a document and get its value also in any place in a document with the help of .aux file (maybe other extension). But is there any way to use this exact (or similar) feature with (Unicode) strings (in Lua)?

For example, I have calculated something at the end which shouldn't be precalculated (i. g., in the preamble) because that will make a big mess and will be much less readable. And then I need to use this result at the beginning (like a \hyperref of some sort, but just for string values). And of course this should be possible with multiple strings at the same time.

  • 3
    see how table of contents are implemented, write to a file, or the aux file and then read at begin document on the next run – David Carlisle Dec 17 '22 at 18:17
  • @DavidCarlisle, thank you for the advice. I was just hoping that there is already some sort of implementation for that in Lua(La)TeX. – Andrew15_5 Dec 17 '22 at 19:36
  • 3
    it's a standard feature of latex, at the end \immediate\write\@auxout{\gdef\noexpand\wibble{my string]} then on the next run \wibble will be defined from begin document to be my string – David Carlisle Dec 17 '22 at 20:26

1 Answers1

0

Thanks to David Carlisle, I made an extensive example of what I wanted to achieve:

\documentclass{article}
\newcommand\lua[1]{\directlua{#1}}

\usepackage{fontspec}

% https://tex.stackexchange.com/questions/572212/ % substituting-fonts-for-emojis-in-lualatex/572220#comment1662059_572220 \lua{luaotfload.add_fallback("FallbackFonts", { "Noto Color Emoji:mode=harf", "Noto Sans CJK JP:" })} \setmainfont{Times New Roman}[RawFeature={fallback=FallbackFonts}]

% Get (command if defined) \newcommand\Get[1]{\csname #1\endcsname}

% Lua get (if variable is not defined or % argument is empty then nothing will be printed) \newcommand\LGet[1]{\lua{tex.print(#1)}}

\makeatletter % Global definition \newcommand\Gdef[2]{\immediate\write@auxout{% \unexpanded{\expandafter\gdef\csname #1\endcsname{#2}}% }}

% Global Lua definition \newcommand\GLdef[2]{\immediate\write@auxout{% \unexpanded{\lua{#1 = #2}}% }}

% Global Lua string definition \newcommand\GLsdef[2]{\immediate\write@auxout{% \unexpanded{\lua{#1 = "#2"}}% }} \makeatother \begin{document}

After 2nd run we get:

\Get{super@ cool!!}

\setlength\parskip{1ex}

\ifdefined\simplename \simplename \fi

\LGet{this1}

\LGet{that2}

% Added nil check \LGet{complex and complex.foo}

\LGet{complex and complex.bar}

% Later in document...

\GLdef{this1}{69}

\GLsdef{that2}{my Unicode string. やった!}

\GLdef{complex}{{foo = 123, bar = "string"}}

\Gdef{super@ cool!!}{% Any string that I want asdf asdfasd fasdfas

df asdf as df \ sdf asf a sdfs }

\Gdef{simplename}{Can be used without the use of additional command. But will give an error if not checked for definition (during the 1st run).} \end{document}

Which will create PDF (with lualatex) after 1st run:

nothing

And after 2nd run we get:

everything