Beautiful chemfig

I want to achieve something similar to the formula in this image

I'm new to chemfig package and I used mol2chemfig webtool to obtain the basic molecule structure and then I modified it manually in order to insert circled and squared entries and the arrow.

This is the code so far:

\documentclass[tikz]{standalone}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=0.5pt] (char) {#1};}}
\newcommand*\squared[1]{\tikz[baseline=(char.base)]{
\node[shape=rectangle,draw,inner sep=2pt] (char) {#1};}}

\usepackage{chemformula}
\usepackage{mol2chemfig}
% bond styling
\setcrambond{2.5pt}{0.4pt}{1.0pt}
\setbondoffset{1pt}
\setdoublesep{2pt}
\setatomsep{16pt}
% print atoms with smaller font and in sans-serif
\renewcommand{\printatom}[1]%
{\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{#1}}}}%
\begin{document}
\schemestart
\chemname{
\chemfig{
O% 3
=[:330]C% 2
(
-[:30]\mcfright{O}{^{\mcfminus}}% 4
)
-[:270,,,2]HC% 1
(
-[:270,,2,1]CH_3% 5
)
-[,,2]O% 6
-P% 7
(
=[:90]O% 8
)
(
-[:270,,,1]OH% 10
)
-[,,,1]OH% 9
}}{fosfoenolpiruvato}
\+
\chemname{
\chemfig{
\circled{P}% 3
-[:270]\circled{P}% 2
-[:270]O% 1
-[:270]\squared{Rib}% 4
\arrow{->[\ch{Mg^2+},\ch{K+}][piruvato chinasi]}
\chemname{
\chemfig{
O% 3
=[:330]C% 2
(
-[:30]\mcfright{O}{^{\mcfminus}}% 4
)
-[:270,,,2]HC% 1
(
-[:270,,2,1]CH_3% 5
)
=[,,2]O% 6
}}{piruvato}
\+
\chemname{
\chemfig{
-[:180]\squared{Rib}% 3
-[:90]O% 1
-[:90]\circled{P}%% 2
-[:90]\circled{P}%% 5
-[:90]P% 6
(
-[:90]\mcfright{O}{^{\mcfminus}}% 7
)
(
-[:180,,,2]^{\mcfminus}O% 8
)
=O% 9
}}{ATP}
\schemestop
\end{document}


And this is the result:

The things that I hope to achieve are:

• Have the entire equation centered vertically
• In the 2nd and 4th structures the circles and rectangles are not connected in the right way, i.e. the straight lines don't connect well with the circles and the rectangles
• The arrow should be longer, or at least the string piruvato chinasi should be placed in two lines (but neither \\ nor \par works)
• Put in evidence with a box filled with a chosen color a part of the 1st and 4th molecule.

The only requirements are:

• It should be done with chemfig
• it should be compatible with PDFLaTeX

Note that in order to compile the code you must download the package mol2chemfig.sty

You can achieve the vertical alignment by naming certain nodes with the @{...} syntax and using them as base. Naming nodes also allows you to shade parts of the diagram. I also made the arrow longer. To make your snipped compile, I had to download mol2chemfig.sty. Note that I removed the commands \squared and \circled in favor of a solution that does not nest tikzpictures. Nesting tikzpictures is risky, there is always a risk that at given point something breaks in such a way that it cannot be repaired easily. That's why I'd like to argue that it is better to avoid it.

\documentclass[tikz]{standalone}
\usetikzlibrary{fit}
\usepackage{chemformula}
\usepackage{mol2chemfig}
% bond styling
\setcrambond{2.5pt}{0.4pt}{1.0pt}
\setbondoffset{1pt}
\setdoublesep{2pt}
\setatomsep{16pt}
% print atoms with smaller font and in sans-serif
\renewcommand{\printatom}[1]%
{\fontsize{8pt}{10pt}\selectfont{\ensuremath{\mathsf{#1}}}}%
\begin{document}
\setarrowdefault{0,1.2}
\schemestart[][base]
\chemname{
\chemfig[baseline=(HC1.base)]{ % <- added base
@{O1}O% 3
=[:330]C% 2
(
-[:30]@{O2}\mcfright{O}{^{\mcfminus}}% 4
)
-[:270,,,2]@{HC1}HC% 1 % <- added @{HC1}
(
-[:270,,2,1]CH_3% 5
)
-[,,2]O% 6
-P% 7
(
=[:90]@{O1}O% 8
)
(
-[:270,,,1]@{OH1}OH% 10
)
-[,,,1]O@{OH2}H% 9
}}{fosfoenolpiruvato}
\+
\chemname{
\chemfig[baseline=(P1.base)]{ %<-
@{P4}{P\strut}% 3
-[:270]@{P1}{P\strut}% 2 %<-
-[:270]@{O6}{O}% 1
-[:270]@{R1}{Rib}% 4
\arrow(.mid east--.mid west){->[\ch{Mg^2+},\ch{K+}][\begin{tabular}{c}
piruvato\\ chinasi
\end{tabular}]}
\chemname{
\chemfig[baseline=(HC2.base)]{
O% 3
=[:330]C% 2
(
-[:30]\mcfright{O}{^{\mcfminus}}% 4
)
-[:270,,,2]@{HC2}HC% 1
(
-[:270,,2,1]CH_3% 5
)
=[,,2]O% 6
}}{piruvato}
\+
\chemname{
\chemfig[baseline=(P2.base)]{
-[:180,2]@{R2}{Rib}% 3
-[:90]O% 1
-[:90]@{P2}{P\strut}%% 2
-[:90]@{P5}{P\strut}%% 5
-[:90]@{P3}P% 6
(
-[:90]\mcfright{O}@{O3}{^{\mcfminus}}% 7
)
(
-[:180,,,2]@{O4}^{\mcfminus}O% 8
)
=@{O5}O% 9
}}{ATP}
\chemmove{
\node[inner sep=2pt,fill=red,fill opacity=0.2,fit=(OH1) (OH2) (O1)]{};
\node[inner sep=2pt,fill=red,fill opacity=0.2,fit=(P3) (O3) (O4) (O5)]{};
\node[inner sep=1pt,draw,fit=(A1)]{};
\node[inner sep=1pt,draw,fit=(R1)] (R1box){};
\node[inner sep=1pt,draw,fit=(A2)]{};
\node[inner sep=1pt,draw,fit=(R2)]{};
\foreach \X in {1,2,4,5}
{\node[draw,circle,fit=(P\X)]{};}
\draw[thin,-] ([yshift=-1mm]O6.center) -- (R1box);
\draw[thin,-] ([yshift=1.5mm]O6.center) -- (P1);
}
\schemestop
\end{document}

• Sorry. I forgot to notify that one should download mol2chemfig.sty in order to compile. – gvgramazio Apr 22 '18 at 17:11
• There is something wrong in nesting tikzpicture? – gvgramazio Apr 22 '18 at 17:12
• You solved all my troubles exept one: makes the connection with circled and squared atoms better. Thanks. – gvgramazio Apr 22 '18 at 17:14
• @giusva Sorry, I was offline. I will try to fix it, but this might mean that I will change the way the boxes and circles are drawn. As I mentioned, you are nesting tikzpictures here, and this can have uncontrollable side effects. So I will try to find a safer solution. Yet this may take a short while. – user121799 Apr 22 '18 at 17:50
• No problem. You already cover almost all my needs. So you suggest to never nest tikzpictures? I never heard of this before. I used this way of drawing boxes and circles because I already using them in other things. A safer solution will sure be appreciated. – gvgramazio Apr 22 '18 at 18:02