8

I have a question regarding asymptote drawing in LaTeX. I'm an absolute novice in using asymptote, and I could not find any help in the manuals I found online.

Here is the problem.

Starting from this simple code

\begin{asy}
  import patterns;

  size(6cm,0);
  draw(box((0,0), (2,1)));

  pen p=1.5bp+.9red;

  pair A=(0,0), B=(0,1), C=(0.7,1), D=(2,0.35),E=(2,0);

  add("shd",hatch(H=3mm,dir=SE,red));

  filldraw(A--B--C--D--E--cycle,pattern("shd"),p);
  \end{asy}

which works fine and shades a region confined with straight lines, I want to change the segment CD to a curve $f(x)=0.7/x$. This is my naive try:

\begin{asy}
  import patterns;
  import graph;

  size(6cm,0);
  draw(box((0,0), (2,1)));

  pen p=1.5bp+.9red;

  pair A=(0,0), B=(0,1), C=(0.7,1), D=(2,0.35), E=(2,0);

  real f(real x) {return 0.7/x}

  add("shd",hatch(H=3mm,dir=SE,red));

  filldraw(A--B--C--graph(f,0.7,2)--D--E--cycle,pattern("shd"),p);
\end{asy}

However, it doesn't work.

Thank you in advance for you help.

Upko
  • 81

1 Answers1

6

For an Asymptote beginner, I'm impressed that you have solved all the difficult problems in your script! You have only forgotten a semicolon in your return statement.

Thank you for the working and non-working example code!

\begin{asy}
  import patterns;
  import graph;

  size(6cm,0);
  draw(box((0,0), (2,1)));

  pen p=1.5bp+.9red;

  pair A=(0,0), B=(0,1), C=(0.7,1), D=(2,0.35), E=(2,0);

  real f(real x) { return 0.7/x; }  // <------ ADDED SEMICOLON HERE

  add("shd",hatch(H=3mm,dir=SE,red));

  filldraw(A--B--C--graph(f,0.7,2)--D--E--cycle,pattern("shd"),p);
\end{asy}

enter image description here

James
  • 4,587
  • 1
  • 12
  • 27
  • Asymptote does not need document class? – Display Name Dec 10 '18 at 19:46
  • @ArtificialStupidity: Good point. Yes it does if run in LaTeX. I copied the code within the \begin{asy} and \end{asy} blocks into a text file and compiled it with asymptote directly. I sometimes forget that not everyone runs Asymptote in this way. – James Dec 10 '18 at 19:49
  • Thank you! I've been trying to figure out what's wrong for quite a while, but this simple thing wouldn't have crossed my mind. – Upko Dec 10 '18 at 20:48