2

I'm looking for a solution to the following problem:

\def\m#1{{
   \dimen0=200pt
   \#1=\the#1 % Outputs 200pt rather than 100pt
}}

\dimen0=100pt
\m{\dimen0}

EDIT:

The code sample above illustrates that passing a register as an argument to a macro could lead to surprising results if the macro happens to use the same register internally. So I wonder if there is a way to pass the value of a register rather than the register itself.

7
  • \m{\dimen0} sets \dimen0=200pt so it overrides the \dimen0=100pt you wrote before.
    – Manuel
    Oct 17, 2014 at 12:46
  • 1
    I think you will need to specify what you are actually trying to achieve: perhaps you want to pass the value of a register as the argument?
    – Joseph Wright
    Oct 17, 2014 at 12:47
  • @JosephWright Yes, I want to pass the value of a register as an argument to a macro, not the register itself.
    – SJU
    Oct 17, 2014 at 12:57
  • @AngelTsankov OK, could you specify if the argument will always be a register (say either \dimen<number> or \mydimen), or could be a literal, macro, ... as well
    – Joseph Wright
    Oct 17, 2014 at 12:59
  • @JosephWright The problem illustrated in the code sample above is that of passing a "scratchwork" register as an argument to a macro: the macro could use the same register, e.g. for internal calculations.
    – SJU
    Oct 17, 2014 at 13:11

2 Answers 2

2

You can put expanded value of the register to the macro \m. No matter if eTeX is used or isn't, because we needn't to use \dimexpr.

\def\m#1{{
   \dimen0=200pt
   \#1=#1 % Outputs 200pt rather than 100pt
}}

\dimen0=100pt
\expandafter\m\expandafter{\the\dimen0}
1

If you use e-TeX, you can avoid using a register, exploiting the fact that \dimexpr is essentially a nameless dimension register:

\def\m#1{\expandafter\minner\expandafter{\the\dimexpr#1\relax}}
\def\minner#1{{\dimen0=200pt \#1=#1}}

The input \dimen0=100pt \m{\dimen0} will produce

#1=100.0pt

Without e-TeX and \dimexpr, the workaround is to reserve a register for this particular purpose:

\newdimen\mypersonalandprivateregister
\def\m#1{\mypersonalandprivateregister=#1\relax
  {{\dimen0=200pt \#1=\the\mypersonalandprivateregister}}%
}

Of course, the body of \m mustn't contain any other assignment to \mypersonalandprivateregister.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .