![如何在 Metapost 圖內插入(如果可能)影像? (在普通的 LaTeX 環境中)](https://rvso.com/image/266399/%E5%A6%82%E4%BD%95%E5%9C%A8%20Metapost%20%E5%9C%96%E5%85%A7%E6%8F%92%E5%85%A5%EF%BC%88%E5%A6%82%E6%9E%9C%E5%8F%AF%E8%83%BD%EF%BC%89%E5%BD%B1%E5%83%8F%EF%BC%9F%20%EF%BC%88%E5%9C%A8%E6%99%AE%E9%80%9A%E7%9A%84%20LaTeX%20%E7%92%B0%E5%A2%83%E4%B8%AD%EF%BC%89.png)
我想將圖像插入 Metapost 圖(我使用的是 MikTeX/LaTeX,沒有 Metafun)。我希望嘗試(也感謝 Aditya 的建議)以下 Metapost 程式碼(使用-tex=pdftex
選項編譯):
verbatimtex
%&latex
\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
etex
beginfig(1);
picture pct;
pct:=btex \includegraphics{myimage.jpg} etex;
%pct:=btex \includegraphics{myimage.eps} etex;
draw pct;
endfig;
end
但它不起作用,! Unable to make mpx file.
如果我使用jpg
格式,則給出錯誤,或者如果我使用格式,則沒有錯誤但沒有圖像eps
。有什麼線索嗎?
更新:嘗試不使用 ConTeXt 的 Metafun 方式
有人告訴我(請參閱下面的egreg評論)我可以使用externalfigure
Metafun的命令(我使用LaTeX,所以我不處理Metafun,如果我理解的話,它是ConTeXt特定的),使用mpost -mem=metafun
Metapost的編譯選項,但是我的系統( Windows 7、MikTeX/LaTeX)似乎不知道Metafun 並告訴我,所提到的Sorry, I can't find the 'metafun' preload file; will try 'plain'.
實際錯誤。如何在 MikTeX 中安裝 Metafun?! Isolated expression.
externalfigure
-mem=metafun
這是我使用和選項編譯的 Metapost 程式碼-tex=pdftex
:
verbatimtex
\documentclass{minimal}
\begin{document}
etex
beginfig(1);
externalfigure "myimage.jpg";
endfig;
end
答案1
正確答案
筆記我刪除了先前的答案,因為這是一個可行的解決方案:
使用以下 MP 文件
beginfig(1);
externalfigure "hacker.jpg" scaled 3cm;
endfig;
end;
並使用編譯它
mptopdf --metafun filename
這將產生以下filename.1
文件:
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 86 86
%%HiResBoundingBox: 0 0 85.03935 85.03935
%%Creator: MetaPost 1.504
%%CreationDate: 2013.04.14:1210
%%Pages: 1
%%BeginProlog
%%BeginResource: procset mpost
/bd{bind def}bind def
/hlw{0 dtransform exch truncate exch idtransform pop setlinewidth}bd
/vlw{0 exch dtransform truncate idtransform setlinewidth pop}bd
/l{lineto}bd/r{rlineto}bd/c{curveto}bd/m{moveto}bd/p{closepath}bd/n{newpath}bd
/C{setcmykcolor}bd/G{setgray}bd/R{setrgbcolor}bd/lj{setlinejoin}bd/ml{setmiterlimit}bd
/lc{setlinecap}bd/S{stroke}bd/F{fill}bd/q{gsave}bd/Q{grestore}bd/s{scale}bd/t{concat}bd
/sd{setdash}bd/rd{[] 0 setdash}bd/P{showpage}bd/B{q F Q}bd/W{clip}bd
%%EndResource
%%EndProlog
%%Page: 1 1
%%MetaPostSpecials: 2.0 123 1000
%%MetaPostSpecial: 9 85.03935 0 0 85.03935 0 0 hacker.jpg 1 10
0.123 0.012 0.001 R
n 0 0 m
0 0 l
0 0 l
0 0 l
p F
P
%%EOF
以及filename-1.pdf
包含圖像的檔案。
答案2
現代的答案
我知道這已經很舊了,但是luamplib
現在lualatex
有一種更簡單、更有用的方法可以在 Metapost 繪圖中包含外部映像。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\usepackage{graphicx}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
beginfig(1);
picture A;
A = TEX("\includegraphics[width=200pt]{example-image-a}");
% alternative, if you like btex ... etex
% A = btex \includegraphics[width=200pt]{example-image-a} etex;
draw A;
% add a grid (to help you measure the coordinates)
numeric wd, ht;
(wd, ht) = urcorner A;
for x=0 step 10 until wd:
draw (x, -6) -- (x, ht + 6)
withcolor if x mod 100 = 0: red else: 7/8 fi;
endfor
for y=0 step 10 until ht:
draw (-6, y) -- (wd + 6, y)
withcolor if y mod 100 = 0: red else: 7/8 fi;
endfor
% add a label (or whatever)
dotlabel.urt("Label here", (125, 133));
endfig;
\end{mplibcode}
\end{document}
編譯它以lualatex
產生如下 PDF 文件:
一旦您對標籤或其他註釋的位置感到滿意,您(當然)可以刪除網格。由TEX()
巨集(或由)產生的圖片btex ... etex
是常規 MP<picture>
對象,因此您可以根據需要縮放它、旋轉它、移動它等。