# Drawing a polymer with multiple polymer brackets in the same bond in chemfig

I need to draw the following polymers and don't quite know how I manage two polymer brackets in one bond. The result should look like this:

My current code produces the first set of brackets, but I don't know how to include the rest:

\documentclass{article}

\usepackage{chemfig}
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}

\def\makebraces(#1,#2)#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{
\node[at=(#4),yshift=(\delimvshift)]
{$\left\delimleft \vrule height\delimhalfdim depth\delimhalfdim width0pt \right.$};
\node[at=(#5),yshift=(\delimvshift)]
{$\left. \vrule height\delimhalfdim depth\delimhalfdim width0pt \right\delimright_{\rlap{#3}}$};
}%
}

\newcommand{\alignC}{C}
\begin{document}
\setpolymerdelim()
\chemfig{H-[:-30]O-[@{op,.5}:30]-[:-30](-[:-90])-[:30]O-[:-30](=[:-90]O)-[:30]O-[@{cl,.25}:-30]-[:30]-[:-30]O-[:30](=[:90]O)-[:-30]=-[:30](=[:90]O)-[:-30]O-[:30]H}
\makebraces(12pt,12pt){$\scriptstyle\!\!m$}{op}{cl}

\end{document}


Method 1:

One way of doing this is by using invisible bonds (as was done for @{OP}), e.g.,

O-[@{OP,-20}:30,0.2,,,draw=none]% Produces the larger opening square bracket in the figure below


where the invisible bond is some finite non-zero (!) length (here, 0.2*bond length), then the node @{OP,-20} is used, where the -20 is a factor that signifies that the node is placed -20*bond length distance away. Essentially displacing the node to the location that you want along the actual, visible bond. Try changing the -20 to other numbers to see what I mean. This is, of course, abusing the notation a little.

The method above introduces blank space into the compound, which is not always ideal, for example when trying to insert this node into a location with no atoms printed, like the closing square bracket:

(I got away with this with @{OP} above because of the printed O atom.

Method 2:

You can also insert nodes manually by wrapping the chemfig in a tikzpicture environment, then just use the \node command as per normal.

\node[right=7pt of cl,yshift=-5pt] (CL) {};


An advantage of this is you can specify quite precisely where you want the braces to go, using yshift and friends.

Below is the full code which does not reproduce exactly what you have, but contains both methods. You should be able to apply this technique for the other braces.

Full Code:

(Note the commented % <---------------.)

\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{positioning} % <----------------- For positioning of nodes
\newcommand\setpolymerdelim[2]{\def\delimleft{#1}\def\delimright{#2}}

\def\makebraces(#1,#2)#3#4#5{%
\edef\delimhalfdim{\the\dimexpr(#1+#2)/2}%
\edef\delimvshift{\the\dimexpr(#1-#2)/2}%
\chemmove{
\node[at=(#4),yshift=(\delimvshift)]
{$\left\delimleft \vrule height\delimhalfdim depth\delimhalfdim width0pt \right.$};
\node[at=(#5),yshift=(\delimvshift)]
{$\left. \vrule height\delimhalfdim depth\delimhalfdim width0pt \right\delimright_{\rlap{#3}}$};
}%
}

\newcommand{\alignC}{C}
\begin{document}
\begin{tikzpicture}
\setpolymerdelim()
\chemfig{H-[:-30]O-[@{OP,-20}:30,0.2,,,draw=none]% <--------------- Method 1
-[@{op,.5}:30]-[:-30](-[:-90])-[:30]O-[:-30](=[:-90]O)-[:30]O-[@{cl,.25}:-30]%
-[:30]-[:-30]O-[:30](=[:90]O)-[:-30]=-[:30](=[:90]O)-[:-30]O-[:30]H}
\node[right=7pt of cl,yshift=-5pt] (CL) {};% <------------------- Method 2
\makebraces(12pt,12pt){$\scriptstyle\!\!m$}{op}{cl}
\setpolymerdelim[]
\makebraces(30pt,30pt){$\scriptstyle\!\!\mathit{stat}$}{OP}{CL}
\end{tikzpicture}

\end{document}