Here are some macros that I am trying to define (based on this question):
\odef{foo}{bar}will define an "object" with namefooand valuebar.\objRef{foo}will insert the value of thefoo"object."
These macros should allow me to reference variables defined anywhere in my document. For example, doing this:
Lorem ipsum dolor \objRef{sentence-Ends/lipsum}.
% INSERT OTHER PARTS OF MY DOCUMENT HERE (CHAPTERS, SECTIONS, FIGURES, TABLES, ETC.)
Defining variables now... \odef{sentence-Ends/lipsum}{sit amet} Done!
should produce
Lorem ipsum dolor ???.
Definining variables now... Done!
after the first run of LaTeX and
Lorem ipsum dolor sit amet.
Definining variables now... Done!
after the second run.
I wrote this code in objectref.sty, but it is not working as expected—I always get ??? on the page and the undefined warning in my log file.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{objectref}[Create and reference custom variables in LaTeX documents]
\def\odef#1#2{\immediate\write@auxout{\expandafter\def \csname obj:#1\endcsname {#2}}}
\def\objRef#1{\ifcsname obj:#1\endcsname \csname obj:#1\endcsname \else ??? \PackageWarning{objectref}{Object #1 undefined on input line \the\inputlineno.}\fi}
Among other things, the .aux file generated when I compile my document contains:
\def \obj:sentence-Ends/lipsum {sit amet}
which should mean that everything will work properly (alas, it does not).
What is the problem with my code, and how do I fix it?



\def\obj:sentence-Ends/lipsum {sit amet}creates a macro\objwith a delimitersentence-Ends/lipsum. You'll either need to either write the macro name wrapped in\csname/\endcsnameto the.auxfile or only use letters +@in the macro name. – Max Chernoff Oct 27 '22 at 03:29