以下简单的 METAPOST 代码无法编译,并且会出现错误:
verbatimtex
%&latex
\documentclass{article}
\begin{document}
etex
beginfig(1);
z0 = (0,0); z1 = (sqrt(3)*cm,0);
z2 = (sqrt(3)*cm,1cm);
draw z0--z1--z2--cycle;
label.bot(btex $w$ etex, 1/2[z0,z1]);
endfig;
end;
日志文件指出:
This is MetaPost, version 2.01 (MiKTeX 22.3) 14 APR 2022 17:40
Sample.mp Preloading the plain mem file, version 1.005) (./Sample.mp
>> Sample.mp
>> Sample.mpx
! ! Unable to read mpx file.
l.10 label.bot(btex
$w$ etex, 1/2[z0,z1]);
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
mpxerr 文件指出:
This is mikTeX, Version 3.141592653 (MiKTeX 22.3) (preloaded format=tex 2022.4.14) 14 APR
2022
17:38
restricted \write18 enabled.
%&-line parsing enabled.
**./mp8HtMK1.tex
(mp8HtMK1.tex
! Undefined control sequence.
l.2 \documentclass
{article}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
! Undefined control sequence.
l.3 \begin
{document}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.
[1] [1] )
Output written on mp8HtMK1.dvi (2 pages, 340 bytes).
我感到很困惑。我反复检查了代码,似乎是正确的。编译时它调用了正确的可执行文件(即 mpost.exe、latex.exe)发生了什么?
我已阅读了论坛中的相关问题,并尝试实施建议的解决方案。但没有任何效果。
答案1
由于现在是 2022 年,因此真的没有必要%&latex
再使用 Metapost 的古董调用。旧文档和示例确实都需要更新。
您有两个选择:
首先,如果您只是想尝试普通版本mpost
,而并不真正关心与任何复杂的 LaTeX 格式的集成,那么请替换您的前言,使您的 MP 文件如下所示:
prologues := 3;
outputtemplate := "%j%c.%{outputformat}";
beginfig(1);
z0 = (0,0); z1 = (sqrt(3)*cm,0);
z2 = (sqrt(3)*cm,1cm);
draw z0--z1--z2--cycle;
label.bot(btex $w$ etex, 1/2[z0,z1]);
endfig;
end;
如果您使用它进行编译,mpost
现在将在具有扩展名的文件中创建封装的 PostScript 格式的输出,您可以使用任何 PostScript 查看器查看它,或者使用或类似工具.eps
将其转换为 PDF 。epstopdf
另一方面,如果您希望以更现代的风格使用 MP,并在需要时使用所有有用的 LaTeX 格式,那么请切换到使用lualatex
和luamplib
包。像这样:
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
z0 = (0,0); z1 = (sqrt(3)*cm,0);
z2 = (sqrt(3)*cm,1cm);
draw z0--z1--z2--cycle;
label.bot(btex $w$ etex, 1/2[z0,z1]);
endfig;
\end{mplibcode}
\end{document}
编译它以lualatex
直接生成 PDF 文件。
实际上,标签有一个智能选项,可以让您完全摆脱繁琐的btex ... etex
机制。查看 mplib textext 选项...
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable} % <---- extra option
\begin{mplibcode}
beginfig(1);
z0 = (0,0); z1 = (sqrt(3)*cm,0);
z2 = (sqrt(3)*cm,1cm);
draw z0--z1--z2--cycle;
label.bot("$w$", 1/2[z0,z1]);
endfig;
\end{mplibcode}
\end{document}
编译此文件lualatex
可获得如下 PDF:
您可以阅读(稀疏)文档luamplib
这里。