HowTo: Using dynamic trace information in the graph legend

I was trying to fix an often (for me) occurring and annoying problem, and might as well post the solution I found in the hope it may be useful for someone. Of course I would love to hear a different or probably much simpler solution, if there is one.

I often compare data by scaling the intensity and offsetting the traces until they are matched for a first glance. Then I of course want to show the offsets or scaling factors somewhere within the graph. So far I attached a text box, which means I had to manually update everything if I made small changes. It would be much more convenient if these parameters just show up automatically somewhere. A possibility is to show these values inside the legend, but Igor provides no ready "click-and-go" solution for this (only limited, and only in case of tags). So I decided to look into dynamic escape codes: It is possible to print something in the legend by writing \{some generated number or text here}.

First it is necessary to bring the needed information into a simple string or variable form within an one-liner. TraceInfo provides all information you may need about a specific trace. I want parameters from the "RECREATION" Keyword, so StringByKey will get me this. Some parameters are straightforward, like for example:
StringByKey("msize", TraceInfo("","",0), "=")

will get me the marker size from the first (0) trace in the graph. I usually want to know the offset parameters, which are a bit more tricky. The values come in pairs like {somenumber,somenumber}. To get the simple numbers I need to get rid of the brackets and choose the appropriate number (i.e., x or y). Thins can get rather long, but will do the job:
StringFromList(1, ReplaceString("{", ReplaceString("}",StringByKey("offset(x)", TraceInfo("","",0), "="),""),""), ",")

will give me the y offset (1 in StringfromList) of the first trace. So putting this into the escape code prints the number into the legend.
Since it has to be an one-line inside the legend definition, you can't use things like sscanf. But it is also possible to write a little helper function for this. Using the example code from above:
Function/S ExtractOffset(trace,which)
    Variable trace,which
    return StringFromList(which, ReplaceString("{", ReplaceString("}",StringByKey("offset(x)", TraceInfo("","",trace), "="),""),""), ",")

So using
the y offset is: \{ExtractOffset(0,1)}

will give me a nice legend which updates automatically.
Beautiful! Thanks.

I seem to remember doing something like this once many ages ago in many tiny steps. I wonder if a regex search would be "easier" to use?

J. J. Weimer
Chemistry / Chemical & Materials Engineering, UAHuntsville
Yeah, I guess you can keep things rather short using sscanf or splitstring, but it probably always needs a helper function for these. The string extraction method can be used directly, albeit making things a bit cluttered.