8

I am trying to plot some graphics calling mplib from lualatex. The 'standard' way is the use of luamplib package and mplibcode environment. I was wandering, is there a way to call mplib from lua directly? As long as mplibcode is just an 'easy' and 'LaTeX' way to do so, it should be possible. However, I could not find any documentation or examples on this topic. Unfortunately, the explanations in the luamplib manual stops at line 54, before I am really able to get an idea of the machinery behind it. It seems that there are few people who knows how to do this, they just had no time to provide others with any documentation.

This thread proposes the solution which seems unnatural: there is lua code which generates tex code inside Tikz environment which is then in turn calls mplib via lua.

Example

The minimal example is:

example.tex:

\documentclass{article}
\usepackage{luamplib}

\begin{document}
\directlua{ dofile('testmplibmin.lua') }
\directlua{ StartMP() }

\end{document}

testmplibmin.lua:

local mpkpse = kpse.new("luatex", "mpost")

local function finder(name, mode, ftype)
  if mode == "w" then
    return name
  else
  return mpkpse:find_file(name,ftype)
  end
end

function StartMP()
  local mplib = require('mplib')
  local mpx=mplib.new({find_file=finder,ini_version=true})
  local result = mpx:execute('input plain;')
  result=mpx:execute('beginfig(1); draw fullcircle scaled 20 withcolor red; endfig;')
  local t,e,l = result.term,result.error,result.log
  if result.status>0 then
    tex.print([[Result of mplib execute is unsuccessfull.]])
  else
    if result.fig then
      tex.sprint('Converted something: \\vrule\\vbox{\\hrule')
      local converted=luamplib.convert(result)
      tex.sprint('\\hrule}\\vrule')
    else
      tex.print([[No figure output.]])
      tex.print([[Log:]])
      tex.print(l)
    end
  end
  mpx:finish()
end

I will try to improve (=shorten) it more. E.g., I believe that there is a way to use finder function from luamplib. Any comments are welcome.

2 Answers 2

6

The file mplibapi.pdf from the metapost source distrobution documents the low-level interface. Still, it is probably wise to read the luamplib code as well, because the conversion from mplib's return table (containing images) to PDF literal code is not quite trivial. http://www.tug.org/metapost/src/manual/mplibapi.pdf

1
  • Thank you for the answer. It seems that you are one of those guys who know the_way. I've played a bit more with mplib and realized that it did not even start for me because I did not realize that loading plain format is absolutely necessary to start mplib. The manual could be more pronounced about it. I actually had an (incorrect) idea that every chunk which compiles with stand-alone metapost should also be correct for mplib. Using manual and (mainly) luamplib source I did a minimal example rather close to what I wanted. I've put it in the post: it might be useful.
    – Misha
    Jul 25, 2011 at 7:34
2

I agree that the documentation of luamplib should give at least a couple of examples. The following does what is intended

\documentclass[a4paper]{article}
\usepackage{luamplib}

\begin{document}
Some text%
\begin{mplibcode}
beginfig(1);
draw (0,0)--(100,0)--(100,100)--(0,100)--cycle;
endfig;
\end{mplibcode}
\ Some other text

\end{document}

The % and \ are necessary to avoid a spurious space and getting no space respectively.

3
  • 3
    he wants to call the lib from lua itself not from TeX, if I understand the question correct.
    – user2478
    Jul 24, 2011 at 14:16
  • Oh, I see! The reference to luamplib was misleading.
    – egreg
    Jul 24, 2011 at 14:20
  • Yes, I am aware of mplibcode and know how to use it for the basic stuff. The question is how to use mplib capabilities from lua directly. The idea is to do something similar to mentioned in that post but without rather messy intermediate steps and packages. I would prefer lua programming.
    – Misha
    Jul 24, 2011 at 14:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .