Drawing with TikZ
LaTeX Lesson 9 2:04 English narration · English + 中文 subtitles burned in
Chapters
Transcript
You can draw diagrams with code, without any image file, using the TikZ package.
你可以用代码画图,不需要任何图片文件,靠的是 TikZ 宏包。
Load it once, then draw inside a tikzpicture environment.
加载它一次,然后在 tikzpicture 环境里画。
Draw from nought nought to two one means: start at the origin and go to the point two across and one up — you can see both points on the grid.
\draw (0,0) -- (2,1) 的意思是: 从原点出发,走到"向右 2、向上 1"的那个点—— 两个点你都能在网格上看到。
And notice the last character: every TikZ command ends with a semicolon.
再注意最后一个字符:每一条 TikZ 命令都以分号结束。
Chain more points with two dashes and you can draw any shape.
用两个短横继续接更多的点,你就能画出任何形状。
Three points here, and then the word cycle, which returns to the first point and closes the triangle.
这里有三个点,然后是 cycle 这个词, 它回到第一个点,把三角形闭合。
Leave cycle off and the drawing has a gap where the third side should be — which is a useful thing to know when a shape refuses to close.
不写 cycle,这幅图在第三条边的位置就会有一道缺口—— 当一个形状不肯闭合时,知道这一点很有用。
Two shapes do not need a path at all.
有两种形状根本不需要路径。
Circle takes a centre and a radius — draw at two, one point five, circle one.
circle 接一个圆心和一个半径——\draw (2,1.5) circle (1)。
Rectangle takes two opposite corners and fills in the other two for you.
rectangle 接两个对角,另外两个角由它替你补上。
Both still end with a semicolon, and both still live inside the same tikzpicture.
两者仍然都以分号结束,也仍然都住在同一个 tikzpicture 里面。
Why bother, when you could draw it in an app and paste a picture?
既然可以在软件里画好再贴一张图,何必费这个事?
Three reasons.
三个理由。
It is vector graphics, so it stays sharp at any size, on screen and in print.
它是矢量图,所以在屏幕上和印刷时,放到任何尺寸都清晰。
It is exact: the point two one is exactly where you said, not wherever your hand landed.
它是精确的:点 (2,1) 就正好在你说的位置, 而不是你手落在哪儿就是哪儿。
And change one number and the drawing updates — which is why textbook geometry figures and circuit diagrams are usually drawn this way.
而且改一个数字,图就更新了—— 这就是为什么教科书里的几何图和电路图通常都是这样画出来的。
Four things to take with you.
带走四点。
One: load tikz, then draw inside a tikzpicture.
第一:加载 tikz,然后在 tikzpicture 里面画。
Two: a coordinate is x across and y up.
第二:坐标是"向右 x,向上 y"。
Three: two dashes chain points, and cycle closes the shape.
第三:两个短横把点串起来,cycle 把形状闭合。
Four: every draw command ends with a semicolon.
第四:每一条 \draw 命令都以分号结束。
Now draw your triangle and press Run.
现在把你的三角形画出来,然后按 Run。