summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/globalvals
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2019-01-23 21:29:45 +0000
committerKarl Berry <karl@freefriends.org>2019-01-23 21:29:45 +0000
commit587bb7e9ffc42cc55c9154d8047f5cfef1de9471 (patch)
tree974418b6c821a28ca8f44c9a68f3e949f632504e /Master/texmf-dist/tex/latex/globalvals
parentb74f40314c53a9b3e912e4e81e4729a6ba6f4327 (diff)
globalvals (23jan19)
git-svn-id: svn://tug.org/texlive/trunk@49802 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/tex/latex/globalvals')
-rw-r--r--Master/texmf-dist/tex/latex/globalvals/globalvals.sty62
1 files changed, 62 insertions, 0 deletions
diff --git a/Master/texmf-dist/tex/latex/globalvals/globalvals.sty b/Master/texmf-dist/tex/latex/globalvals/globalvals.sty
new file mode 100644
index 00000000000..48b99ded28c
--- /dev/null
+++ b/Master/texmf-dist/tex/latex/globalvals/globalvals.sty
@@ -0,0 +1,62 @@
+% Global Vars
+
+% (c) Charles Baynham 2019
+
+% License: LaTeX Project Public License 1.3c
+
+% % This program can be redistributed and/or modified under the terms
+% % of the LaTeX Project Public License Distributed from CTAN archives
+% % in directory macros/latex/base/lppl.txt.
+
+% This package allow the user to declare a variable which can then be used
+% anywhere else in a document, including before it was declared. This is done
+% by putting the definitions into the .aux files, therefore requiring two runs
+% to get it right.
+
+% It implements two commands: \defVal{<value>} and \useVal{<value>}. \defVal
+% sets up a global variable and \useVal recalls it.
+
+% Using \defVal twice will result in an error. Using \useVal for an undefined
+% value will output the text "??"
+%
+\makeatletter
+\NeedsTeXFormat{LaTeX2e}
+\def\@globalvalsPkgName{globalvals}
+\ProvidesPackage{\@globalvalsPkgName}
+ [2019/01/23 v1.0 Define and use global variables throughout a document, including before their definition]
+
+
+
+% useVal@#1 will hold the value of the variable: it's defined by an .aux file
+% somewhere
+\newcommand{\useVal}[1]{%
+ \ifcsname useVal@#1\endcsname
+ \csname useVal@#1\endcsname
+ \else
+ ??%
+ \PackageWarning{\@globalvalsPkgName}{Value "#1" has not yet been defined. This may be fine, you may need to recompile. If you keep getting this message it's because you haven't defined it yet. }
+ \fi
+}
+
+% useVal@#1 is defined in the .aux file to be used on the next run.
+% useVal@#1@defined is defined immediately to mark that useVal@#1 has already
+% been used and cannot be used again
+\newcommand{\defVal}[2]{%
+\ifcsname useVal@#1@defined\endcsname
+ \PackageError{\@globalvalsPkgName}{Value "#1" already defined}%
+ {You have two defVal commands which have the same parameter name. This error is occurring on the 2nd (or 3rd, or 4th...).}%
+\else%
+ \write\@auxout{%
+ \unexpanded{\global\@namedef{useVal@#1}{#2}}%
+ }%
+ \global\@namedef{useVal@#1@defined}{}%
+ \global\@namedef{useVal@#1}{#2}%
+ \PackageInfo{\@globalvalsPkgName}{Defining new variable "#1" with value "#2"}%
+\fi%
+}
+
+\makeatother
+
+\endinput
+%%
+%% End of file `globalvals.sty'. \ No newline at end of file