我看过之前的 tikz 示例,与这个类似,但那个示例无法编译,我得到了 101 个错误!如何在 tikz 中编写下图?谢谢,Diegis。
答案1
一种可能性是:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,shapes,fit,calc}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}
\begin{document}
\begin{tikzpicture}[
every node/.style={on grid},
setA/.style={fill=blue,circle,inner sep=2pt},
setC/.style={fill=red,rectangle,inner sep=2pt},
every fit/.style={draw,fill=blue!15,ellipse,text width=25pt},
>=latex
]
% set A
\node[setA,label=left:$a$] (a) {};
\node [setA,below = of a,label=left:$b$] (b) {};
\node [setA,below = of b,label=left:$c$] (c) {};
\node[above=of a,anchor=south] {$A$};
% set B
\node[inner sep=0pt,right=3cm of a] (x) {$x$};
\node[below = of x] (y) {$y$};
\node[inner sep=0pt,below = of y] (z) {$z$};
\node[above=of x,anchor=south] {$B$};
% set C
\node[setC,label=right:$m$,right = 3cm of x] (m) {};
\node[setC,label=right:$n$,below = of m] (n) {};
\node[setC,label=right:$p$,below = of n] (p) {};
\node[above=of m,anchor=south] {$C$};
% the arrows
\draw[->,shorten >= 3pt] (a) -- node[label=above:$f$] {} (x);
\draw[->,shorten >= 3pt] (b) -- node[label=above:$f$] {} (x);
\draw[->] (c) -- node[label=above:$g$] {} (y);
\draw[->,shorten <= 3pt] (x) -- node[label=above:$h$] {} (m);
\draw[->] (n) -- node[label=above:$u$] {} (y);
% the boxes around the sets
\begin{pgfonlayer}{background}
\node[fit= (a) (c) ] {};
\node[fit= (x) (z) ] {};
\node[fit= (m) (p)] {};
\end{pgfonlayer}
\end{tikzpicture}
\end{document}
答案2
为了消磨时间,我用 MetaPost 画了这幅画,借助boxes
包装。结果当然是完美的。
使用 LuaLaTeX 执行。
\documentclass[border=3bp]{standalone}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
input boxes;
u := 3cm; v := 1.5cm; % Horizontal and vertical spaces between consecutive nodes
path ellipse[];
ellipse1 = fullcircle xscaled .6u yscaled 3.2v;
for i = 2, 3: ellipse[i] = ellipse[i-1] shifted (u, 0); endfor
def midlabelarrow(expr A, B, labl) = drawarrow A--B; label.top(labl, .5[A,B]); enddef;
beginfig(1);
for i = 1, 2, 3:
fill ellipse[i] withcolor .95[blue, white]; draw ellipse[i];
endfor
circmargin := 2bp; forsuffixes i = A, B, C: circleit.i(); endfor
boxit.X("$x$"); boxit.Y("$y$"); boxit.Z("$z$");
defaultdx := 2bp; defaultdy := 2bp; forsuffixes i = M, N, P: boxit.i(); endfor
B.c = origin = A.c - (0, v) = C.c + (0, v);
Y.c = (u, 0) = X.c - (0, v) = Z.c + (0, v);
N.c = (2u, 0) = M.c - (0, v) = P.c + (0, v);
drawunboxed(X, Y, Z);
forsuffixes i = A, B, C: fill bpath i withcolor blue; endfor
forsuffixes i = M, N, P: fill bpath i withcolor red; endfor
midlabelarrow(A.e, X.w, "$f$"); midlabelarrow(B.e, X.sw, "$f$");
midlabelarrow(C.e, Y.w, "$g$"); midlabelarrow(X.e, M.w, "$h$");
midlabelarrow(N.w, Y.e, "$u$");
label.lft("$b$", B.w); label.lft("$a$", A.w); label.lft("$c$", C.w);
label.rt("$n$", N.e); label.rt("$m$", M.e); label.rt("$p$", P.e);
labeloffset := 6bp;
label.top("{\large $A$}", point 2 of ellipse1);
label.top("{\large $B$}", point 2 of ellipse2);
label.top("{\large $C$}", point 2 of ellipse3);
endfig;
\end{mplibcode}
\end{document}