Skip to content

Commit

Permalink
Merge pull request #100 from jemmybutton/named-mp-instances
Browse files Browse the repository at this point in the history
Separate instances support for LaTeX environment and respective tests
  • Loading branch information
dohyunkim authored Jan 9, 2022
2 parents bb1d858 + 82fac19 commit 0f8aec7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 7 deletions.
22 changes: 15 additions & 7 deletions luamplib.dtx
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ end
luamplib.codeinherit = false
local mplibinstances = {}

local function process (data)
local function process (data, instancename)
% \end{macrocode}
%
% The workaround of issue \#70 seems to be unnecessary, as we use
Expand All @@ -857,10 +857,17 @@ local function process (data)
% end
% \end{verbatim}
% \begin{macrocode}
local standalone = not luamplib.codeinherit
local currfmt = currentformat .. (luamplib.numbersystem or "scaled")
local defaultinstancename = currentformat .. (luamplib.numbersystem or "scaled")
.. tostring(luamplib.textextlabel) .. tostring(luamplib.legacy_verbatimtex)
local currfmt = instancename or defaultinstancename
if #currfmt == 0 then
currfmt = defaultinstancename
end
local mpx = mplibinstances[currfmt]
local standalone = false
if currfmt == defaultinstancename then
standalone = not luamplib.codeinherit
end
if mpx and standalone then
mpx:finish()
end
Expand Down Expand Up @@ -1229,7 +1236,7 @@ local function unprotect_expansion (str)
end
end

local function process_mplibcode (data)
local function process_mplibcode (data, instancename)
% \end{macrocode}
%
% This is needed for legacy behavior regarding |verbatimtex|
Expand Down Expand Up @@ -1283,7 +1290,7 @@ local function process_mplibcode (data)
end)
end

process(data)
process(data, instancename)
end
luamplib.process_mplibcode = process_mplibcode

Expand Down Expand Up @@ -2103,7 +2110,8 @@ luamplib.colorconverter = colorconverter
%
% The \LaTeX-specific part: a new environment.
% \begin{macrocode}
\newenvironment{mplibcode}{%
\newenvironment{mplibcode}[1][]{%
\global\def\currentmpinstancename{#1}%
\mplibtmptoks{}\ltxdomplibcode
}{}
\def\ltxdomplibcode{%
Expand All @@ -2117,7 +2125,7 @@ luamplib.colorconverter = colorconverter
\mplibtmptoks\expandafter{\the\mplibtmptoks#1}%
\def\mplibtemp@a{#2}%
\ifx\mplib@mplibcode\mplibtemp@a
\directlua{luamplib.process_mplibcode([===[\the\mplibtmptoks]===])}%
\directlua{luamplib.process_mplibcode([===[\the\mplibtmptoks]===],"\currentmpinstancename")}%
\end{mplibcode}%
\else
\mplibtmptoks\expandafter{\the\mplibtmptoks\end{#2}}%
Expand Down
54 changes: 54 additions & 0 deletions test-luamplib-latex.tex
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,59 @@
endfig;
\end{mplibcode}%
\vskip 2\baselineskip
\mplibcodeinherit{disable}%
\begin{mplibcode}[instanceOne]
beginfig(1);
a := 1cm;
draw fullcircle scaled a;
draw btex a circle with $d=a$ etex shifted (a,0);
endfig;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip
\begin{mplibcode}[instanceTwo]
beginfig(1);
if not known a:
draw btex code is not inherited from an instance with a different name etex;
else:
errmessage("Variable was inherited from a different instance");
fi;
endfig;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip
\begin{mplibcode}
beginfig(1);
if not known a:
draw btex code is not inherited if instance name is not listed etex;
else:
errmessage("Variable was inherited from a different instance");
fi;
a := 1cm;
endfig;
\end{mplibcode}%
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip
\begin{mplibcode}
beginfig(1);
if not known a:
draw btex code is not inherited if mplibcodeinherit is disabled and instance name is not explicitly set etex;
else:
errmessage("Variable was inherited when code inheritance is turned off and instance name is not set");
fi;
endfig;
\end{mplibcode}%
Current instance name is: \currentmpinstancename (should be empty) \vskip 2\baselineskip
\begin{mplibcode}[instanceOne]
beginfig(1);
draw unitsquare scaled a;
draw btex a square with side $=a$, inherited from the same instance etex shifted (3/2a, 1/2a);
endfig;
\end{mplibcode}%
Current instance name is: \currentmpinstancename \vskip 2\baselineskip
\tracingcommands0
\end{document}

0 comments on commit 0f8aec7

Please sign in to comment.