On Tue, 12 Apr 2016, Summers, Peter wrote:
Hi all,
I'm trying to write a script that will produce a LaTeX table as output - basically a
customized version of tabprint. Here's the basic idea:
<hansl>
matrix doh = mnormal(1,3) # this would be a coefficient vector, e.g.
string fmt = "%.4g "
string baz = "doh[1]"
loop i=2..cols(doh) # build up the format and argument strings
fmt ~= " & %.4g "
baz ~= ", doh[$i]"
endloop
fmt
baz
sprintf Foo "\"%s\"\n", fmt
Foo
# this printf is what I want, but I want to use the "fmt" string above
printf "%.4g & %.4g & %.4g\n", @baz
# this seems like it should work, but I get a "syntax error"
sprintf bar Foo, @baz
</hansl>
I guess it's time to replace the doc for the "sprintf" command with
a "deprecated" statement plus pointer to the sprintf() function,
which is now the recommended form.
<hansl>
matrix doh = mnormal(1,3)
string fmt = "%.4g "
string args = "doh[1]"
loop i=2..cols(doh) -q
fmt ~= " & %.4g "
args ~= ", doh[$i]"
endloop
# assuming you want a terminating newline
fmt ~= "\n"
Foo = sprintf(fmt, @args)
Foo
</hansl>
We recommend, these days, that string substitution be kept to a
minimum, but the "args" argument requires it in context, so OK.
Allin