lualatex / luamplib

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

Difficulties to center a Metapost picture properly with luamplib and LuaLaTeX #12

Closed franckpastor closed 10 years ago

franckpastor commented 10 years ago

Hello,

I've recently used MetaPost within LuaLaTeX via the luamplib package and the mplibcode environment, and to my confusion I couldn't manage to center any picture. Here is an example:

\documentclass{minimal} \usepackage{luamplib} \begin{document} \begin{center} \begin{mplibcode} beginfig(1); draw fullcircle scaled 2cm; endfig; \end{mplibcode} \end{center} \end{document}

The circle is drawn but stays at the left…

I've found a workaround though: I've read in the documentation that the contents of the mplibcode environment are put into a TeX \hbox. If I place it into a LaTeX \mbox, or use a \leavevmode command (since I've read somewhere that \mbox is essentially equivalent to \leavevmode\hbox), the figure is centered.

\begin{center} \leavevmode \begin{mplibcode} beginfig(1); draw fullcircle scaled 2cm; endfig; \end{mplibcode} \end{center}

Wouldn't it be better if the contents of the mplibcode environment was put into a \mbox, in the case LaTeX is used instead of Plain TeX? And how could I create an environment that centers such a picture automatically? Coding something like

\newenvironment{mplibcenter}{\begin{center}\leavevmode\begin{mplibcode}}{\end{mplibcode}\end{center}}

doesn't work at all, it triggers an error message.

Those are only details but it would make this package yet more useful. By the way, many thanks for your recent work, the incorporation of the btex ... etex and textext commands are really, really welcome.

Franck Pastor

dohyunkim commented 10 years ago

Yes, \includegraphics of graphicx package always insert \leavevmode before a picture. We may want to follow this convention. Unlike \includegraphics, however, one mplibcode environment can have more than one figures. Those who want to align them vertically would not like \leavevmode before mplib box. So I think current state is better than forcing horizontal mode.

If you want to make a mplib figure horizontally centered automatically, how about this code?

\documentclass{minimal}
\usepackage{luamplib}
\usepackage{etoolbox}
\preto\mplibcode{\center\leavevmode}
\appto\endmplibcode{\endcenter}
\begin{document}
\begin{mplibcode}
beginfig(1);
draw fullcircle scaled 2cm;
endfig;
\end{mplibcode}
\end{document}
franckpastor commented 10 years ago

I didn't know this stuff about \leavevmode and the \includegraphics command. Nor about the etoolbox package. Thank you for this, and for the code that uses it.

Is it thus possible to verticallly align two metapost figures defined in two different beginfig() ... endfig environment but in the same mpibcode environment? I would not know how to do this.

In fact, now that I'm thinking a bit more about it, changing the default behavior of the mplibcode environment is not my principal concern, since it may have its uses, as you notice. What I miss most is an LaTeX environment built on the top of mplibcode, that would leave vertical mode and then center the picture. Something like the "displaymath" environment compared to the "math" environment in LaTeX. As I said, I tried to define this environment by myself, with

\newenvironment{mplibcenter}{\begin{center}\leavevmode\begin{mplibcode}}{\end{mplibcode}\end{center}}

But unfortunately it doesn't work. The following program

\documentclass{minimal} \usepackage{luamplib} \newenvironment{mplibcenter}{\begin{center}\leavevmode\begin{mplibcode}}{\end{mplibcode}\end{center}} \begin{document} \begin{mplibcenter} beginfig(1); draw fullcircle scaled 2cm; endfig; \end{mplibcenter} \end{document}

triggers this error message in the console:

! File ended while scanning use of \ltxdomplibcodeindeed.

I don't know what that means, since I don't know enough about TeX/Lua programming to efficiently look at the package itself.

Thanks a lot for answering me

Franck Pastor

dohyunkim commented 10 years ago

That does not work. mplib environment is very much similar to the verbatim environment. As you caanot make a new enviornment on top of vebatim, you cannt make a new environment on top of mplibcode.

franckpastor commented 10 years ago

I didn't know that… Thanks for pointing this out.

About putting the result of the mplibcode environment in a \hbox (with LuaLaTeX) instead of a \mbox, I've thought a little about it. You said that the interest of a \hbox would be to maintain the possibility of aligning two Metapost figures vertically.

But I still can't see how it is possible to change the alignment of two figures produced with "beginfig(1)… endfig", "beginfig(2)…endfig" environments in the same mplibcode environment. Which Metapost command(s) should we use inside the mplibcode environment to achieve this?

dohyunkim commented 10 years ago

By vertical alignment, I just meant putting mplib boxes from top to bottom. Anyhow, I am now trying to implement an easy way of moving each mplib box vertically and/or horizontally. My plan is using verbatimtex ... etex. For instance:

verbatimtex \moveright 3cm etex; beginfig(0); ...
verbatimtex \leavevmode etex;  beginfig(1); ...
verbatimtex \leavevmode\raise 1ex etex; beginfig(2); ...

Actually I have just committed an experimental code to https://github.com/dohyunkim/luamplib/tree/trandsh . Please wait for a while for an official release.

franckpastor commented 10 years ago

Coming back to this issue after a while… Now with a combination of the \everymplib command and verbatimtex…etex I am able to place my figures as I want, i.e. with LaTeX environments (flushright, center, …) and/or LaTeX commands (\raggedleft, \centering…), by systematically putting

\everymplib{%
    verbatimtex
        \leavevmode
    etex;
    …
    beginfig(1);}
\everyendmplib{endfig;}

in the preamble. So I consider that this issue is now entirely solved.

I personally think it would be useful to emphasize the usefulness of this little \leavevmode-trick a bit more in the documentation, since most LaTeX users don't have the faintest idea of the Plain TeX commands (\moveright, \lower, \endgraf, etc.) that the verbatimtex…etex flags now recognize.

Best regards!

Franck Pastor