在 TikZ 节点中定位 chemfig 结构

在 TikZ 节点中定位 chemfig 结构

使用以下 MWE(不完全是 M!)我不明白为什么当我从椭圆框更改为矩形框(通过取消注释相应的行)时,分子会移出框。

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, decorations.pathmorphing}
\usepackage{chemfig}
\usepackage[version=4]{mhchem}

\newcommand{\molA}[1]{\chemfig[line width=0.2pt][scale=#1]{[:45]HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]OH}}
\newcommand{\molB}[1]{\chemfig[line width=0.2pt][scale=#1]{H-[:-30](=[:-90]O)-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30](=[:90]O)-[:-30]H}}

\begin{document}
\begin{tikzpicture}
% starting dots
\fill[fill=blue] (0.0,1.9) circle (1pt);
\fill[fill=orange] (0.0,1.7) circle (1pt);
\fill[fill=red] (0.0,1.5) circle (1pt);
\fill[fill=green] (0.0,1.3) circle (1pt);
\fill[fill=purple] (0.0,1.1) circle (1pt);

\filldraw[draw=blue,fill=blue!10] (3.0,2.7) ellipse [x radius=55pt,y radius=6pt] node[color=black] (A) {\molA{0.2}};
\filldraw[draw=orange,fill=orange!10] (3.5,2.2) ellipse [x radius=55pt,y radius=6pt] node[color=black] (B) {\molA{0.2}};
\filldraw[draw=red,fill=red!10] (3.0,1.7) ellipse [x radius=55pt,y radius=6pt] node[color=black] (C) {\molA{0.2}};
% ellipse boxes
\filldraw[draw=green,fill=green!10] (3.5,0.8) ellipse [x radius=55pt,y radius=16pt] node[color=black] (D) {\molB{0.2}};
\filldraw[draw=purple,fill=purple!10] (3.0,-0.4) ellipse [x radius=55pt,y radius=16pt] node[color=black] (E) {\molB{0.2}};
%rectangle boxes
%\filldraw[draw=green,fill=green!10,rounded corners=5] (1.5,0.5) rectangle (5.5,1.1) node[color=black] (D) {\molB{0.2}};
%\filldraw[draw=purple,fill=purple!10,rounded corners=5] (1.0,-0.7) rectangle (5.0,-0.1) node[color=black] (E) {\molB{0.2}};

% arrows
\begin{scope}[thick,>=latex,->]
 \draw[color=blue] (0.0,1.9) -- (A);
 \draw[color=orange] (0.0,1.7) -- (B);
 \draw[color=red] (0.0,1.5) -- (C);
 \draw[color=green] (0.0,1.3) -- (D);
 \draw[color=purple] (0.0,1.1) -- (E);
\end{scope}
\end{tikzpicture}
\end{document}

用椭圆框就可以了, 在此处输入图片描述

但是有了矩形框,它就不行了! 在此处输入图片描述

有什么提示吗?

答案1

使用节点本身作为容器。原始方法的问题在于,椭圆后的当前位置是椭圆的中心,而矩形后的当前位置是右上角。

\node[draw=green, fill=green!10, rounded corners=5] (D) at (3.5,0.8)  {\molB{0.2}};
\node[draw=purple,fill=purple!10,rounded corners=5] (E) at (3.0,-0.4) {\molB{0.2}};

在此处输入图片描述

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, decorations.pathmorphing}
\usepackage{chemfig}
\usepackage[version=4]{mhchem}

\newcommand{\molA}[1]{\chemfig[line width=0.2pt][scale=#1]{[:45]HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]OH}}
\newcommand{\molB}[1]{\chemfig[line width=0.2pt][scale=#1]{H-[:-30](=[:-90]O)-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30](=[:90]O)-[:-30]H}}
\begin{document}
\begin{tikzpicture}
% starting dots
\fill[fill=blue] (0.0,1.9) circle (1pt);
\fill[fill=orange] (0.0,1.7) circle (1pt);
\fill[fill=red] (0.0,1.5) circle (1pt);
\fill[fill=green] (0.0,1.3) circle (1pt);
\fill[fill=purple] (0.0,1.1) circle (1pt);

\filldraw[draw=blue,fill=blue!10] (3.0,2.7) ellipse [x radius=55pt,y radius=6pt] node[color=black] (A) {\molA{0.2}};
\filldraw[draw=orange,fill=orange!10] (3.5,2.2) ellipse [x radius=55pt,y radius=6pt] node[color=black] (B) {\molA{0.2}};
\filldraw[draw=red,fill=red!10] (3.0,1.7) ellipse [x radius=55pt,y radius=6pt] node[color=black] (C) {\molA{0.2}};
\node[draw=green,fill=green!10,rounded corners=5] (D) at (3.5,0.8) {\molB{0.2}};
\node[draw=purple,fill=purple!10,rounded corners=5] (E) at (3.0,-0.4) {\molB{0.2}};
% arrows
\begin{scope}[thick,>=latex,->]
 \draw[color=blue] (0.0,1.9) -- (A);
 \draw[color=orange] (0.0,1.7) -- (B);
 \draw[color=red] (0.0,1.5) -- (C);
 \draw[color=green] (0.0,1.3) -- (D);
 \draw[color=purple] (0.0,1.1) -- (E);
\end{scope}
\end{tikzpicture}
\end{document}

编辑:根据@Guilherme 的建议,这里有一个使用椭圆节点的版本。

\usetikzlibrary{shapes.geometric}
...
\node[draw=blue,  fill=blue!10,  ellipse,inner sep=1pt] (A) at (3.0, 2.7) {\molA{0.2}};
\node[draw=orange,fill=orange!10,ellipse,inner sep=1pt] (B) at (3.5, 2.2) {\molA{0.2}};
\node[draw=red,   fill=red!10,   ellipse,inner sep=1pt] (C) at (3.0, 1.7) {\molA{0.2}};

在此处输入图片描述

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, decorations.pathmorphing,shapes.geometric}
\usepackage{chemfig}
\usepackage[version=4]{mhchem}

\newcommand{\molA}[1]{\chemfig[line width=0.2pt][scale=#1]{[:45]HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]OH}}
\newcommand{\molB}[1]{\chemfig[line width=0.2pt][scale=#1]{H-[:-30](=[:-90]O)-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30](=[:90]O)-[:-30]H}}

\begin{document}
\begin{tikzpicture}
% starting dots
\fill[fill=blue] (0.0,1.9) circle (1pt);
\fill[fill=orange] (0.0,1.7) circle (1pt);
\fill[fill=red] (0.0,1.5) circle (1pt);
\fill[fill=green] (0.0,1.3) circle (1pt);
\fill[fill=purple] (0.0,1.1) circle (1pt);

\node[draw=blue,  fill=blue!10,  ellipse,inner sep=1pt] (A) at (3.0, 2.7) {\molA{0.2}};
\node[draw=orange,fill=orange!10,ellipse,inner sep=1pt] (B) at (3.5, 2.2) {\molA{0.2}};
\node[draw=red,   fill=red!10,   ellipse,inner sep=1pt] (C) at (3.0, 1.7) {\molA{0.2}};
\node[draw=green, fill=green!10, rounded corners=5]     (D) at (3.5, 0.8) {\molB{0.2}};
\node[draw=purple,fill=purple!10,rounded corners=5]     (E) at (3.0,-0.4) {\molB{0.2}};
% arrows
\begin{scope}[thick,-latex]
 \draw[color=blue] (0.0,1.9) -- (A);
 \draw[color=orange] (0.0,1.7) -- (B);
 \draw[color=red] (0.0,1.5) -- (C);
 \draw[color=green] (0.0,1.3) -- (D);
 \draw[color=purple] (0.0,1.1) -- (E);
\end{scope}
\end{tikzpicture}
\end{document}

答案2

pos=0.5尝试一下(注意在节点中添加):

\filldraw[draw=green,fill=green!10,rounded corners=5] (1.5,0.5) rectangle (5.5,1.1) node[color=black,pos=0.5] (D) {\molB{0.2}};
\filldraw[draw=purple,fill=purple!10,rounded corners=5] (1.0,-0.7) rectangle (5.0,-0.1) node[color=black,pos=0.5] (E) {\molB{0.2}};

该手册(第 235 页,版本 3.01)有如下说明pos

/tikz/pos=⟨fraction⟩ (no default)

当给出此选项时,节点不会锚定在最后一个坐标上。相反,它会锚定在从前一个坐标到当前点的线上的某个点上。⟨fraction⟩ 决定了该点应该在线上有多“远”。⟨fraction⟩ 为 0 表示前一个坐标,1 表示当前坐标,其他值介于两者之间。具体来说,0.5 表示中间值。

更好地放置 chemfig 分子。

相关内容