Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested mplibcode environments? #63

Open
thruston opened this issue Mar 30, 2016 · 6 comments
Open

Nested mplibcode environments? #63

thruston opened this issue Mar 30, 2016 · 6 comments

Comments

@thruston
Copy link

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 added a commit to dohyunkim/luamplib that referenced this issue Mar 31, 2016
@dohyunkim
Copy link
Contributor

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

dohyunkim added a commit to dohyunkim/luamplib that referenced this issue Mar 31, 2016
@dohyunkim dohyunkim mentioned this issue Mar 31, 2016
@dohyunkim dohyunkim reopened this Dec 10, 2019
@dohyunkim
Copy link
Contributor

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
Copy link
Author

thruston commented Nov 6, 2020

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

@dohyunkim
Copy link
Contributor

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
Copy link
Author

thruston commented Jun 4, 2024

yes, looks good to me too. Many thanks.

@dohyunkim
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants