我有以下图形代码,我对结果非常满意。由于我是使用 tikz 制作图形的新手,所以我想听听大家对如何改进此代码的反馈。特别是 for 循环的使用和节点的命名。
重点是,我将绘制第二个图形,与这个图形非常相似,但球之间的长度不同(左侧较短,右侧较长)。这就是为什么位置在 x 方向上为负数,因为第二个图形将位于正数侧。
提前致谢!
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[oxygen/.style = {inner sep=2mm, draw=gray!80,fill=gray!30, radius=0.6}, copper/.style = {draw=orange!80,fill=orange!30, radius=0.2}]
\draw[step=1cm,gray,very thin] (-6,-2) grid (0,2);
\foreach \x in {-1,-3,-5}
{\node at (\x,0) [circle, draw, oxygen] (O\x0) {};}
\foreach \x in {-2,-4}
{
\node at (\x,0) [circle, draw, copper] (Cu\x) {};
\foreach \y in {1,-1}
{\node at (\x,\y) [circle, draw, oxygen] (O\x\y) {};}
}
\draw [->, >=stealth, rotate=-45] ($(Cu-2) + (0,-0.35) $) -- ($(Cu-2) + (0,0.35) $);
\draw [->, >=stealth, rotate=135] ($(Cu-4) + (0,-0.35) $) -- ($(Cu-4) + (0,0.35) $);
\foreach \i in {-10, -30, -21, -2-1}
{\draw[thick] (Cu-2) -- (O\i);}
\foreach \i in {-30, -50, -41, -4-1}
{\draw[thick] (Cu-4) -- (O\i);}
\end{tikzpicture}
\end{document}
编辑
这是我第一次(不太优雅地)尝试创建我需要的第二个图形。
\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[oxygen/.style = {inner sep=2mm, draw=gray!80,fill=gray!30, radius=0.6}, copper/.style = {draw=orange!80,fill=orange!30, radius=0.2}]
\foreach \x [count=\i] in {1.25,2.75,5.25}
{\node at (\x,0) [circle, draw, oxygen] (O\i0) {};}
\foreach \x in {2,4}
{
\node[circle, draw, copper] (Cu\x) at (\x,0) {};
}
\node at ($(Cu2) + (0,0.75)$) [circle, draw, oxygen] (O21) {};
\node at ($(Cu2) + (0,-0.75)$) [circle, draw, oxygen] (O2-1) {};
\node at ($(Cu4) + (0,1.25)$) [circle, draw, oxygen] (O41) {};
\node at ($(Cu4) + (0,-1.25)$) [circle, draw, oxygen] (O4-1) {};
\draw [->, >=stealth, rotate=-45] ($(Cu4) + (0,-0.35) $) -- ($(Cu4) + (0,0.35) $);
\draw [->, >=stealth, rotate=135] ($(Cu2) + (0,-0.35) $) -- ($(Cu2) + (0,0.35) $);
\foreach \i in {10,20, 21, 2-1}
{\draw[thick] (Cu2) -- (O\i);}
\foreach \i in {20, 30, 41, 4-1}
{\draw[thick] (Cu4) -- (O\i);}
\end{tikzpicture}
\end{document}
答案1
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\tikzset
{oxygen/.style =
{circle, draw=gray!80, fill=gray!30, radius=0.6, inner sep=2mm},
copper/.style =
{circle, draw=orange!80, fill=orange!30, radius=0.2},
bond/.style = {thick}
}
% \oxygen{name}{position}
\newcommand\oxygen[2]%
{\node[oxygen] (#1) at (#2) {};}
% \copper[direction of arrow]{name}{position}
% direction: <- (down), -> (up, default)
\newcommand\copper[3][]%
{\node[copper] (#2) at (#3) {};
\draw[->,>=stealth,#1] ($(#3)+(225:0.35)$) -- ($(#3)+(45:0.35)$);
}
% \copox[direction of arrow]{name}{position}{distance Cu-O}
% name of nodes: (name), (name-0), (name-90), (name-180), (name-270)
\newcommand\copox[4][]%
{\copper[#1]{#2}{#3}
\foreach \a in {0,90,180,270}
{\oxygen{#2-\a}{$(#2)+(\a:#4)$}
\draw[bond] (#2) -- (#2-\a);
}
}
\begin{document}
\begin{tikzpicture}
\draw[step=1cm,gray,very thin] (-6,-2) grid (3,2);
\copox[<-]{CO1}{-4,0}{1}
\copox{CO2}{-2,0}{1}
\copox[<-]{CO3}{0.5,0}{1.5}
\end{tikzpicture}
\end{document}