3

With the following MWE (well not quite M!) I don't understand why, when I change from an ellipse box to a rectangle box (by uncommenting the corresponding lines), the molecule are shifted out of the boxes.

\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}

With the ellipse boxes, it's OK, enter image description here

but with the rectangle boxes, it's out ! enter image description here

Any hint ?

2

Use the node itself as a container. The problem with the original approach is that the current position after an ellipse is the center of the ellipse, whereas after a rectangle it is the top right corner.

\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}};

enter image description here

\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}

Edit: Following the suggestion of @Guilherme, here is a version using elliptic nodes.

\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}};

enter image description here

\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}
  • OK, it is a good solution which gives the expected result. Thanks! – DRi Nov 25 '16 at 14:04
  • @DRi, There are Ellipse shape nodes as well (library shapes.geometric). You can use them instead of drawing the Ellipses, although they give weird inner spacing sometimes. – Guilherme Zanotelli Nov 25 '16 at 14:08
  • @DRi The advantage of using the node itself is that when you draw a line, tikz knows by itself where the border of the node is and where the line/arrow has to stop. – gernot Nov 25 '16 at 14:12
  • @Guilherme Z. Santos, I may try this option sometimes. Thanks! – DRi Nov 25 '16 at 14:12
  • I've added a version using elliptic nodes, to illustrate the proposal of @GuilhermeZ.Santos . – gernot Nov 25 '16 at 14:29
1

Try this (notice the addition of pos=0.5 in the nodes):

\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}};

The manual (page 235, version 3.01) has this to say about pos:

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

When this option is given, the node is not anchored on the last coordinate. Rather, it is anchored on some point on the line from the previous coordinate to the current point. The ⟨fraction⟩ dictates how “far” on the line the point should be. A ⟨fraction⟩ of 0 is the previous coordinate, 1 is the current one, everything else is in between. In particular, 0.5 is the middle.

Better placement of chemfig molecule.

  • +1. Aside from that you can use the node between the path operation and the coordinate, in that case the node automatically receives the pos=.5 option (e.g. \draw (0,0) rectangle node{A} (1,1);). Also, a shorthand for pos=.5 is midway, they mean the same. – Guilherme Zanotelli Nov 25 '16 at 14:06
  • I works fine. I didn't know this option pos= . This solution is perhaps 'dirtier' than the second one, but it may be very useful for some manual fine adjustments. Thanks! – DRi Nov 25 '16 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.