The problem with using solutions like
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
label.bot(decimal $,(i,-3.7cm));
$ := incr($);
endfor;
is that the infont
operator is called to typeset the labels. infont
has very few typesetting abilities, particularly with mathematical expressions, to the contrary of the btex … etex
flags which allows the use of TeX and co. for this task.
Currently there are at least three ways to achieve the inclusion of variables in a label with MetaPost, while leaving the typesetting to (La)TeX:
Using the TEX.mp
package and the TEX
function it defines.
input TEX;
beginfig(1);
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
label.bot(TEX("$n_" & decimal $ & "$"),(i,-3.7cm));
$ := incr($);
endfor;
endfig;
end.
TEX.mp
calls Plain TeX by default to typeset the labels but it can be configured to use LaTeX as well. It works quite well, but processing a big loop takes an awful lot of time since each call of the TEX
function triggers the creation and the processing of a new separate file. See the MetaPost manual, p. 63 and 64 (as for the version contained in TeX Live 2014).
Using the much more flexible latexmp
package and its textext
function, inspired from the function with the same name defined in the MetaFun format included in ConTeXt. This is by far the best way to include labels if you use standalone MetaPost and the LaTeX format for your papers. As its name implies, it uses LaTeX (exclusively) to typeset the labels. Contrary to TEX, it needs two runs to produce the labels (they can be automatized), but since these labels are passed to a single file for processing it goes much quicker anyway. Also latexmp
is more easily configurable to suit the author's typesetting needs (e.g. for reproducing the LaTeX preamble of the file which is supposed to contain the drawings). The latexmp
documentation is very well done and gives all the details.
input latexmp;
beginfig(1);
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
label.bot(textext("$n_" & decimal $ & "$"),(i,-3.7cm));
$ := incr($);
endfor;
endfig;
end.
Including the MetaPost program in a LuaLaTeX file (for example with the standalone
class) and using the luamplib
package. It also has a textext
function with the same properties as before, but the author needs to run the file only once to produce the labels. See the luamplib
documentation. The main advantage of this tactic is that the labels are automatically typeset in the same fonts as in the general LuaLaTeX document, and so it becomes possible to use OpenType fonts in MetaPost drawings (which is quite impossible to do with standalone MetaPost).
\documentclass{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
$ := 1;
for i = -.3cm step .6cm until 3.6cm:
label.bot(textext("$n_" & decimal $ & "$"),(i,-3.7cm));
$ := incr($);
endfor;
endfig;
\end{mplibcode}
\end{document}
There is also the possibility to use ConTeXt and its own textext
function, but I'm not fluent at all in this format.
Each of this method produces the same result, much more difficult to achieve with the mere use of strings combined with the infont
operator (if it is possible at all to achieve it this way):
![enter image description here](https://cdn.statically.io/img/i.sstatic.net/ZXWMo.png)