7

I'm fairly new to Asymptote, and am trying to create a basic number line using a for loop. I would like the for loop to also label the points for me. Could somebody tell me how to fix this?

\documentclass{article}

\begin{document}

\begin{figure}[hbt]
\centering
\begin{asy}
    unitsize(0.1inch);

    draw((0,0)--(50,0),);
    draw((0,0)--(1,1));
    draw((0,0)--(1,-1));
    draw((50,0)--(49,1));
    draw((50,0)--(49,-1));

    draw((25,1)--(25,-1));
    label("$0$",(25,0)+2*dir(270));

    for(int i=1; i<12; ++i)
    {
        draw((25+2*i,1)--(25+2*i,-1));
        label("$i$",(25+2*i,0)+2*dir(270));
    }

\end{asy}
\end{figure}

\end{document}

When run, almost everything displays the way I would expect it to, except instead of labeling the numbers, it labels everything with my counter variable.

Also, earlier my system was working fine, but now it's stopped creating the .pdf files for the images. For example, my first image is supposed to be Neighborhoods-1.pdf. It creates the .asy file, but not the .pdf file. Can somebody tell me how to fix this?

1
  • Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Adam Liter
    Commented Jan 16, 2014 at 18:06

1 Answer 1

9

The command label("$i$",(25+2*i,0)+2*dir(270)); indeed typeset a name of the counter in math mode $i$ for every tick. To typeset values of i, use "$"+string(i)+"$" as a label:

\documentclass{article}
\usepackage[inline]{asymptote}
\begin{document}
\begin{figure}[hbt]
\centering
\begin{asy}
    unitsize(0.1inch);
    draw((0,0)--(50,0));
    draw((0,0)--(1,1));
    draw((0,0)--(1,-1));
    draw((50,0)--(49,1));
    draw((50,0)--(49,-1));

    draw((25,1)--(25,-1));
    label("$0$",(25,0)+2*dir(270));

    for(int i=1; i<12; ++i){
        draw((25+2*i,1)--(25+2*i,-1));
        label("$"+string(i)+"$",(25+2*i,0)+2*dir(270));
    }

\end{asy}
\end{figure}
\end{document}

enter image description here

3
  • Thank you! g.kov, do you know how to fix the other problem I listed?
    – Higgie2718
    Commented Jan 16, 2014 at 18:46
  • @Higgie2718: Usually, missing .pdf files mean that there are errors in the asy code (there is a typo in the MWE). I suggest to prepare your images as a compilable standalone .asy files first and then include them in the .tex document. Also, if you really need to use inline asy code, it is better to have come kind of a clear command, which deletes all the intermediate files, created by tex and asy.
    – g.kov
    Commented Jan 16, 2014 at 19:02
  • Thank you for all of your help! Edit: I feel really, really stupid. I had a misplaced comma. :/
    – Higgie2718
    Commented Jan 16, 2014 at 19:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .