METAPOST: using loop variables in labels

不想你离开。 提交于 2019-12-22 08:45:08

问题


Dear stackoverflowers,

recently, while playing around with the METAPOST enviroment, I encountered a problem. While drawing something using the loop 'for' macro I needed the value of the loop variable to be correctly displayed inside a label, however I could not figure out how to do that and Mr.Google was unable to help me. Below 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 kind of help will be appreaciated :]


回答1:


at first there's a etex missing before , some_position. All between btex and etex is taken as a string. It is not interpreted. For this purpose the contents of the string has to be calculated first 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 have to modify the original TEX() in this way:

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. W.



来源:https://stackoverflow.com/questions/15930810/metapost-using-loop-variables-in-labels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!