这很好用。
\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations, decorations.markings, positioning}
\begin{document}
\chemfig{
H-[,,,,draw=none,
postaction={decorate},
decoration={
markings,
mark = at position 0.22 with {\fill[red] circle[radius=1pt];},
mark = at position 0.5 with {\fill[red] circle[radius=1pt];},
mark = at position 1-0.22 with {\fill[red] circle[radius=1pt];}
}
]O
}
\end{document}
这不管用。
\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations, decorations.markings, positioning}
\usepackage{xparse}
\NewDocumentCommand \cfhbond { O{red} O{1} O{0.22} } {%
draw=none,
postaction={decorate},
decoration={
markings,
mark = at position #3 with {\fill[#1] circle[radius=#2pt];},
mark = at position 0.5 with {\fill[#1] circle[radius=#2pt];},
mark = at position 1-#3 with {\fill[#1] circle[radius=#2pt];}
}%
}
\begin{document}
\chemfig{H-[,,,,\cfhbond]O}
\chemfig{H-[,,,,\cfhbond{blue}{1.5}{0.25}]O}
\end{document}
可以创建这样的命令吗?
答案1
这就是样式和 pgf 键的作用:您可以使用带有可选 pgf 键的样式,而不是定义带有许多选项的命令,而这些选项需要您重新记住命令的顺序。您可以设置一些初始值(您可能认为是默认值),然后随时更改键。这开箱即用,不会出现任何扩展问题。
\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations, decorations.markings, positioning}
\tikzset{cfhbond/.style={%
/utils/exec=\tikzset{cfh/.cd,#1},
draw=none,
postaction={decorate},
decoration={
markings,
mark = at position \pgfkeysvalueof{/tikz/cfh/position} with {\fill[\pgfkeysvalueof{/tikz/cfh/color}] circle[radius=\pgfkeysvalueof{/tikz/cfh/radius}*1pt];},
mark = at position 0.5 with {\fill[\pgfkeysvalueof{/tikz/cfh/color}] circle[radius=\pgfkeysvalueof{/tikz/cfh/radius}*1pt];},
mark = at position 1-\pgfkeysvalueof{/tikz/cfh/position} with {\fill[\pgfkeysvalueof{/tikz/cfh/color}] circle[radius=\pgfkeysvalueof{/tikz/cfh/radius}*1pt];}
}%
},cfh/.cd,color/.initial=red,radius/.initial=1,position/.initial=0.22} % { O{red} O{1} O{0.22} } {%
\begin{document}
\chemfig{H-[,,,,cfhbond]O}
\chemfig{H-[,,,,cfhbond={color=blue,radius=1.5,position=0.25}]O}
\end{document}
这种方法的另一个优点是可以轻松升级。您想让另一个参数更灵活吗?没问题,将其设为 pgf 键。旧用法仍然有效。