Trevor
I'm trying to include an underscore in a label in MetaPost. Here is a simple example:
```tex
prologues := 3;
outputtemplate := "%j%c.%{outputformat}";
beginfig(1);
label("aaa_bbb", (0,0));
endfig;
end.
```
When I compile that file with `$ mpost file.mp` I get the following:
![test11-1.png](/image?hash=40a4260eb94cf85325b01e0124847a33d75928f6f7effdb57fc9047edf08f513)
As you can see, the underscore character appears as a raised dot.
*Things I've tried which don't work*:
* replacing `_` with `\_`
* compiling the code in Latex using the `luamplib` package
* changing `prologues` to 0, 1 and 2
**How do I render an underscore?**
Top Answer
frougon
The native font encoding in TeX is OT1 and this encoding has no slot for the underscore. TeX implements `\_` using a rule (even under `\tt` régime):
```
\show\_
\bye
```
```
> \_=macro:
->\leavevmode \kern .06em \vbox {\hrule width.3em}.
l.1 \show\_
```
I don't remember all details for LaTeX but IIRC, it implements `\_` in an encoding-dependent way, because text encodings other than OT1 often have an underscore slot.
# Solution in non-Unicode mode
One can have MetaPost labels use the underscore from T1-encoded fonts by switching to such a font using for instance `defaultfont:="ec-lmr10"` (the value has to be a TeX font name, which is the base name of a TFM file for 8-bit TeX):
```
outputtemplate := "foo%c.mps";
defaultfont:="ec-lmr10";
beginfig(1);
label("aaa_bbb", (0,0));
endfig;
end
```
![image.png](/image?hash=f0ee2053b71e8aa0273b3d77e8f8de2edc395f8b189c711fc4c48d1d38ab9824)
This works when, e.g., included in:
```
\documentclass{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\begin{document}
\includegraphics{foo1}
\end{document}
```
compiled with `pdflatex`.
# Solution in Unicode mode
The following works when compiled with `lualatex`. In this case, the whole label including the underscore is typeset *as LaTeX markup* using the document font (here, TeX Gyre Pagella). Note the use of `\_`.
```
\documentclass{article}
\usepackage{fontspec}
\setmainfont{TeX Gyre Pagella}
\usepackage{luamplib}
\everymplib{ beginfig(0); }
\everyendmplib{ endfig; }
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
label("aaa\_bbb", (0,0));
\end{mplibcode}
\end{document}
```
![image.png](/image?hash=e009a01b42169ba684e6012fd61f7c09768dd5b9a40a6e88251e9a13bd29c6ac)
Answer #2
samcarter
You could set the label as TeX:
```
input TEX;
prologues := 3;
outputtemplate := "%j%c.%{outputformat}";
beginfig(1);
label(TEX("aaa\_bbb"), (0,0));
endfig;
end.
```
(see the metapost manual, section "13.1 TEX.mp", for more information)