Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to put some bar charts in a ConTeXt document. I'm using mkiv from TeX Live 2011, under Gentoo GNU/Linux. I tried using Gnuplot but I found mpgraph suits better my needs.

I'm using this TeX file

\usemodule[graph]
\usemodule[sarith]

\enableregime[utf-8]
\starttext

\startMPcode
beginfig(1)
  draw begingraph(12cm,6cm);
    glabel.lft(btex Eje Y etex, OUT);
    glabel.bot(btex Eje X etex, OUT);
    gdata("data.d",
        $,
        path p;
        augment.p (0,$1);
        augment.p ($2,$1);
        augment p ($2,$1 Sadd "1");
        augment p (0,$1 Sadd "1");
        gdraw p withpen pencircle scaled 2bp withcolor red;
    );
  autogrid(grid.bot,grid.lft) withcolor .85white;
  endgraph;
endfig;
\stopMPcode
\stoptext

and this data file

1 4
2 2
3 1
4 2
5 4

as a minimal 'working' example.

However, some strange is happening: I can't use any operator from sarith module (used in mpgraph module for drawing boxes). I got this output with ConTeXt. I tried compiling with Metapost, although I got no errors or warnings the output .dvi is corrupted.

I've tried also with TeX Live 2012 under Windows 7. I can't compile there because of the sarith thing, but when I comment that lines it works. But doing the same thing in my Gentoo box works, excepting that text for labels or axis doesn't appear, just a very little square. I don't understand what is happening, even the example from the ConTeXt wiki doesn't work. Clues? Any help is very appreciated!

share|improve this question
    
Try to take off beginfig(1)/endfig when you run with context, –  morbusg Sep 2 '12 at 5:24
    
BTW, you should also check the pgfplots package (which is supposed to work with ConTeXt as well). Metapost provides only a limited plotting capabilities. –  Aditya Sep 2 '12 at 6:04
add comment

2 Answers 2

up vote 5 down vote accepted

You need to replace \usepackage[sarith] with

\startMPinitializations
input sarith;
\stopMPinitializations

sarith is not a ConTeXt module, rather it is a metapost package, so needs to be included at the metapost end.

Moreover, as morbusg pointed out, there is no need to add beginfig(1)...endfig in a MPcode environment. With these two changes, the following works:

\usemodule[graph]
\startMPinitializations
input sarith;
\stopMPinitializations

\starttext

\startMPcode
  draw begingraph(12cm,6cm);
    glabel.lft(btex Eje Y etex, OUT);
    glabel.bot(btex Eje X etex, OUT);
    gdata("data.d",
        $,
        path p;
        augment.p (0,$1);
        augment.p ($2,$1);
        augment p ($2,$1 Sadd "1");
        augment p (0,$1 Sadd "1");
        gdraw p withpen pencircle scaled 2bp withcolor red;
    );
  autogrid(grid.bot,grid.lft) withcolor .85white;
  endgraph;
\stopMPcode
\stoptext
share|improve this answer
    
You're a genius, you saved my life again! Thank you so much! In fact, fonts showed up! Now I will look up for how change them in Metapost. Thanks! –  acidrums4 Sep 2 '12 at 11:02
    
If you are using MkIV, just change the main doucment font and the change will be reflected in metapost as well. –  Aditya Sep 2 '12 at 12:57
    
Nope, it only changes the font for glabel. Numbers on grids are plotted in cmr and labels with the default LaTeX fixed font. –  acidrums4 Sep 2 '12 at 13:02
    
Can you create a minimal example? If I add \setupbodyfont[times] in the above example, only TeXGyre Theremes is used. –  Aditya Sep 2 '12 at 14:37
    
Here's the example, here's the typescript I'm using (can't share the font due to its license) and the data file. I added a third column values 'cause I need to plot text labels on the axis, but I don't know how to workaround with the spaces on labels. I found out that mpgraph is rendering text with math font, that's because the typescript fails. Just changed \setupbodyfont[times] to \setupbodyfont[droid], it didn't work either. I looked for type-win.mkiv but don't know how it does the trick. –  acidrums4 Sep 2 '12 at 17:13
show 3 more comments

To change the font for the numbers on the grids I discovered that if I manually set the grid values, instead of using the automatic set, the font is converted to the document format. Here is an example from John Handy's MPGraph documentation with my revision:

Original

draw begingraph(6.5cm,4.5cm);
    setrange(80,0, 90,whatever);
        glabel.bot(btex Year etex, OUT);
        glabel.lft(btex \vbox{\hbox{Emissions in} \hbox{thousands of}
            \hbox{metric tons} \hbox{(heavy line)}}etex, OUT);
        gdraw "lead.d" withpen pencircle scaled 1.5pt;
        autogrid(,otick.lft);
        setcoords(linear,linear);
    setrange(80,0, 90,whatever);
        glabel.rt(btex \vbox{\hbox{Micrograms} \hbox{per cubic}
            \hbox{meter of air} \hbox{(thin line)}}etex, OUT);
    gdraw "lead.d";
        autogrid(otick.bot,otick.rt);
endgraph;

with

draw begingraph(6.5cm,4.5cm);
    setrange(79.5,0, 90.5,80) ;
    glabel.bot(btex Year etex, OUT) shifted(-8pt,-2pt);
    glabel.lft(btex \vbox{% -- \hss fills space horizontally; \strut fixes line space
        \hbox to 5cm {\hss \strut Emissions in thousands of \hss}
        \hbox to 5cm {\hss \strut metric tons (solid line) \hss}
    }
    etex, OUT) shifted(83pt,17pt) rotated 90;
    gdraw "lead.d" withpen pencircle scaled 1pt;
    itick.lft(format("\%2",0),0) withcolor white;
    itick.lft(format("\%2",20),20) withcolor white;
    itick.lft(format("\%2",40),40) withcolor white;
    itick.lft(format("\%2",60),60) withcolor white;
    itick.lft(format("\%2",80),80) withcolor white;
    frame.lft withcolor white ;

    otick.bot(format("\%2",80),80) withcolor white;
    otick.bot(format("\%2",82),82) withcolor white;
    otick.bot(format("\%2",84),84) withcolor white;
    otick.bot(format("\%2",86),86) withcolor white;
    otick.bot(format("\%2",88),88) withcolor white;
    otick.bot(format("\%2",90),90) withcolor white;
endgraph;

Try this out.

share|improve this answer
    
Sorry, I just saw this today. It worked well! Thanks –  acidrums4 Jan 15 '13 at 14:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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