Text label in MetaPost: language support

font-encodingsinput-encodingslabelslanguagesmetapost

I am working with the example:

beginfig(1);
label.urt("Текст на русском.", origin);
label.llft("Text in English.", origin);
endfig;

The output contains:

Missing character: There is no 208 in font cmr10!
Missing character: There is no 162 in font cmr10!
Missing character: There is no 208 in font cmr10!
...

What I've tried:

  • different values of the prologues variable (1,2 and 3)
  • in the beginning of the .mp file I wrote:
verbatimtex
\usepackage[utf8]{inputenc}
\usepackage[T2A]{fontenc}
\usepackage[russian]{babel}
\nofiles
etex;
  • also I added to the mpost command an option -tex=latex (hoping MetaPost would take the language settings in LaTeX…)

As I understand, the cmr10 font contains Russian symbols. So what do I have to tell MetaPost that it would 'understand' these letters? I've looked through the MetaPost Manual and browsed over the Internet about the problem — nothing found…

Best Answer

It is possible to get this to work with good old mpost, but it is 2021 now and frankly it is much easier to use lualatex instead. If I wrap the OP example up in luamplib, and choose an appropriate font with good Unicode support, like this:

\documentclass[border=5mm]{standalone}
\usepackage{fontspec}
\setmainfont{Linux Libertine O}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
label.urt("Текст на русском.", origin);
label.llft("Text in English.", origin);
endfig;
\end{mplibcode}
\end{document}

then compiling with lualatex produces this:

enter image description here

which is probably what you want. Notice that the font selection is done at the LaTeX level and that Unicode UTF8 is the default encoding.

I do all of my MP work like this now, simply for the better integration with LaTeX. And if you can't use lualatex for the rest of your document, then you can at least produce separate PDF files using the standalone class, as my example shows.

For documentation, try texdoc luamplib on your local system. Or have a look at sections 11 and 12 of my Drawing with Metapost document (in preparation...). And this earlier answer.

Plain old Metapost version

If you are (for whatever reason) really stuck with plain mpost, then you might be able to get away with something like this.

prologues := 3;
outputtemplate := "%j%c.eps";

def CY primary s = if string s: decode_some_cyrillic(s) infont "fcmr6z" fi enddef;

vardef decode_some_cyrillic(expr given) = 
    save a, b, i, s, out; string s, out; numeric a, b, i;
    out = ""; i=0;
    forever:
        s := substring (i, incr i) of given; 
        a := ASCII s;
        if a < 128: 
        elseif a = 208: 
            s := substring (i, incr i) of given;
            b := ASCII s;
            s := if (143 < b) and (b < 192): char (b + 48) else: "?" fi;
        elseif a = 209: 
            s := substring (i, incr i) of given;
            b := ASCII s;
            s := if (127 < b) and (b < 144): char (b + 112) else: "?" fi;
        else: 
            s := "?";
        fi
        out := out & s;
        exitif i >= length given;
    endfor
    out
enddef;

beginfig(1);
    label.top(CY"Текст на русском.", origin);
    draw (left--right) scaled 2cm;
endfig;
end

Provided that your system has a Cyrillic font called fcmr6z then compiling this with plain mpost should produce:

enter image description here

And just for reference, here is the font layout:

enter image description here

But the luamplib solution is a much better way to go!

Related Question