2

In a summary, the issue is how to show label text in Metapost.

I have a Metapost function simple_supported_single_span_load_diagram to draw a load diagram. I can call the function in Latex, and it worked well. But, on the other hand, I want to call it in pure Metapost to build an image. It sounded that Metapost didn't support the font mathsf (maybe I was wrong), this is the reason I add the if..else part by the end for latex and metapost respectively. As shown in the code below, the if..else part was commented for the latex part, it worked well as shown in the picture below. But if I umcommented the part, I got the following error:
! ! Unable to read mpx file.
l.71 label.bot (btex
$R_1$ etex, (p6 - (0, gap))); % label R1 of left ...

The first following part is the function in the file moi_test. The second part is the file test.mp to call simple_supported_single_span_load_diagram. My working environment is MiKTex 2.9. The compiling command is mpost.exe test.mp.

What is the problem? Thank you.

enter image description here

The file moi_test:

input moi_utility

vardef simple_supported_single_span_load_diagram (expr type, scale, span) =
  numeric u, len, dia, gap, force_len, UDL_len, UDL_start_height, UDL_end_height, UDL_equal_divide;
  numeric point_load_above_UDL;
  pair p[], head, tail;
  picture all;
  interim ahangle := 30;

  % initialization
  u = 1 / scale * 1mm;

  len = span * u;
  UDL_len = len / 5;
  UDL_start_height = 5mm;
  UDL_end_height = 10mm;
  UDL_equal_divide = 10;
  point_load_above_UDL = 5mm;
  dia = 2mm;
  gap = 1mm;
  force_len = 10mm;

  p0 = origin; % left support
  p1 = p0 + (len, 0); % right support
  p2 = p1 + (0, -dia / 2); % circle centre of right support
  p3 = p0 + (dia / 2, -dia); % left corner of left support
  p4 = p0 + (-dia / 2, -dia); % right corner of left support
  p5 = p0 - (0, dia + gap); % start point of left reaction force
  p6 = p5 - (0, force_len); % end point of left reaction force
  p7 = p1 - (0, dia + gap); % start point of right reaction force
  p8 = p7 - (0, force_len); % end point of right reaction force
  p9 = p0 + (len * 0.6, 0); % start point of UDL
  p10 = p9 + (UDL_len, 0); % end point of UDL
  p11 = p9 + (0, UDL_start_height); % start point of UDL - up
  p12 = p10 + (0, UDL_end_height); % end point of UDL - up
  p13 = p0 + (len * 0.3, 0); % point of point load
  p14 = p13 + (0, UDL_end_height + point_load_above_UDL); % point of point load - up
  p15 = p5 + (0, -force_len * 0.4); % dim line for UDL
  p16 = p5 + (0, -force_len * 0.9); % dim line for span
  p17 = p13 + (0, UDL_end_height + point_load_above_UDL * 0.5); % dim line for point load

  all = image(
    draw p0 -- p1; % span
    draw fullcircle scaled dia shifted p2; % circle for right support
    draw p0 -- p3 -- p4 -- cycle; % triangle for left support
    drawarrow reverse (p5 .. p6); % left reaction force line
    drawarrow reverse (p7 .. p8); % right reaction force line
    for i = 0 upto UDL_equal_divide:
      if (i = 0) or (i = UDL_equal_divide):
        if i = 0:
          draw p11 -- p12;
          drawarrow reverse (p9 .. p11); % start UDL line
        else:
          drawarrow reverse (p10 .. p12); % end UDL line
        fi
      else:
        head := p9 + (i * UDL_len / UDL_equal_divide, 0);
        tail := whatever[p11, p12]=whatever[head, (head + (0, UDL_end_height))];
        drawarrow reverse (head .. tail); % UDL lines
      fi
    endfor;
    drawarrow reverse (p13 .. p14); % point load
    dim (p5, p9, p15, 1, 0, scale, "a"); % UDL dim
    dim (p9, p10, p15, 1, 0, scale, "b"); % UDL dim
    dim (p10, p7, p15, 1, 0, scale, "c"); % UDL dim
    dim (p5, p7, p16, 1, 0, scale, "L"); % span dim
    dim (p0, p13, p17, 1, 0, scale, "A"); % point load dim
    dim (p13, p1, p17, 1, 0, scale, "B"); % point load dim

    %  if type = 0:
      label.bot (btex $R_1$ etex, (p6 - (0, gap))); % label R1 of left reaction force
      label.bot (btex $R_2$ etex, (p8 - (0, gap))); % label R2 of right reaction force
      label.bot (btex $P$ etex, (p14 + (0, 4 * gap))); % label P for point load
      label.lft (btex $q_1$ etex, 1/2[p9, p11]); % label q1 of start UDL
      label.rt (btex $q_2$ etex, 1/2[p10, p12]); % label q2 of start UDL
    %  else:
      %  label.lft (btex $\qquad\qquad$ etex, origin);
      %  label.bot (btex $\mathsf{R_1}$ etex, (p6 - (0, gap))); % label R1 of left reaction force
      %  label.bot (btex $\mathsf{R_2}$ etex, (p8 - (0, gap))); % label R2 of right reaction force
      %  label.bot (btex $\mathsf{P}$ etex, (p14 + (0, 4 * gap))); % label P for point load
      %  label.lft (btex $\mathsf{q_1}$ etex, 1/2[p9, p11]); % label q1 of start UDL
      %  label.rt (btex $\mathsf{q_2}$ etex, 1/2[p10, p12]); % label q2 of start UDL
    %  fi
  );
  draw all;
