# How to draw parentheses inside chemfig?

I have the following code:

\chemfig[][scale=0.6]{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O-[:30]CH_2CH_2-[:-30]O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}


Which gives me this image:

Now I want to draw openening parentheses between O-CH_2CH_2 and closing parentheses between O-C. And to subindex the closing parentheses with an n. How can I achieve that? Do I have to use tikzpicture environement or the \leftdelimiter(. The problems I'm facing are alignment and length of the connector.

EDIT

I have some additional figure, how could I achieve the brackets in this case?

\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}}$};
}%
}
\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[@{cl,.8}:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(50pt,50pt){$\scriptstyle\!\!n$}{op}{cl}


And the image:

-

An example for this is given in the documentation of chemfig (part III section 12.5 Draw polymer element). There a macro

\makebraces(<dim up>,<dim down>){<subscript>}{<opening node name>}{<closing node name>}


is defined that exploits chemfig's @{<node name>,<pos>} syntax inside formulas to position delimiters on a bond.

The code below is basically a copy from there:

\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}}$};
}%
}

\begin{document}

\setpolymerdelim()
\chemfig{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O
-[@{op,.75}]CH_2CH_2-[@{cl,.25}]
O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}
\makebraces(5pt,5pt){$\!\!n$}{op}{cl}

\end{document}


The first part of \makebraces(<dim up>,<dim down>) specifies the height of the parentheses (divided in an up and down part). The parentheses can be positioned via the <pos> part of @{<node name>,<pos>}. The fontsize can be changed in the <subscript> part of \makebraces. It could also be hardcoded in the macro itself, of course.

\setpolymerdelim()
\chemfig{
-[@{op,.75}:30]CH_2CH_2-[:-30]O-[@{cl,.5}:30]
}
\makebraces(12pt,12pt){$\scriptstyle\!\!n$}{op}{cl}


To be more flexible one could change the definition of \makebraces to get different upper and lower height for each parenthesis.

Edit in response to edited question:

The code below is a quick modification of the \makebraces command that makes the argument (<dim up>,<dim down>) optional and sets both to 5pt as default for both delimiters. It also introduces a second optional (<dim up>,<dim down>) that when used sets different values for the closing delimiter. For convenience this is done with the help of xparse. This allows for better control of both delimiters.

\documentclass{article}

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

\usepackage{xparse}
\makeatletter
\newcommand\@set@open@delim[2]{%
\edef\open@delim@halfdim{\the\dimexpr(#1+#2)/2\relax}%
\edef\open@delim@vshift{\the\dimexpr(#1-#2)/2\relax}%
}
\newcommand\@set@close@delim[2]{%
\edef\close@delim@halfdim{\the\dimexpr(#1+#2)/2\relax}%
\edef\close@delim@vshift{\the\dimexpr(#1-#2)/2\relax}%
}
\newcommand\@make@braces[3]{%
\chemmove{
\node[at=(#2),yshift=(\open@delim@vshift)]
{$\left\delimleft \vrule height\open@delim@halfdim depth\open@delim@halfdim width0pt \right.$};
\node[at=(#3),yshift=(\close@delim@vshift)]
{$\left. \vrule height\close@delim@halfdim depth\close@delim@halfdim width0pt \right\delimright_{\rlap{#1}}$};
}%
}
\NewDocumentCommand\makebraces
{
>{ \SplitArgument{1}{,} } D(){5pt,5pt}
>{ \SplitArgument{1}{,} } d()
mmm
}
{%
\@set@open@delim #1%
\IfNoValueTF {#2}
{\@set@close@delim#1}
{\@set@close@delim#2}%
\@make@braces{#3}{#4}{#5}%
}
\makeatother

\begin{document}

\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[@{cl,.8}:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(10pt,100pt)(46pt,64pt){$\scriptstyle\!\!n$}{op}{cl}

\end{document}


-
Thanks it's exactly what I was searching (didn't found it in the docs). There are some issues left: how can I change the fontsize of the small n and how can I shift the brackets in y-axis when I'm using them for connections other then 0 degree. Is there a way to align them? – Reza Feb 3 '13 at 20:00
@Reza see edited answer – clemens Feb 3 '13 at 22:17

The use of \rlap and \llap seems much easier in this case since there is no need to copy and adapt a complex macro:

\def\leftparen{\llap{$\left(\strut\right.$\kern5pt }}
\def\rightparen{\rlap{$\kern5pt \left.\strut\right)_n$}}
\chemfig{
CH_2=[:-30]CH-[:30]C(=[:90]O)-[:-30]O
-[:30]\leftparen CH_2CH_2|\rightparen
-[:-30]O-[:30]C(=[:90]O)-[:-30]CH=[:30]CH_2
}


which gives

That said, I'm not sure the right bracket is where the OP wants...

EDIT Always complicated, Clemens... You can do something simple which does not require the complicated xparse package:

\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)]
{\rlap{$\left\delimleft \vrule height\delimhalfdim depth\delimhalfdim width0pt \kern#5 \right\delimright_{#3}$}%
};%
}}
\setpolymerdelim[]
\chemfig[][scale=0.6]{
?(-[2]CH_2-[@{op,.8}:180,3])(-[::-30,1.5]O-[::+60](-[:-30,1.5]O-[:0,3])<[::-150,1.5](-[:-50]O(-[7](=[6]O)-[1](-[7]CH_3)=[8]CH_2))-[::-90,1.5,,,line width=3pt](-[:45,1.5]OH)>[::+60]?(-[:-120]OH))
}
\makebraces(5pt,90pt){n}{op}{130pt}


and the result is

-
You're right, he wanted it somewhere else (I missed that...). But it shouldn't be too hard to change either example to adapt. What I like better about the way given in the documentation is that once defined it's easy to apply to various different kinds of molecules. A document about polymers often has more than one of them. – clemens Feb 3 '13 at 19:48
Nice idea, I should have thought of a horizontal shift! :). Although I wouldn't call xparse complicated – clemens Feb 4 '13 at 8:38
xparse, not complicated??? Funny. I guess you probably have not read the code. In my opinion, I would call this package a very complicated thing. In this case, \kern and \rlap are more intuitive and simple. – unbonpetit Feb 4 '13 at 11:53
Ah, this has been a misunderstanding :). I did not mean the package code but the user interface... But I agree for the example at hand the horizontal shift is definitely easier. – clemens Feb 4 '13 at 11:55
Yes I like your solution, exactly what I was looking for. – Reza Feb 4 '13 at 19:52