summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/asy-overview/src/chapter1/chapter1.tex
blob: 40c4ecb1be90351815ff4d5ffd9b1367ee9933b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
% Chapter 1 from Asymptote tutorial Jim Hefferon
\chapter{A first graphic}
Making an \Asy{} input file is like making a \LaTeX{} input file,
so you already have a feel for the basics.
To start, 
in your favorite editor open an \Asy{} input file \path{asy/unit_circle.asy}.
\begin{minted}{Bash}
jim@millstone:~/Documents/asy_tut/src$ cd asy
jim@millstone:~/Documents/asy_tut/src/asy$ emacs unit_circle.asy  
\end{minted}
Type this text into the file and save it, or
copy it from this document's online source.
\begin{center}
  \inputminted{Asymptote}{chapter1/asy/unit_circle.asy}
\end{center}
Get the output file \path{unit_circle.pdf} by running \Asy{} on the source.
\begin{minted}{Bash}
jim@millstone:~/Documents/asy_tut/src/asy$ asy unit_circle  
\end{minted} 
%$ Make auctex recognize end of "math"
You can see the result in any PDF viewer.
\begin{center}
  \includegraphics{chapter1/asy/unit_circle.pdf}
\end{center}




\section{What do we have here?}
The input \path{unit_circle.asy} illustrates a number of things.
Globally, it shows that you must declare variables such as line~12's
\mintinline{Asymptote}{theta}, that
commands end with a semicolon,
and that comments are preceded by a double slash, 
\mintinline{Asymptote}{//}.

Locally, line 2's \mintinline{Asymptote}{settings.outformat}
variable fixes the format of output files to PDF so we needn't remember 
each time to do that from the command line.
Line~4's \mintinline{Asymptote}{import}
is like a \LaTeX{} \mintinline{TeX}{\usepackage{...}} in that
it gives us access to a module, a collection of related commands and data.
In this case it
gives us commands to make plots.
In this drawing we only use its axis-making commands 
but the second chapter has more about plots.

In line~6 the \mintinline{Asymptote}{unitsize(1.5cm)} command means that
if we describe a point $(x,y)$ then \Asy{} will interpret it as the
location $x\cdot 1.5\text{\,cm}$ and $y\cdot 1.5\text{\,cm}$ from the origin.
 
Line~9 is self-explanatory.
Lines~12 through~15 draw the line segment from the origin to the 
point labeled $(\cos\theta,\sin\theta)$.
(On line~15, in addition to drawing the dot, \Asy{} labels it and
the \mintinline{Asymptote}{E} puts that label east of the dot.)
% Note that if after seeing the drawing we decide to adjust the angle
% then we just need to change one number, on line~12. 

Finally, lines 18 and~19 draw the axes.
These commands have many options, most of which we did not use here,
and we will see more of them later.

One more thing.
\Asy{} gets the point label $(\cos\theta,\sin\theta)$ and the axis labels
by putting the given strings in a small file,
running \LaTeX{} on it, and then extracting the result back into the
output graphic.
So your labels have access to all of \LaTeX's capabilities.  





\section{Adjustments}
After you get a graphic draft there are always some tweaks.

First, here the axis labels are too big.
We will replace \mintinline{Asymptote}{"$x$"}
with \mintinline{Asymptote}{"\scriptsize $x$"}.
(We could instead omit the label by deleting the entire
string and the comma after it,
which illustrates that commands
such as \mintinline{Asymptote}{"xaxis(..)"}
can have a variable number of arguments.
While you are working you may want to have open the \Asy{} reference
for a list of the options.)

Second, the dot showing the generic point on the unit circle is too big.
In the revised source below we've adjusted the size by inserting
\mintinline{Asymptote}{dotfactor = 4} in line~23
(the default factor is $6$).

Finally, the $(\cos\theta,\sin\theta)$ label is in a different font than
the other mathematics in this overview.
We want that \Asy, when making the small \LaTeX{} document to create the
in-graphic text,
will use the same font setup as the main \path{.tex} file.
So below we added a 
\mintinline{Asymptote}{texpreamble("..")};~the 
string is long so for readability we've spread it across multiple lines.
\begin{center}
  \inputminted{Asymptote}{chapter1/asy/unit_circle_after.asy}
\end{center}
\begin{center}
  \includegraphics{chapter1/asy/unit_circle_after.pdf}
\end{center}



\section{Include the graphic in a \LaTeX{} file}
Open a new \LaTeX{} file \path{main.tex}. 
\begin{minted}{Bash}
jim@millstone:~/Documents/asy_tut/src$ emacs main.tex  
\end{minted} 
Enter this text
or copy it from this document's source.
\begin{center}
  \inputminted{TeX}{chapter1/main.tex}
\end{center} 
This document shows two ways to include the graphic.
Line~11 is straightforward because
after you've iterated through some adjustments to the figure then you   
use \LaTeX's standard \mintinline{TeX}{\includegraphics{..}}.
The other way, commented out, 
uses the \mintinline{TeX}{asymptote} \LaTeX{} package
to include the \Asy{} source file with 
\mintinline{TeX}{\asyinclude{..}}.
In this approach, getting the graphic is a three step process, where 
you run ``\mintinline{Bash}{pdflatex main}'' (of course
you can instead use ``\mintinline{Bash}{xelatex main}'' or 
``\mintinline{Bash}{lualatex main}''), then you go into
the \path{asy/} subdirectory and run 
``\mintinline{Bash}{asy <latex-filename>-1}''
(here, ``\mintinline{Bash}{asy main-1}''), then go back to the
\LaTeX{} file's directory and run ``\mintinline{Bash}{pdflatex main}'' once more.
In either case here is the one-page output.
\begin{center}
  \framebox{\includegraphics[page=1,scale=0.325,trim=0.25in 5.9in 0.25in 0.25in]{chapter1/main.pdf}}
\end{center}