enddef;

The file test.mp:

outputformat := "png";
outputtemplate := "%j-%c.%o";
hppp := 0.5;
vppp := 0.5;
warningcheck := 0;
input moi_test
beginfig(1);
numeric scale, span;
span = 600mm;
scale = 10;
simple_supported_single_span_load_diagram (0, scale, span);
endfig;
end

@Thruston: Followed is a minimal code to show the problem.

  • case 1: as shown in the code, compiling failed and showed this error:
    ! ! Unable to read mpx file.
    l.11 label.bot (btex
    $R_1$ etex, origin); % line 11

  • case 2: if we commented line 10, and line 12-14, it worked.

  • case 3: if we commented line 10-12, and line 14, compiling failed and showed this error:
    ! ! Unable to read mpx file.
    l.13 label.bot (btex
    $\mathsf{R_1}$ etex, origin); % line 13
    Sound \mathsf is not supported in metapost. mpost.exe -tex=latex would not help.

      outputformat := "png";
      outputtemplate := "%j-%c.%o";
      hppp := 0.8; % adjust image size
      vppp := 0.8; % adjust image size
      warningcheck := 0;
    
      beginfig(1);
        numeric type;
        type := 0;
        if type = 0: % line 10
          label.bot (btex $R_1$ etex, origin); % line 11
        else: % line 12
          label.bot (btex $\mathsf{R_1}$ etex, origin); % line 13
        fi % line 14
      endfig;
    

    end

4
  • Your example is not minimal and it is not complete. You have left out the definition of dim. Can you please make a much smaller example that shows the problem, and that we can compile directly? I've down voted because of this.
    – Thruston
    Jun 20 '20 at 8:30
  • @ Thruston, the minimum code was just added.
    – warem
    Jun 20 '20 at 9:08
  • 1
    You need to add the verbatimtex preamble i showed.
    – Thruston
    Jun 20 '20 at 9:46
  • @ Thruston It worked by adding "add a small verbatimtex block with a suitable LaTeX preamble". I just reread the manual mpman.pdf. Althrough it mentioned verbatimtex..etex block, I won't know it can include Latex preamble without your help. Thank you very much.
    – warem
    Jun 20 '20 at 10:02
3

So I think the problem is that you cannot typeset $\mathsf{R_1}$ in a label.

The !Unable to read MPX file error is generated when MP finds something in your TeX input that it does not understand. In this case you have included a TeX macro -- \mathsf -- that is not part of plain TeX. So you either have to get the font you want using the built-in support for fonts provided by the "infont" operator; or you have to make Metapost use LaTeX instead of plain TeX.

Getting CM sans using infont

If all you want is plain single letter or single word labels, then you can use infont with any of the simple font names, including "cmss10".

outputformat := "png"; hppp := 1/8; vppp := 1/8;
outputtemplate := "%j%c.%{outputformat}";
beginfig(1);
label("R" infont "cmss10", origin)
endfig;
end.

Compile with mpost or mpost.exe with no special options.

Getting CM Sans using LaTeX

If you want to have subscripts or other maths features then you need to tell MetaPost to use LaTeX instead of plain TeX, and you have to add a small verbatimtex block with a suitable LaTeX preamble.

outputformat := "png"; hppp := 1/8; vppp := 1/8;
outputtemplate := "%j%c.%{outputformat}";
verbatimtex
\documentclass{article}
\begin{document}
etex
beginfig(1);
label(btex $\mathsf{R_1}$ etex, origin);
endfig;
end.

This time you need to compile with mpost -tex=latex or mpost.exe -tex=latex.

You should get something like this:

enter image description here

Full details can be found in the MetaPost manual -- or try texdoc mpman locally.

2
  • I think my question is a little confusing. I realized mathsfis not supported in metapost. That is why I added the if..else in the end. The if part is for the label in metapost and the else part is for label in Latex. The provided example worked and built the image attached. As you can see, the else part is commented, it worked in both metapost and Latex. If we unconnected the part and set variable type to 0 for metapost, it didn’t work and show the error.
    – warem
    Jun 20 '20 at 0:43
  • But \mathsf is supported by metapost! Let me edit the question to make it clearer.
    – Thruston
    Jun 20 '20 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.