Skip to content

Commit 664d1ac

Browse files
committed
Add guideline for TikZ users
1 parent a9f73cb commit 664d1ac

File tree

2 files changed

+278
-0
lines changed

2 files changed

+278
-0
lines changed

manual/manual.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
intro
1616

17+
tikz
18+
1719
graphics
1820

1921
path

manual/tikz.rst

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
2+
************************
3+
Guideline for TikZ users
4+
************************
5+
6+
The TikZ package is a powerful tool for creating graphics in TeX. It is
7+
based on the pgf package and allows you to create graphics programmatically.
8+
9+
If you are familiar with TikZ, this document provides a translation for
10+
common TikZ commands to the corresponding PyX commands.
11+
12+
The code below assumes you have executed the preparations listed in
13+
:ref:`graphics`.
14+
15+
Simple path operations
16+
======================
17+
18+
``\draw (0, 0)--(1, 1);``
19+
20+
::
21+
22+
c.stroke(path.line(0, 0, 1, 1));
23+
24+
``\draw (0, 0)--(1, 1)--(2, 1);``
25+
26+
::
27+
28+
c.stroke(path.path(path.moveto(0, 0), path.lineto(1, 1), path.lineto(2, 1)));
29+
30+
``\draw (0bp, 0bp)--(100bp, 100bp)--(200bp, 100bp);``
31+
32+
::
33+
34+
c.stroke(path.path(path.moveto_pt(0, 0), path.lineto_pt(100, 100), path.lineto_pt(200, 100)));
35+
36+
.. note ::
37+
The suffix ``_pt`` is used to specify the coordinates in PostScript.
38+
The unit in TeX is ``bp`` (big point), which is equivalent to 1/72 inch.
39+
TeX's ``pt`` is equivalent to 1/72.27 inch instead.
40+
41+
``\draw (0, 0)--++(1, 1) ++(1, 0)--++(1, 0);``
42+
43+
::
44+
45+
c.stroke(path.path(path.moveto(0, 0), path.rlineto(1, 1),
46+
path.rmoveto(1, 0), path.rlineto(1, 0)));
47+
48+
``\draw (0, 0)..controls (1, 0) and (3, 2)..(3, 3);``
49+
50+
::
51+
52+
c.stroke(path.curve(0, 0, 1, 0, 3, 2, 3, 3));
53+
54+
or equivalently::
55+
56+
c.stroke(path.path(path.moveto(0, 0), path.curveto(1, 0, 3, 2, 3, 3)));
57+
58+
``\draw (0, 0) to [out=0, in=-45] (1, 1);``
59+
60+
::
61+
62+
from pyx.metapost.path import beginknot, endknot, smoothknot, tensioncurve
63+
c.stroke(metapost.path.path([beginknot(0, 0, angle=0), tensioncurve(),
64+
endknot(1, 1, angle=135)]))
65+
66+
``\draw plot [smooth] coordinates{(0,0) (1,1) (2,0) (1,-1)};``
67+
68+
::
69+
70+
from pyx.metapost.path import beginknot, endknot, smoothknot, tensioncurve
71+
c.stroke(metapost.path.path([
72+
beginknot(0, 0), tensioncurve(), smoothknot(1, 1), tensioncurve(),
73+
smoothknot(2, 0), tensioncurve(), endknot(1, -1)]))
74+
75+
.. note ::
76+
The algorithm used by PyX is the Hobby algorithm, which produces
77+
much better results than the default algorithm used by TikZ.
78+
.. seealso ::
79+
`A question on TeX.StackExchange <https://tex.stackexchange.com/q/33607>`_.
80+
81+
``\draw plot [smooth cycle] coordinates{(0,0) (1,1) (2,0) (1,-1)};``
82+
83+
::
84+
85+
from pyx.metapost.path import beginknot, endknot, smoothknot, tensioncurve
86+
c.stroke(metapost.path.path([
87+
smoothknot(0, 0), tensioncurve(), smoothknot(1, 1), tensioncurve(),
88+
smoothknot(2, 0), tensioncurve(), smoothknot(1, -1), tensioncurve()]))
89+
90+
``\draw (2, 3) rectangle (12, 33);``
91+
92+
::
93+
94+
c.stroke(path.rect(2, 3, 10, 30));
95+
96+
``\draw (2, 3) circle (1);``
97+
98+
::
99+
100+
c.stroke(path.circle(2, 3, 1));
101+
102+
``\draw (1, 0) arc [start angle=0, end angle=90, radius=1];``
103+
104+
::
105+
106+
c.stroke(path.path(path.arc(0, 0, r=1, angle1=0, angle2=90)));
107+
108+
.. note ::
109+
In PyX, the coordinate must be the center of the circle
110+
instead of the starting point of the arc.
111+
112+
113+
``\draw (1, 0) arc [start angle=90, end angle=0, radius=1];``
114+
115+
::
116+
117+
c.stroke(path.path(path.arcn(0, 0, r=1, angle1=90, angle2=0)));
118+
119+
``\draw [rounded corners=2] (0, 0) rectangle (1, 2);``
120+
121+
::
122+
123+
c.stroke(path.rect(0, 0, 1, 2), [deformer.smoothed(radius=0.1)])
124+
125+
or::
126+
127+
c.stroke(deformer.smoothed(radius=0.1).deform(path.rect(0, 0, 1, 2)))
128+
129+
.. note ::
130+
Unlike TikZ, the rounded corners are not exactly part of a circle
131+
with the specified radius, even if the turn angles are 90 degrees.
132+
133+
Filled paths and colors
134+
=======================
135+
136+
``\fill (2, 3) circle (1);``
137+
138+
::
139+
140+
c.fill(path.circle(2, 3, 1));
141+
142+
``\fill [yellow] (2, 3) circle (1);``
143+
144+
::
145+
146+
c.fill(path.circle(2, 3, 1), [color.cmyk.Yellow]);
147+
148+
.. seealso ::
149+
:ref:`colorname`
150+
151+
``\filldraw [fill=yellow, draw=red] (2, 3) circle (1);``
152+
153+
::
154+
155+
c.draw(path.circle(2, 3, 1), [deco.filled([color.cmyk.Yellow]), deco.stroked([color.rgb.red])])
156+
157+
Nodes and text
158+
==============
159+
160+
``\node at (0, 0) {content};``
161+
162+
::
163+
164+
c.text(0, 0, "content", [text.halign.boxcenter, text.valign.middle])
165+
166+
``\node [text=red] at (0, 0) {content};``
167+
168+
::
169+
170+
c.text(0, 0, "content",
171+
[text.halign.boxcenter, text.valign.middle, color.rgb.red])
172+
173+
``\node [above] at (0, 0) {content};``
174+
175+
::
176+
177+
c.text(0, 0, "content", [text.halign.boxcenter, text.valign.bottom])
178+
179+
``\node [below] at (0, 0) {content};``
180+
181+
::
182+
183+
c.text(0, 0, "content", [text.halign.boxcenter, text.valign.top])
184+
185+
``\node [base left] at (0, 0) {content};``
186+
187+
::
188+
189+
c.text(0, 0, "content", [text.halign.right, text.valign.baseline])
190+
191+
``\node [base right] at (0, 0) {content};``
192+
193+
::
194+
195+
c.text(0, 0, "content", [text.halign.left, text.valign.baseline])
196+
197+
``\node [left] at (0, 0) {content};``
198+
199+
::
200+
201+
c.text(0, 0, "content", [text.halign.right, text.valign.middle])
202+
203+
``\node [right] at (0, 0) {content};``
204+
205+
::
206+
207+
c.text(0, 0, "content", [text.halign.left, text.valign.middle])
208+
209+
``\node [anchor=base] at (0, 0) {content};``
210+
211+
::
212+
213+
c.text(0, 0, "content", [text.halign.center, text.valign.baseline])
214+
215+
``\node [draw, fill=yellow, rectangle] at (0, 0) {Boxed text};``
216+
217+
::
218+
219+
tbox = text.text(0, 0, r"Boxed text",
220+
[text.halign.boxcenter, text.valign.middle])
221+
tpath = tbox.bbox().enlarged(3*unit.x_pt).path()
222+
c.draw(tpath, [deco.filled([color.cmyk.Yellow]), deco.stroked()])
223+
c.insert(tbox)
224+
225+
``\draw (0, 0) -- (10, 1) node [midway, right] {Hello};``
226+
227+
::
228+
229+
c.stroke(path.line(0, 0, 10, 1),
230+
[deco.text("Hello",
231+
[text.halign.left, text.valign.middle],
232+
relarclenpos=0.5)])
233+
234+
235+
Arrows
236+
======
237+
238+
``\draw [->] (0, 0) -- (1, 1);``
239+
240+
::
241+
242+
c.stroke(path.line(0, 0, 1, 1), [deco.earrow])
243+
244+
.. seealso ::
245+
:ref:`arrows`
246+
247+
``\draw [<-] (0, 0) -- (1, 1);``
248+
249+
::
250+
251+
c.stroke(path.line(0, 0, 1, 1), [deco.barrow])
252+
253+
``\draw [<->] (0, 0) -- (1, 1);``
254+
255+
::
256+
257+
c.stroke(path.line(0, 0, 1, 1), [deco.barrow, deco.earrow])
258+
259+
Path intersections
260+
==================
261+
262+
TikZ code::
263+
264+
\draw [name path=a] (0, 0) circle (2);
265+
\draw [name path=b] (1, 0) circle (2);
266+
\draw [name intersections={of=a and b, by={e, f}}, red] (e) -- (f);
267+
268+
PyX code::
269+
270+
a=path.circle(0, 0, 2)
271+
c.stroke(a)
272+
b=path.circle(1, 0, 2)
273+
c.stroke(b)
274+
[[a_t0, a_t1], [b_t0, b_t1]] = a.intersect(b)
275+
e, f=a.at(a_t0), a.at(a_t1)
276+
c.stroke(path.line(*e, *f), [color.rgb.red])

0 commit comments

Comments
 (0)