# Incorrect positioning when using tikz overlay and remember picture

When I use chemfig in conjuction when \tikz[overlay,remember picture] the positioning does go in the correct position. I am using XeLaTeX and the yLaTeX class (see this link: https://github.com/HarveySheppard/yLaTeX). Here are a few examples of the error:

\begin{center}
\setchemfig{cram width=3pt}
\schemestart
\subscheme{
\chemleft[
\chemfig{@{F}\Lewis{0:2:4:6:,F}}
\chemright]}
\+
\chemfig{F-@{B}{\phantom{B}}(-[2,0,,,draw=none]{})(<[:-20]F)<:[:20]F}
\arrow{->}
\chemleft[
\chemfig{\chembelow[0.5pt]{F}{\scriptstyle\hspace{4.5mm}\oplus}-[:-90,0.8]@{cb}\chemabove[0.5pt]{B}{\scriptstyle\hspace{4.5mm}\ominus}(<:[:-40,0.8]F)(<[:-70,0.8]F)-[:-140]F}
\chemright]
\schemestop
\tikz[overlay,remember picture]{
\begin{scope}[overlay,shift={(B)}]
\draw[-] (-0.1,0.2) ..controls +(120:1) and +(60:1) .. (0.1,0.2);
\draw[-,fill=black!10] (-0.1,-0.2) ..controls +(-120:1) and +(-60:1) ..
+(0.2,0);
\end{scope}
\node[fill=white,inner sep=0.1pt] at (B){{\fontfamily{cmr}\selectfont
B
}};
\draw[-latex,shorten <=2pt] (F) ..controls +(90:1.7cm)and+(90:1cm)..
([yshift=9pt]B.north);
}
\tikz[overlay,remember picture]{
\draw[-latex,shorten <=2pt,densely dashed] ([yshift=9pt]cb.north) ..controls +(180:0.7) and +(-90:1).. +(-0.7,1) node[above,align=center] {\scriptsize coordinate \scriptsize covalent\\ \scriptsize bond};
}
\chemmove{\node[xshift=6pt] at (c2.north east) {$-$};}
\chemmove{\node[xshift=6pt] at (c3.north east) {$-$};}
\end{center}


Here is the output: The dashed arrow that points to coordinate covalent bond should be from the vertical bond between the Boron (B) and fluorine (F) in the last compound.

Another example:

\setchemfig{cram width=3pt}
\chemnameinit{}
\schemestart
\chemname{\chemfig{H_{3}C-C(=[:45]O)-[:-45]O-[:0]H}}{\tikzmarknode{A}{Acetic
Acid}}
+
\chemname{\chemfig{H_{3}C-\Lewis{2:,N}(<:[:-15]H)<[:-40]H}}{\tikzmarknode{B}{Methylamine}}
\arrow{<=>}
\chemname{\chemfig{H_{3}C-C(=[:45]O)-[:-45]O\rlap{${}^-$}}}{\tikzmarknode{C}{Acetate
Ion}}
+
\chemname{\chemfig{H_{3}C-N\rlap{${}^+$}(-[:45]H)(<:[:-15]H)<[:-40]H}}{\tikzmarknode{D}{Mathylammonium
Ion}}
\schemestop
\begin{tikzpicture}[overlay,remember picture]
\node[below=3mm of A,fill=dullblue,draw,ellipse] (Acid1) {Acid};
\node[below=3mm of B,draw,ellipse] (Base1) {Base};
\node[below=3mm of C,fill=dullblue,draw,ellipse] (Base2) {Base};
\node[below=3mm of D,draw,ellipse] (Acid2) {Acid};
\draw[blue!50] (Acid1) -- ++ (0,-1.5) coordinate(aux) -| (Base2);
\draw (Base1) -- ++ (0,-2)  -| (Acid2);
\node[anchor=south,blue!50,fill=white,outer sep=1pt] at (aux-|Base1) {Conjugate pair};
\end{tikzpicture}


The following is required in the preamble:

\usepackage{chemfig}
\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning,shapes.geometric}
\definecolor{dullblue}{RGB}{178,201,231}


The code comes from one of my previous questions: How to typset/draw conjugate acids and bases. It produces the following output The text comes from the rest of my document. I am more worried about the fact that the tikz ovals are not in the correct location and do not know how to solve this issue. Any help would be appreciated.

• Have you compiled the documents at least twice in succession? This is necessary with overlays. – AndréC Dec 14 '18 at 6:20
• Yes I have compiled it 3 times – sab hoque Dec 14 '18 at 6:22
• While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. – Peter Grill Dec 14 '18 at 6:30
• The overlay/remember picture thing just doesn't work with other drivers than pdftex or luatex. I got similar unsatisfactory results with the Dvips route. – AlexG Dec 14 '18 at 10:44
• @sabhoque It appears there is a workaround. There are two adjusts you need to make. First, replace \tikzmarknode with \subnode. Second, use the workaround definition within \makeatletter ... \makeatother from the answer in the link. – whatisit Dec 15 '18 at 5:12

As suggested by @whatisit adding the following code to the preamble makes the code render correctly:

\makeatletter
\def\pgfsys@hboxsynced#1{%
{%
\pgfsys@beginscope%
\setbox\pgf@hbox=\hbox{%
\hskip\pgf@pt@x%
\raise\pgf@pt@y\hbox{%
\pgf@pt@x=0pt%
\pgf@pt@y=0pt%
\special{pdf: content q}%
\pgflowlevelsynccm%
\pgfsys@invoke{q -1 0 0 -1 0 0 cm}%
\special{pdf: content -1 0 0 -1 0 0 cm q}% translate to original coordinate system
\pgfsys@invoke{0 J [] 0 d}% reset line cap and dash
\wd#1=0pt%
\ht#1=0pt%
\dp#1=0pt%
\box#1%
\pgfsys@invoke{n Q Q Q}%
}%
\hss%
}%
\wd\pgf@hbox=0pt%
\ht\pgf@hbox=0pt%
\dp\pgf@hbox=0pt%
\pgfsys@hbox\pgf@hbox%
\pgfsys@endscope%
}%
}
\makeatother


The code comes from here