[metapost] Dynamic TeX labels in functions

John S. Walker jsw9c at uic.edu
Wed Nov 10 18:34:03 CET 2004


G'day,

I'm trying to write a function where I give a filename and axis name in 
the argumants and have the function draw a graph based
on the data in the file name and add labels to the graph.  The 
behaviour I'm looking for is the ability to read the last value from 
the file and label the final point and also to include a name at the 
extreme right

I'd like to use this with LaTeX so I'm trying to use the btex etex 
construct but I'm having little success. I'm trying Hoenig's idea of 
writing a string to a file and then reading it back, however I get an 
error message about not having filenames in macros. If I try to use 
readfrom along the lines of

next:=readfrom "sparklabels.tmp"; scantokens next;

There is an error message about using btex etex from a file

Here is a file with the macro included.

%%%Begin sparklinef.mp file
input graph;
prologues:=0;
verbatimtex
%&latex
\documentclass{article}
\usepackage{color}
\begin{document}
etex

vardef sparkline(text gname)(expr fname,w,h,upper,lower)=
   draw begingraph(w,h);
    string s,ip ;
    numeric endx, endy, gupper, glower;
    path p,q;
    pair ly, by;
    gdata(fname, s, augment.p(i, scantokens s1); );
    by:=point 0 of p;
    ly:= point infinity of p;

    endx:=xpart(ly);
    endy:=ypart(ly);
    gupper:=upper;
    glower:=lower;
    q:= (0,gupper)--(endx,gupper)--(endx,glower)--(0,glower)--cycle;
    gfill q withcolor 0.8white;

    gdraw p withcolor 0.3white;

    pickup pencircle scaled 2pt;
    gdraw ly withcolor red;
    s:="glabel.rt( btex {\scriptsize \textsf{"&decimal endy&"}} etex , 
ly)  withcolor red; glabel.lft(btex {\scriptsize \textsf{"&gname&"}} 
etex, OUT);";

    write s to "sparklabels.tmp";
    write EOF to "sparklabels.tmp";
    input "sparklabels.tmp";
    Gneedgr_ := false;
    Gneedfr_ := false;
  endgraph;
  enddef;
beginfig(1);
  sparkline("glucose")("newdata.d", 36pt, 12pt, 70, 40)
endfig;
bye;
%%%end sparklinef.mp



More information about the metapost mailing list