lualatex / luamplib

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

Unexpected behavior of the \everymplib command in version 2.5.2 concerning the % sign #30

Closed franckpastor closed 10 years ago

franckpastor commented 10 years ago

Hello,

I've noticed today that luamplib 2.5.2 is available, and I've just installed it. I noticed immediately that it broke most of my files making use of luamplib. I think I've managed to know why, and it's puzzling. These files share the same preamble, like the one of this minimal example:

\documentclass{article}
\usepackage{luamplib}
\everymplib{%
verbatimtex \leavevmode etex; 
beginfig(0);
}
\everyendmplib{endfig;}
\begin{document}
A try.
\begin{center}
\begin{mplibcode}
draw fullcircle scaled 2cm;
\end{mplibcode}
\end{center}
\end{document}

The circle of this program should be centered, but since luamplib 2.5.2 it is not.

circle_not_centerdd

Bigger programs than this minimal one can't even be compiled.

The "culprit" is the comment sign right at the beginning of the \everymplib command:

 \everymplib{%

If I suppress this comment sign, the circle is centered as expected in my minimal program (and the much bigger ones run as expected).

centered_circle

As I said, the previous versions of luamplib did not show this behavior. This does not seem a very big problem, but I wonder what is its explanation?

dohyunkim commented 10 years ago

@franckpastor Thanks for the report, especially before upload to ctan.

As you have said, the reason of failure is that the comment sign comes at the same line of \everymplib. The detailed reason behind the scene is so complicated that it's not easy for me to explain here. It is a problem regarding \catcode and \endlinechar.

Please think like this: the code between \begin{mplibcode} and \end{mplibcode} or \everymplib{ and }, including comment sign, space, and newline character, is basically metapost code, not TeX code. Only simple TeX code such as btex ... etex or \mpdim{...} is allowed here exceptionally. So the comment sign after \everymplib{ is not necessary as the space character after { is not for TeX typesetting.

However, metapost code starting with comment sign is also a valid code, so the compilation failure is a bug, actually a serious bug. So I have patched luamplib source and uploaded to github repo a few hours ago. Please test again and report if the issue yet persists. When OK, I will upload to ctan tomorrow.

Incidentally, a similar issue regarding newline character might occur after \end{mplibcode} or \everymplib{...}. For instance, with the code

\begin{mplibcode}
draw fullcircle scaled 2cm;
\end{mplibcode}{\large
A}

we would get compilation failure again. It's because the metapost mode has not yet fully terminated as long as we are at the same line of \end{mplibcode}. To avoid failure, we have to write down the code like this:

\end{mplibcode}{\large A}

or

\end{mplibcode}%
{\large A}   

I'm not currently able to repair this issue. But it does not seem to be a common practice to start a group just after \end{mplibcode} and stop that group at another line. Anyway, be careful about this unusual case.

dohyunkim commented 10 years ago

After posted previous reply, an idea hit my brain and I patched luamplib again for the solution to the issue of trailing newline character after \end{mplibcode}. Now the problem seems to have disappeared. It should be okay now to write a code starting a group after \end{mplibcode} and stop the group at another line.

franckpastor commented 10 years ago

I've just installed this patched version of luamplib. The bug seems to be removed: all my previously problematic files, with their comment sign right after \everymplib{, are now correctly typeset. Starting a group after \end{mplibcode} (which I never did previously) doesn't trigger any error either.

Thanks and congratulations for this lightning-quick fix!