0

I wanted to take the solution provided by @Thruston at phases (stages) of economic cycle. However, when I copied and pasted the first block of code provided as a solution in English, I could not get the output in latex. I am getting the error Missing \begin{document} What could be the causes?

itc
  • 657

1 Answers1

4

The problem was that the original diagram was designed to be compiled with Metapost directly -- "the old way". So to get the desired output, you should compile it with mpost rather than any LaTeX engine. If you do that it will create a .eps file in Encapsulated PostScript which you can render into various forms with GhostScript, epstopdf etc.

Or you can adapt it for modern usage with lualatex and luamplib. The adaptation is pretty easy.

Start with this template file.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);
% drawing-commands-go-here
endfig;
\end{mplibcode}
\end{document}

Then copy everything in the source file between beginfig and endfig and put it in the template in place of the comment line.

Then you can compile it with lualatex to get a PDF output file directly.

Here is a worked example:

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\begin{mplibcode}
beginfig(1);

string ff; ff = "phvr8r";

% fiddle with the parameters to get a nice rising wave path wave; wave = (origin for t=1 step 1 until 3360 + 24: -- (2/3t,70 sind(t)) shifted (0,1/3t) endfor) scaled 3/4 shifted (21,34);

% draw the straight arrow and the wave on top drawarrow point 0 of wave -- point 3*360 of wave scaled 1.05 dashed evenly withcolor .5[blue, white]; draw wave withpen pencircle scaled 1 withcolor .67 green;

% labels picture s[]; % center each label and push it up/down slightly s1 = "peak" infont ff; s1 := s1 shifted (-1/2 xpart urcorner s1, 6); s2 = "trough" infont ff; s2 := s2 shifted (-1/2 xpart urcorner s2, -12); s3 = "expansion" infont ff; s3 := s3 shifted (-1/2 xpart urcorner s3, 6); s4 = "recession" infont ff; s4 := s4 shifted (-1/2 xpart urcorner s4, 6);

% add the labels in the "right" places t = 0; n = 0; forever: dt := directiontime right of subpath(t,infinity) of wave; exitif dt < 0; t := t + dt; n := n + 1; if odd(n): draw s1 shifted point t of wave withcolor .67 blue;
if n>1: draw s3 rotated angle direction t-dt/2 of wave shifted point t-dt/2 of wave ; fi else: draw s2 shifted point t of wave withcolor .67 red;
draw s4 rotated angle direction t-dt/2 of wave shifted point t-dt/2 of wave; fi endfor

% do the axes and titles path xx, yy; xx = origin -- (xpart point infinity of wave, 0); yy = origin -- (0, ypart point infinity of wave); drawarrow xx; drawarrow yy;

label.bot("Time" infont ff scaled 1.2, point 1/2 of xx); label.lft("Level of real output" infont ff scaled 1.2 rotated 90, point 1/2 of yy); label.top("The business cycle" infont ff scaled 1.44, point 0.8 of yy shifted 108 right) withcolor .8 red;

endfig; \end{mplibcode} \end{document}

compile this with lualatex to get a PDF looking like this:

enter image description here

Note

If you have more than one beginfig / endfig pair in your source MP file, you should copy each of them into separate template files.

Thruston
  • 42,268
  • Lets say I have loaded xelatex and not lualatex, what changes should I make for the code to run? – itc Feb 09 '23 at 03:38
  • You can't use luamplib with xelatex, but you can use lualatex to make a stand alone PDF and include that in your main document with \includegraphics – Thruston Feb 09 '23 at 08:30
  • Noted with many thanks. Lets say I want to change the font type to times new roman, which commands should I add to achieve that? – itc Feb 09 '23 at 11:50
  • For tutorials on MP have a look here. For the specifics of handling text in MP see chapters 11 and 12 of this. – Thruston Feb 09 '23 at 12:06
  • But to change to Times in the above example, just change "phvr8r" to "ptmr8r". – Thruston Feb 09 '23 at 12:07
  • Thank you very much @Thruston. – itc Feb 09 '23 at 12:46