I'm writing a long document and, to make my life easier, I like to define quantites I use as macros so that I just have to update them in one place when they change. For me, it makes sense to define these as I go along, in the section of my document which is most relevent to them. However, sometimes I'd like to use these commands in other chapters, sometimes even in previous chapters.
I'd therefore like to add a macro which spits a definition of a command into the latex .aux
files, so that a command can be used everywhere once it's been defined. So far I've got this:
% Make a command which defines a macro with \providecommand but in the aux file,
% so it's accessible to all other chapters
\makeatletter
\newcommand{\doccommand}[2]{%
\protected@write\@auxout{}{\protect\providecommand\protect#1{#2}}%
}
\makeatother
This puts the following into the aux file
\providecommand \tester {123}
for a chapter like this:
\chapter{Test chapter}
This is a test chapter!
Tester is \tester{}.
\doccommand{\tester}{123}
Unfortunately, it doesn't work. Even using the command within the test chapter gives me "Undefined control sequence", and the same in other chapters.
Originally I thought this might have something to do with the spaces that ended up in the .aux
file, but I manually removed them and compiled with an \includeonly{a_different_chapter}
which still gives the same error.
What gives?
Update:
Thanks to all the help here, I got this working and wrote it into a little package. If you're interested, you can find it on CTAN at https://ctan.org/pkg/globalvals
.tex
) and to\input
this file in the preamble? That way, all of your macros will automatically be accessible globally.Foo was measured and its value was found to be \fooValue{}
where\foovalue
is\SI{100(10)}{\meter}
. I have an awful lot of measurements like this, so piling them all into an external text file is possible, but I'd prefer to do it this way.\gdef
not\providecommand
\providecommand
doesn't work because the aux-file is always read in a group. This group ends after reading the aux-file, so local definitions in the aux-file are never visible from the document.