2

It is quite weird that the gmp package cannot corporate with \sqrt in label. The MME on overleaf:

\documentclass{article}
\usepackage[shellescape,latex]{gmp}%nowrite
\usempxclass{amsart}
\usempxpackage{amssymb}

\title{Metapost+gmp}

\begin{document}

\maketitle
    \begin{figure}[htbp]
        \centering
        \begin{mpost}
            draw fullcircle scaled 100pt;
            label(btex $r=10$ etex, origin);
            %to test, un-comment the following line:
            %label(btex $r=\sqrt{100}$ etex, origin);
        \end{mpost}
        \caption{Caption}
        \label{fig:my_label}
    \end{figure}
\end{document}

I have test that, the problem not appear in mpgraphics. Is it a bug for gmp?

UPDATE:

the error message:

l.17  label(btex
                 $r=\protect \let \reserved@d =[\def \begingroup \def ##1,{}...
The two files given above are one of your source files
and an auxiliary file I need to read to find out what your
btex..etex blocks mean. If you don't know why I had trouble,
try running it manually through MPtoTeX, TeX, and DVItoMP
2

When LaTeX macros are needed in a label, use \btex, rather than btex:

\begin{mpost}
  draw fullcircle scaled 100pt;
  label(\btex $r=\sqrt{100}$ etex, origin);
\end{mpost}

It is already announced at the bottom of page 1 in the documentation

This has some consequences; for example, METAPOST labels will usually go inside
\btex ... etex

See also the example in figure 6 and look for \btex in the manual.

enter image description here

Why is \btex necessary? Because one of the aims of gmp is to be able passing LaTeX macros to the mpost environment mainly in order to use lengths depending on the context. Thus the .mp file is written with full expansion. A few macros would not suffer from this, but a label using btex...etex typically contains things that need to be protected from premature expansion.

Therefore the \btex...etex method has been devised; basically it becomes

btex \unexpanded{...} etex

when it is passed for being written to the .mp file, thus the effect is to write the btex ... etex construction “literally”.

In a standard .mp file \btex is completely equivalent to btex, because \ is defined to be relax, so copying and pasting to another .mp file is possible.

2

Or if lualatex is a suitable option for you, you might like to try the luamplib approach.

\documentclass{amsart}
%-----------
\usepackage{luatex85}
\usepackage{luamplib}
\everymplib{verbatimtex \leavevmode etex; beginfig(0);}
\everyendmplib{endfig;}
\mplibtextextlabel{enable}
% see: `texdoc luamplib` for details...
%----------
\usepackage{amssymb}
\title{Metapost+luamplib}
\begin{document}

\maketitle
\begin{figure}[htbp]
    \centering
        \begin{mplibcode}
            draw fullcircle scaled 100pt;
            label("$r=10$", 20 up);
            label("$r=\sqrt{100}$", origin);
        \end{mplibcode}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}
\end{document}

With the \mplibtextextlabel{enable} option, you can just use strings instead of the btex .. etex mechanism, and no special escapes are needed.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.