如何在 Metapost 圖內插入(如果可能)影像? (在普通的 LaTeX 環境中)

如何在 Metapost 圖內插入(如果可能)影像? (在普通的 LaTeX 環境中)

我想將圖像插入 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評論)我可以使用externalfigureMetafun的命令(我使用LaTeX,所以我不處理Metafun,如果我理解的話,它是ConTeXt特定的),使用mpost -mem=metafunMetapost的編譯選項,但是我的系統( 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>對象,因此您可以根據需要縮放它、旋轉它、移動它等。

相關內容