I was quite impressed by the figures generated by TikZ and wanted to try TikZ to generate a few figures (please see the attached image drawn using an external WYSWYG program). When I tried to reproduce the figures myself using TikZ I found it very difficult to control the shapes in required positions. What is the simplest method to draw!these figures using TikZ?
[Tex/LaTex] Beginner: Drawing using Tikz
technical-drawingtikz-pgf
Related Solutions
Complete LaTeX documents used to generate the figures in this answer are available as a Gist on GitHub.
One way to solve the "putting text over arrows" question is to use inline nodes and relative positioning:
\begin{tikzpicture}
\draw[->] (0,0) -- (5,0) node[above]{Hello, world!};
\end{tikzpicture}
You can also use named nodes and coordinates combined with relative positioning. A \coordinate
is just like a \node
without a text area (they may still have shapes). Naming your nodes and coordinates allows you to re-use information about their position without re-typing the locations. Using relative positioning constructs (provided by the TikZ positioning
library), like left = 1ex of A
, allows you to position nodes/coordinates relative to other nodes/coordinates without working out the mathematics and geometry in your head.
\begin{tikzpicture}[
Name/.style = {font={\bfseries}}
]
\coordinate (A) at (0,0);
% ^---- A name for the node/coordinate
\coordinate (B) at (5,1);
\coordinate (C) at (3,4);
\draw (A) -- (B) -- (C) -- cycle;
\node (A-label) [left=1ex of A, Name] {A};
\node (B-label) [right=1ex of B, Name] {B};
\node (C-label) [above=1ex of C, Name] {C};
\node [above left = 2 em of C] {This is a relatively positioned node!}
edge[out=270,in=180,thick,->] (C-label);
\end{tikzpicture}
See Tutorial 2 "A Petri-Net for Hagen" in the TikZ manual for an excellent step-by-step walkthrough of named nodes and relative positioning.
I prefer constructing figures using relative positioning as opposed to using a GUI program because I can change the look of the figure by adjusting the locations of a few "base coordinates". In this case, I could draw a completely different triangle by moving the locations of coordinates A, B and C and for most adjustments the rest of the figure would just fall into place. Learning to set up figures like this takes practice but once you get the it down it can save a bunch of time when it comes to fine-tuning the image when compared to a GUI program.
Finally, as to your question about GUI programs, I would recommend Inkscape- it is a very robust, open source editor for vector graphics that is similar to Adobe Illustrator. There is even a plugin available that generates TikZ code from Inkscape graphics.
This is a MWE
with the Asymptote
,
// blobs.asy :
//
size(7cm);
import graph;
import patterns;
import fontsize;
defaultpen(fontsize(9pt));
texpreamble("\usepackage{lmodern}");
pen hatchPen=orange+0.4bp;
pen borderPen=deepblue+1bp;
pen arrPen=red+1bp;
pen areaBG=palegreen;
pair[] dots={
(25,280),
(60,280),
(25,140),
};
pair star=(60,137);
guide[] arrows={(25,265)--(25,160),
(60,265){dir(-90)}..(60,200)..(40,160),
};
guide[] markedArrows={
(150,280)--(200,280),
(150,140)--(200,140),
(360,240)--(360,200),
};
pair[] xArea={
(409,257),
(415,272),
(400,289),
(383,291),
(367,292),
(358,300),
(355,307),
(344,317),
(330,317),
(320,312),
(311,319),
(303,308),
(302,303),
(306,298),
(306,287),
(312,277),
(319,269),
(330,267),
(346,260),
(366,259),
};
pair[] xAreaInside={
(341,272),
(332,282),
(333,301),
(339,312),
(385,270),
};
pair[] yArea={
(379,35),
(398,36),
(422,39),
(453,52),
(468,63),
(484,86),
(478,107),
(453,110),
(426,111),
(393,129),
(361,149),
(349,157),
(335,157),
(323,173),
(303,173),
(283,153),
(267,134),
(266,113),
(280,100),
(297,93),
(313,82),
(329,64),
(341,45),
};
pair[] smallShArea={
(400,41),
(403,46),
(399,51),
(406,58),
(408,66),
(398,63),
(387,59),
(380,57),
(369,62),
(360,57),
(366,48),
(376,46),
(384,48),
};
pair[] bigShArea={
(380,74),
(390,83),
(396,99),
(389,111),
(376,115),
(358,123),
(340,130),
(326,144),
(307,151),
(295,146),
(295,140),
(299,129),
(304,119),
(321,116),
(334,107),
(348,87),
};
pair[] yAreaInside={
(315,136),
(370,97),
(436,85),
(460,82),
};
dot(dots,UnFill);
draw(arrows[0],arrPen,Arrow(HookHead,size=4));
draw(arrows[1],arrPen,Arrow(HookHead,size=4));
for(int i=0;i<markedArrows.length;++i){
draw(markedArrows[i],arrPen,Arrow(HookHead,size=4));
}
filldraw(graph(xArea,operator..)..cycle,areaBG,borderPen);
draw(graph(xAreaInside[0:4],operator..),borderPen);
dot(xAreaInside[1:3],UnFill);
label("$X$",xAreaInside[4]);
filldraw(graph(yArea,operator..)..cycle,areaBG,borderPen);
guide gsmallShArea=graph(smallShArea,operator..)..cycle;
guide gbigShArea=graph(bigShArea,operator..)..cycle;
add("hatch",hatch(1mm,hatchPen));
fill(gsmallShArea,pattern("hatch"));
fill(gbigShArea,pattern("hatch"));
draw(gsmallShArea,borderPen);
draw(gbigShArea,borderPen);
dot(yAreaInside[0],UnFill);
label("$f(X)$",yAreaInside[1]);
label("*",yAreaInside[2]);
label("Y",yAreaInside[3]);
label("$\alpha$",markedArrows[0],N);
label("$\beta$",markedArrows[1],N);
label("$f$",markedArrows[2],E);
label("*",star);
To get a standalone blobs.pdf
, run asy -f pdf blobs.asy
,
it will automatically run pdflatex
to typeset the labels.
Best Answer
With next code you can obtain first example. All others can be easily done after reading first tutorial in
TiKZ
documentation (pgfmanual).