lualatex / luamplib

generic TeX package - including MetaPost code in LuaTeX documents
http://ctan.org/pkg/luamplib
16 stars 11 forks source link

vardef macros with suffix do not work as usual with luamplib #55

Closed franckpastor closed 8 years ago

franckpastor commented 9 years ago

Hello,

I was surprised today to notice that the following program did not work:

\documentclass{article}
\usepackage{luamplib}
\begin{document}
  \begin{mplibcode}

    vardef rotatedlabel@#(expr str, loc, angl) =
      draw thelabel@#(str, loc) rotatedaround(loc, angl)
    enddef;

    beginfig(1);
      rotatedlabel.top(textext("Rotated!"), origin, 45);
    endfig; 
  \end{mplibcode}
\end{document}

While I expected the same result as with standalone MetaPost, i.e. a rotated label:

capture d ecran 2015-03-25 a 22 03 11

I got this error message instead:

Illegal parameter number in definition of \mplibtemp.
<to be read again> 
(
l.15   \end{mplibcode}

If I ask more precisions by entering h in the console, I read:

You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I''m going to assume that you meant ##.

And indeed, if I replace # by ## in the vardef definition, i.e. like that,

vardef rotatedlabel@##(expr str, loc, angl) =
  draw thelabel@##(str, loc) rotatedaround(loc, angl)
enddef;

it suddenly works like a charm. OK, problem solved it seems, but I would like to understand the reasons of this peculiar behavior. Why must we enter ## instead of # with luamplib, whereas only one # is enough with standalone MetaPost? Any idea?

Thanks in advance,

Franck Pastor

dohyunkim commented 9 years ago

Thanks a lot. Fixed in v2.10.1

franckpastor commented 9 years ago

You are welcome! May I ask you what was the cause of this bug?

dohyunkim commented 9 years ago

There was no problem before the introduction of \mpdim and allowing other TeX macros. But now as TeX macros (not all but only simple macros) are allowed in mplib code, we should take care macro expansion by TeX, which complains about single hash tokens (#). Latest patch simply doubles hash tokens before the TeX's macro expansion stage, and restores them to sinlge hash tokens after that process.