chemfig
Operation of chemfig
12 Connecting distant atoms¶
We have seen how to connect atoms which are adjacent in the code. It is often necessary to connect atoms which are not next to each other in the code. Let’s call these particular bonds “distant bonds”.
Let’s take this molecule:
\chemfig{A-B(-[1]W-X)(-[7]Y-Z)-C}
and suppose that we want to connect the atoms X and C. In this case, chemfig allows a “hook” to be placed immediately after the atom of interest. The character used for a hook is “?” because of its similarity to a hook. So, if one writes X? then the atom X will have a hook. Later in the code, all atoms followed by a ? will be connected to X:
\chemfig{A-B(-[1]W-X?)(-[7]Y-Z)-C?}
We could connect other atoms to X by following them with ?. Here it’s the atoms C and Z:
\chemfig{A-B(-[1]W-X?)(-[7]Y-Z?)-C?}
Now imagine if we were to leave the distant bonds X-C and X-Zwhile adding another: A-W. We must therefore ask for two different hooks, one on A and the other on X. Fortunately the character ? has an optional argument:
?[⟨name⟩,⟨bond⟩,⟨tikz⟩]
where each field takes its default value if it is empty:
The ⟨name⟩ is the name of the hook: all alphanumeric characters (a…z, A…Z, 0…9) are allowed1. The name is a by default. In the first occurrence of the hook with this name, only this field is used.
⟨bond⟩ specifies how the atom with the current occurrence of the named hook is to be bonded to the atom with the first occurrence of the hook. There are two ways this can be done. First, this field can be an integer representing the desired bond type: 1=single bond, 2=double bond, etc. (See the table on section 4 for the bond codes.)
Second, the field can be one of the bond character codes, provided that this character is between braces.
⟨tikz⟩ will be passed directly to TikZ as we have seen with regular bonds.
Here is our molecule with the required distant bonds, then with the bond A-W and X-C customized:
\chemfig{A?[a]-B(-[1]W?[a]-X?[b])(-[7]Y-Z?[b])-C?[b]}\par\medskip
\chemfig{A?[a]-B(-[1]W?[a,2,red]-X?[b])(-[7]Y-
Z?[b,1,{line width=2pt}])-C?[b,{>},blue]}
Several different hooks can be written after an atom. Suppose that in this unfinished pentagon, we wish to connect A-E, A-C and E-C:
\chemfig{A-[:-72]B-C-[:72]D-[:144]E}
Then we must do this:
\chemfig{A?[a]-[:-72]B-C?[a]?[b]-[:72]D-[:144]E?[a]?[b]}
1 This is not exactly right. Actually all the characters that can be put between \csname...\endcsname are allowed.