TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The following MWE works just fine, but I had to resort to using \global\def (not that there's anything wrong with it). The intent of the MWE is to provide access to the value of macro as set in an external file, but don't want it to effect the current values that have been set in the parent file via the same macro.

I would like to know how to replace the


with something more LaTeXish as in:


which compiles, but does not yield the proper result. Only need to look at last line in output which should be:

enter image description here


\usepackage{parskip}% formatting only (no issues here)



\newcommand{\GetPropertyAValue}{No Value}%
\newcommand{\ExternalPropertyAValue}{No Value}%


\section{This works just fine}


\section{How get the setting PropertyA but not effect PropertyAValue }
\verb|\GetPropertyAValue|=\GetPropertyAValue (should be \verb|BarA|)\par
\verb|\ExternalPropertyAValue|=\ExternalPropertyAValue (should be \verb|No Value|)\par

    % How do I replace the above line with something like:
\verb|\GetPropertyAValue|=\GetPropertyAValue (should be \verb|BarA|)\par
\verb|\ExternalPropertyAValue|=\ExternalPropertyAValue (should be \verb|FoobarA|)\par
share|improve this question
LaTeX2e does not provide a full set of 'programming' tools: there is no global version of \newcommand. That's why programming LaTeX2e requires quite a bit of TeX, and why the LaTeX3 work has focussed on a programming layer. – Joseph Wright Apr 13 '12 at 6:21
up vote 18 down vote accepted

Simple version

Just hook on the (complicated) definition of \renewcommand:


Now \grenewcommand will have the same syntax as \renewcommand, but its action will be global. The first line after \makeatother defines also a global version of \newcommand.

CAUTION: As David comments, this won't work with commands defined with optional arguments: so \grenewcommand{\foo}[1][bar]{#1} will not work.

Full version

Here's a version that should work fully

  \kernel@ifnextchar [{\@gxargdef#1[#2]}%
share|improve this answer
I don't think that works for optional argument forms though, as you need to make both two commands global in that case and only the internal command gets made long with the star or long code. – David Carlisle Apr 13 '12 at 9:36
@DavidCarlisle You're right; I've added a corrected version. – egreg Apr 13 '12 at 9:59

It's a bit tricky as for optional argument forms you need to make sure that both the top level command (that just applies protection and checks for [) needs to be global and the internal command. this seems to work:

enter image description here


\long \def \@yargd@f#1#2{%
  \def \reserved@a ##1#1##2##{%
    \expandafter\def\expandafter#2\reserved@b ##1#1%
  \l@texglobal\l@ngrel@x \reserved@a 0##1##2##3##4##5##6##7##8##9###1%

















share|improve this answer

I have to admit I didn't follow your code closely, but shouldn't



share|improve this answer
This may have adverse effects on the "save stack" usage: with \def\x{a}{\def\x{b}\global\let\x\x} and \tracingrestores=1 the log file shows {retaining \x=macro:->b} so some space is the save stack is wasted. – egreg Apr 13 '12 at 9:21
I can't quite get this to work. Can you try to delete the \renewcommand and replace the commented line in the MWE with this and see if you can get the last line to be as shown in the question. Sorry about my code not being clear: I want to know what is defined in the external file for one particular macro and ignore everything else in the file. – Peter Grill Apr 13 '12 at 15:29
As well, I think you might get into serious troble if you made the parameter optional. – yo' Apr 13 '12 at 16:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.