Graphics, Figures & Tablesmetapost creates figure out of the border

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
lastone2000
Posts: 2
Joined: Mon Jul 15, 2019 11:46 am

metapost creates figure out of the border

Post by lastone2000 »

Hi Guys,

I'm a beginner of metapost. When I make figures by metapost, figures always appear at the bottom left corner of the layout. For example, I tried an example from 'Tutorial in MetaPost by Andre Heck' on Page 33, see:
Screenshot 2019-07-15 at 11.57.28.png
Screenshot 2019-07-15 at 11.57.28.png (120.53 KiB) Viewed 2098 times
But the plotted figure appear partly at the corner, see:
Screenshot 2019-07-15 at 11.53.53.png
Screenshot 2019-07-15 at 11.53.53.png (153.99 KiB) Viewed 2098 times
My computer is MacOS Mojave 10.14.5. And the version of metapost is kpathsea version 6.2.2, which was installed by macport. Actually this problem occurred several years ago, when I first tried metapost and then gave it up. I will appreciate if you can help me to solve this issue. Thanks in advance.

Zun

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
lastone2000
Posts: 2
Joined: Mon Jul 15, 2019 11:46 am

metapost creates figure out of the border

Post by lastone2000 »

Solved! After creating eps by 'mpost', user should use 'epstopdf' instead of 'ps2pdf'.

Seems the preview is not a good software to open eps file. And 'ps2pdf' doesn't work very well.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10308
Joined: Mon Mar 10, 2008 9:44 pm

metapost creates figure out of the border

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

Thank you for letting us know the solution you found. It can help other metapost users searching for a fix for such an issue.

I tried metapost myself, I just find TikZ more intuitive and readable. Here's a code example for such rotated triangles as in your figure, written by Martin Scharrer here:

Code: Select all

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz,ifthen}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (-60:12cm);
    \coordinate (C) at (240:12cm);
    \foreach \density in {20,30,...,160}{%
        \draw[fill=MidnightBlue!\density] (A)--(B)--(C)--cycle;
        \path
             (A) coordinate (X)
          -- (B) coordinate[pos=.15](A)
          -- (C) coordinate[pos=.15](B)
          -- (X) coordinate[pos=.15](C);
    }
\end{tikzpicture}
\end{document}
It's a complete document, the picture code is pretty short too. The output:
rotated-triangle.png
rotated-triangle.png (90.32 KiB) Viewed 2047 times
It is an example from the TikZ gallery.

Stefan
LaTeX.org admin
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

metapost creates figure out of the border

Post by Ijon Tichy »

With luaatex and luamplib it is very easy to add MetaPost-Code into a LaTeX document:
\documentclass{article}

\usepackage{luamplib}

\begin{document}
\begin{mplibcode}
  beginfig(1);
  pair A,B,C; u:=3cm;
  A=u*dir(-30); B=u*dir(90); C=u*dir(210);
  
  transform T;
  A transformed T = 1/6[A,B];
  B transformed T = 1/6[B,C];
  C transformed T = 1/6[C,A];

  path p; p = A--B--C--cycle;
  for i=0 upto 60:
    draw p; p:= p transformed T;
  endfor;
  endfig;  
\end{mplibcode}
\end{document}
If you cannot use lualatex, but have to use xelatex or pdflatex, you can use package gmp:
\documentclass{article}

% See the gmp-manual for information about using
% one of the following lines:
\usepackage[shellescape]{gmp} 
%\usepackage[noshellescape]{gmp}
%\usepackage[nowrite]{gmp}

\begin{document}
\begin{mpost}
  beginfig(1);
  pair A,B,C; u:=3cm;
  A=u*dir(-30); B=u*dir(90); C=u*dir(210);
  
  transform T;
  A transformed T = 1/6[A,B];
  B transformed T = 1/6[B,C];
  C transformed T = 1/6[C,A];

  path p; p = A--B--C--cycle;
  for i=0 upto 60:
    draw p; p:= p transformed T;
  endfor;
  endfig;  
\end{mpost}
\end{document}
But in this case you either have to run pdflatex or xelatex with option --shell-escape or follow the advice in the log-file to generate the MetaPost image between two LaTeX runs.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
Post Reply