lualatex / luamplib

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

Nested mplibcode environments? #63

Open thruston opened 8 years ago

thruston commented 8 years ago

I'm trying to use luamplib to define a new symbol, and it works ok in normal math formulas, but I've run into a problem when I try to use it in a formula inside a label in another mplibcode environment. Here's my example that shows the problem.

\documentclass{article}
\usepackage{luamplib}
\newcommand{\redbox}{\ensuremath{\mathop{\begin{mplibcode}beginfig(1);draw unitsquare
scaled 4 withcolor .67 red;endfig;\end{mplibcode}}}}
\begin{document}
\noindent
First show that new command works ok: $\left(\redbox p \to q\right)$.
Now show that we can use MP as expected in a display.
\[
    \begin{mplibcode}
    beginfig(2);
       draw fullcircle scaled 24 withcolor .67 blue;
       label.rt(textext("$\left(p\to q\right)$"), 20 right);
    endfig;
    \end{mplibcode}
\]
And now try to use the new command inside a label inside \texttt{mplibcode}.
\[
    \begin{mplibcode}
    beginfig(3);
       draw fullcircle scaled 24;
       label.rt(textext("$\redbox p\to q$"), 20 right);
    endfig;
    \end{mplibcode}  
\]   
Oops!
\end{document}`

In the last math display the whole mplibcode picture is being replaced by the symbol.

dohyunkim commented 8 years ago

Thanks for the report. Commit #64 is an attempt to address the issue. I'll upload to ctan after several days of testing.

dohyunkim commented 4 years ago

Since luamplib v2.20 (TeX Live 2019), which was a huge internal change from the previous version, the example code shown above does not work anymore. I am currently trying to tackle the issue once again, but no success yet. So let me suggest another solution:

\documentclass{article}
\usepackage{luamplib}
\newbox\mympbox
\setbox\mympbox\hbox{%
  \begin{mplibcode}
  beginfig(1);
  draw unitsquare scaled 4 withcolor .67 red;
  endfig;
  \end{mplibcode}}
\def\redbox{\ensuremath{\mathop{\copy\mympbox}}}
\begin{document}
% same as MWE
\end{document}

Now that beginfig(1) has been processed in advance by \setbox...\hbox, beginfig(3) does not contain nested beginfig(1) any more.

thruston commented 3 years ago

thanks for the work around -- and glad to know you are still trying to tackle the issue.

dohyunkim commented 2 months ago

With recent version of luamplib, independent mplib instances can coexist with each other, running side by side. So, just adding an mplibcode instance name, say [nested] as follows, in the \redbox definition seems to solve the issue.

diff --git a/bug63ori.tex b/bug63.tex
index 1423e12..222a249 100644
--- a/bug63ori.tex
+++ b/bug63.tex
@@ -1,6 +1,6 @@
 \documentclass{article}
 \usepackage{luamplib}
-\newcommand{\redbox}{\ensuremath{\mathop{\begin{mplibcode}beginfig(1);draw unitsquare
+\newcommand{\redbox}{\ensuremath{\mathop{\begin{mplibcode}[nested]beginfig(1);draw unitsquare
 scaled 4 withcolor .67 red;endfig;\end{mplibcode}}}}
 \begin{document}
 \noindent
thruston commented 2 months ago

yes, looks good to me too. Many thanks.

dohyunkim commented 1 month ago

With just released version 2.34.1, which will be distributed via TeX Live tomorrow, we can obtain the same result very efficiently.

\begingroup
\setbox0=\hbox{%
  \begin{mplibcode}
    beginfig(1);
    draw unitsquare scaled 4
      asgroup "" withgroupname "redbox"
      withcolor .67red;
    endfig;
  \end{mplibcode}%
}
\endgroup
\newcommand{\redbox}{\ensuremath{\mathop{\usemplibgroup{redbox}}}}

Of course, box 0 will be discarded after endgroup. But, as the transparency group object redbox has already been written to PDF, it can be referred to even after the endgroup.

Even if the redbox is used hundreds times, PDF file size will remain quite small. Rather than replicating the same drawing code hundreds times, usemplibgroup will write to the PDF only the reference to the same PDF object.