METAPOST: using loop variables in methods

Dear stackoverflowers,

I recently ran into a problem while playing with METAPOST. When drawing something using a loop macro for a macro, I needed the value of the loop variable to be displayed correctly inside the label, however, I could not figure out how to do this, and Mr. Google could not help me. The following is an example of the code I used:

for i=1 upto N: label(btex $here should be the value of i$, some_position); endfor; 

Any help would be fixed:]

+4
source share
1 answer

at first there etex missing before , some_position . Everything between btex and etex taken as a string. This is not interpreted. To do this, the contents of the string must first be calculated by TEX() . Example:

 prologues := 2; input tex; verbatimtex \documentclass[12pt,a4paper]{article} \usepackage[T1]{fontenc} \usepackage[ansinew]{inputenc} etex; beginfig(0); n := 10; for i := 1 upto n: label.lrt(TEX("$i = "&decimal(i)&"$"),(0,i*1cm)); endfor; endfig; 

If you want to use LaTeX-Struktures, you need to modify the original TEX () as follows:

 vardef TEX primary s = write "verbatimtex" to "mptextmp.mp"; write "\documentclass[12pt]{article}" to "mptextmp.mp"; write "\usepackage[T1]{fontenc}" to "mptextmp.mp"; write "\usepackage[ansinew]{inputenc}" to "mptextmp.mp"; write "\usepackage{amsmath,amssymb}" to "mptextmp.mp"; write "\begin{document}" to "mptextmp.mp"; write "etex" to "mptextmp.mp"; write "btex "&s&" etex" to "mptextmp.mp"; write EOF to "mptextmp.mp"; scantokens "input mptextmp" enddef; 

Hope that helps

V. Tue

+3
source

All Articles