帮助使用 tikzpicture 复制图形

帮助使用 tikzpicture 复制图形

我想模仿我所附的相同图像。我需要使箭头更粗,并在每个字母下方的括号中添加文本。如果您能告诉我需要对此代码进行哪些修改,我将不胜感激。

此致。

\begin{figure}[h]
\centering
\begin{tikzpicture}[
  node distance=1cm and 1cm,
  mynode/.style={draw,circle,text width=0.5cm,align=center}
]

\node[mynode, fill={rgb:black,1;white,2}] (a) {A};
\node[mynode, below right=of a, fill={rgb:black,1;white,2}] (b) {J};
\node[mynode,right=of b, fill={rgb:black,1;white,2}] (c) {S};
\node[mynode, below left=of a, fill={rgb:black,1;white,2}] (e) {S};
\node[mynode, above right=of c, fill={rgb:black,1;white,2}] (f) {A};
\node[mynode, below right=of f, fill={rgb:black,1;white,2}] (d) {J};
\node[mynode, left=of e, fill={rgb:black,1;white,2}] (g) {J};
\node[mynode, left=of g, fill={rgb:black,1;white,2}] (h) {S};
\node[mynode, below=of h] (i) {U};
\node[mynode, below=of g,  fill={rgb:black,1;white,2}] (j) {Y};
\node[mynode, below=of c] (k) {U};
\node[mynode, below=of e] (l) {U};
\node[mynode, below=of b,  fill={rgb:black,1;white,2}] (n) {Y};
\node[mynode, below=of d,  fill={rgb:black,1;white,2}] (m) {Y};

\path (a) edge[-latex] (b);
\path (f) edge[-latex] (c);
\path (f) edge[-latex] (d);
\path (c) edge[latex-] (d);
\path (c) edge[-latex] (m);
\path (c) edge[latex-] (k);
\path (d) edge[-latex] (m);
\path (k) edge[-latex] (m);
\path (b) edge[-latex] (e);
\path (e) edge[latex-] (l);
\path (e) edge[-latex] (n);
\path (l) edge[-latex] (n);
\path (g) edge[-latex] (h);
\path (i) edge[-latex] (h);
\path (j) edge[latex-] (h);
\path (i) edge[-latex] (j);

\end{tikzpicture}
\caption{Example.}
\label{fig:newgraph}
\end{figure}

在此处输入图片描述

答案1

您可以使用tikz-cd来绘制此类图表,以避免重复。箭头的样式由 控制every arrow/.style,您可以使用小表格来表示节点内容。

\documentclass{article}
\usepackage{amsmath}
\usepackage{geometry}
\usepackage{tikz}
\usetikzlibrary{cd,shapes.geometric}
\tikzcdset{m/.style={column sep=0pt,
    every arrow/.style={draw,thick,-latex},
    /tikz/w/.style={fill=white},
    cells={nodes={ellipse,inner sep=0.5pt,fill=gray!30,draw,minimum height=3em}}}}  
\newcommand\mm[3][]{\begin{tabular}{@{}c@{}}
   \ensuremath{#2}\\[-1.2ex]{\text{\tiny[#3]}}
   \end{tabular}}
\begin{document}
\[\begin{tikzcd}[m]
  & |[opacity=0]|\mm{A}{raza} & \\
   \mm{S}{blabla} \arrow[drr] & &\arrow[ll] \mm{J}{abcdef}\\
  |[w]| \mm{S}{blabla} \arrow[u] \arrow[rr]& & \mm{J}{abcdef}\\
\end{tikzcd}\qquad
\begin{tikzcd}[m]
  & \mm{A}{raza}\arrow[dr] & \\
   \mm{S}{blabla} \arrow[drr] & &\arrow[ll] \mm{J}{abcdef}\\
  |[w]| \mm{S}{blabla} \arrow[u] \arrow[rr]& & \mm{J}{abcdef}\\
\end{tikzcd}\qquad
\begin{tikzcd}[m]
  & \mm{A}{raza}\arrow[dl]\arrow[dr] & \\
   \mm{S}{blabla} \arrow[drr] & &\arrow[ll] \mm{J}{abcdef}\arrow[d]\\
  |[w]| \mm{S}{blabla} \arrow[u] \arrow[rr]& & \mm{J}{abcdef}\\
\end{tikzcd}
\]
\end{document}

在此处输入图片描述

答案2

TikZ 有几个用于设置线条粗细的键:thickvery thickultra thick(与 类似thin)。您还可以使用例如 明确设置宽度line width=1mm

至于附加文本,您可以text width=0.5cmmynode样式中删除,然后执行例如A\\[-4pt]\tiny(foobar)。在下面的代码中,我为此定义了一个宏,但您不必这样做。

注意我还添加了on grid样式mynode,并增加了node distanceon grid第二个节点的放置方式是节点中心相距node distance。没有on grid定义node distance节点边界之间的距离。这样做的原因是为了确保不同大小的节点正确对齐。

最后我使用了一个循环来绘制所有箭头。这也不是必需的,但它可以缩短代码。

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\newcommand\CwL[2]{% short for character with label
\begin{tabular}{@{}c@{}}
#1 \\[-4pt]
\tiny (#2)
\end{tabular}}
\begin{document}

\begin{tikzpicture}[
  node distance=2cm and 2cm,
  mynode/.style={draw,circle,align=center, inner sep=0pt, on grid}
]

\node[mynode, fill={rgb:black,1;white,2}] (a) {\CwL{A}{foobar}};
\node[mynode, below right=of a, fill={rgb:black,1;white,2}] (b) {\CwL{J}{foobar}};
\node[mynode, right=of b, fill={rgb:black,1;white,2}] (c) {\CwL{S}{foobar}};
\node[mynode, below left=of a, fill={rgb:black,1;white,2}] (e) {\CwL{S}{foobar}};
\node[mynode, above right=of c, fill={rgb:black,1;white,2}] (f) {\CwL{A}{foobar}};
\node[mynode, below right=of f, fill={rgb:black,1;white,2}] (d) {\CwL{J}{foobar}};
\node[mynode, left=of e, fill={rgb:black,1;white,2}] (g) {\CwL{J}{foobar}};
\node[mynode, left=of g, fill={rgb:black,1;white,2}] (h) {\CwL{S}{foobar}};
\node[mynode, below=of h] (i) {\CwL{U}{foobar}};
\node[mynode, below=of g,  fill={rgb:black,1;white,2}] (j) {\CwL{Y}{foobar}};
\node[mynode, below=of c] (k) {\CwL{U}{foobar}};
\node[mynode, below=of e] (l) {\CwL{U}{foobar}};
\node[mynode, below=of b,  fill={rgb:black,1;white,2}] (n) {\CwL{Y}{foobarbaz}};
\node[mynode, below=of d,  fill={rgb:black,1;white,2}] (m) {\CwL{Y}{foobar}};


\foreach \x/\y in {a/b,f/c,f/d,d/c,c/m,k/c,d/m,k/m,b/e,l/e,e/n,l/n,g/h,i/h,h/j,i/j}
   \draw [ultra thick, -latex] (\x) -- (\y);

\end{tikzpicture}

\end{document}

相关内容