# Crossing bonds in chemfig

Sometimes, molecular structures have crossing bonds, and we want to indicate which bond is in the foreground. In the chemfig example below, I have done this by first drawing a thick, white bond to an empty fake substituent; this bond then serves as a background for a real bond.

\documentclass{minimal}
\usepackage{chemfig}
\setatomsep{6em}
\begin{document}
\chemfig{
A
-[:-45]B
-[:180,0.75]C
-[:45]D
}
%
%
\chemfig{
A
-[:-45]B
-[:180,0.75]C
(-[:45,,,,white,line width=3pt]) % fake substituent
-[:45]D
}
\end{document}


This works, but there must be a better way. chemfig is built on tikz, so may be the tikz.decorations library can be used to create a bond style that carries its white background with it at all times. Does anyone know how to do this?

-
The way I would do this in TikZ is with a double line where the outer line was the background colour and the inner line the original colour. But when you dig deep in the code, what really happens is that it draws two lines just as you are doing. –  Loop Space Jan 5 '12 at 18:06
(Incidentally, the minimal class isn't a great one to use for examples. It really is too minimal.) –  Loop Space Jan 5 '12 at 18:39

I would write this:

\chemfig{A
-[:-45]B
-[:180,0.75]C
-[:45,,,,preaction={draw=white, -,line width=6pt}]D
}

-
Perfect - thanks. –  Michael Palmer Jan 5 '12 at 18:17

I've never written anything with chemfig before so had to just guess at the syntax. This is basically my comment above made into an example and expanded a little.

There are two ways to achieve the effect of one line going over another. One way is for the under line to "know" that it is the under line and break itself at the crossing point (this is what I do in my braids package). The other way is for the over line to, as you say, carry around its white background. This has the advantage that no-one actually has to know the exact crossing point (and is what I do in my knots package .. which isn't on CTAN yet, but there's examples hereabouts). However, this has the disadvantage that you have to know the background colour and it has to be uniform. Plus there can be "edge" effects if the lines get close to each other but don't actually cross.

Here's examples of both of those approaches. To get the under line to break, we use the decorations.markings library to put a node in the line. As you may be able to guess from the code, I had to do a bit of trial-and-error to get the right break point. The over line method doesn't need any such hand-holding, nor any special libraries. I've also packaged the methods up as styles to make them easy to apply.

Here's the code:

\documentclass{article}
%\url{http://tex.stackexchange.com/q/40153/86}
\usepackage{chemfig}
\usetikzlibrary{decorations.markings}
\setatomsep{6em}
\tikzset{
over line/.style={
white,
double=black,
double distance=\the\pgflinewidth,
line width=1.5pt,
},
under line/.style={
decoration={
markings,
mark connection node=mid node,
mark=at position #1 with {}
{\node[transform shape,minimum size=3pt] (mid node) {};}
},
decorate
}
}
\begin{document}
\chemfig{
A
-[:-45]B
-[:180,0.75]C
-[:45]D
}
%
%
\chemfig{
A
-[:-45]B
-[:180,0.75]C
(-[:45,,,,white,line width=3pt]) % fake substituent
-[:45]D
}
%
%
\chemfig{
A
-[:-45]B
-[:180,0.75]C
-[:45,,,,over line]D
}
%
%
\chemfig{
A
-[:-45,,,,under line=.46]B
-[:180,0.75]C
-[:45]D
}

\end{document}


Here's the result. Your original ones are the first two, then the "over line" approach, and lastly the "under line".

-
Excellent solution. I will post a variation to this further down in a separate answer, as apparently the comments don't allow for the inclusion of complete code examples. –  Michael Palmer Jan 5 '12 at 19:03
Oh I just discovered that I can't answer my own question because of low reputation. So what I did was simply add the chemfig command \setbondstyle{over line} to Andrew's example, and then that style will be used to draw every bond, unless overridden with specific tikz code. –  Michael Palmer Jan 5 '12 at 19:11
However, \setbondstyle{overline} messes up wedged bonds, so it cannot be used generally. –  Michael Palmer Jan 5 '12 at 19:31
@MichaelPalmer As I said, I've never used chemfig before so had to make some guesses. Neither of the two answers that you have been given works well with all bond types (try >: with unbonpetit's solution, or try it where the crossing is near the fat end of the bond). Doing this properly will require a different solution per bond type because the bonds are drawn in different ways: - is drawn, > is filled, and I don't know how >: is rendered. That would be possible, but would involve more work understanding how chemfig works. –  Loop Space Jan 6 '12 at 9:17