# chemfig/chemmacros: Vertical alignment of polymer brackets

Is there a way to adjust the horizontal alignment of the polymer brackets of the chemmacros package? It's just a little, unimportant detail, but I am curious.

MWE:

\documentclass{scrreprt}
\usepackage{chemformula}
\usepackage{chemfig}
\setangleincrement{30}
\usepackage{chemmacros}
\chemsetup{modules={polymers}}

\begin{document}
\begin{figure}[ht]
\centering
\small
\setatomsep{18.34pt}
\chemfig{([:-126]O*5(--(-[@{d}:210]O-[:150]-[:210]-[@{c}:150]HO)-(-[@{e}-1]O-[1]-[-1]-[@{f}1]OH)-(-[1](-[:90]-[@{i}1]O-[-1]-[1]-[@{j}-1]O-[1](=[:90]O)-[-1]-[1]-[-1]-[1]-[-1]-[1]-[-1]-[1]=[-1]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330])-[@{g}-1]O-[1]-[-1]-[@{h}1]OH)-))}
\makepolymerdelims[subscript={\small w}]{8.15pt}{c}{d}
\makepolymerdelims[subscript={\small x}]{8.15pt}{e}{f}
\makepolymerdelims[subscript={\small y}]{8.15pt}{g}{h}
\makepolymerdelims[subscript={\small z}]{8.15pt}{i}{j}
\end{figure}
\end{document}


Current Output:

Desired: (ignore the different brackets, Source: Wikipedia)

The reason for the misaligned brackets is that the nodes of both brackets aren't aligned in the first place:

\documentclass{article}

\usepackage{chemfig}

\begin{document}

\setatomsep{18.34pt}
\chemfig{-[@{a}:-30]O-[::60]-[::-60]-[@{b}::60]OH}

\chemmove{
\draw[red] (a) -- (b) ;
}

\end{document}


A simple solution is to use the nodes (a-|b) (together with (a)) or (a|-b) (together with (b)). Please have a look at the PGF manual for a explanation of the -| syntax (p 137f section 13.3.1 Intersections of Perpendicular Lines). The following probably gives you a hint:

\documentclass{article}

\usepackage{chemfig}

\begin{document}

\setatomsep{18.34pt}
\chemfig{-[@{a}:-30]O-[::60]-[::-60]-[@{b}::60]OH}

\chemmove{
\draw[red] (a) -- (b) ;
\draw[green] (a) -- (a-|b) ;
\draw[blue] (a|-b) -- (b) ;
}

\end{document}


Using them with the brackets:

\documentclass{article}

\usepackage{chemfig}

\usepackage{chemmacros}
\chemsetup{modules={polymers}}

\begin{document}

\setatomsep{18.34pt}
\chemfig{-[@{a}:-30]O-[::60]-[::-60]-[@{b}::60]OH}

\makepolymerdelims{8.15pt}{a|-b}{b}

\end{document}


Applying the above to your example:

\documentclass{article}

\usepackage{chemfig}
\setangleincrement{30}
\usepackage{chemmacros}
\chemsetup{modules={polymers}}

\begin{document}

\small
\setatomsep{18.34pt}
\chemfig{([:-126]O*5(--(-[@{d}:210]O-[:150]-[:210]-[@{c}:150]HO)-(-[@{e}-1]O-[1]-[-1]-[@{f}1]OH)-(-[1](-[:90]-[@{i}1]O-[-1]-[1]-[@{j}-1]O-[1](=[:90]O)-[-1]-[1]-[-1]-[1]-[-1]-[1]-[-1]-[1]=[-1]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330])-[@{g}-1]O-[1]-[-1]-[@{h}1]OH)-))}
\makepolymerdelims[subscript={\small$w$}]{8.15pt}{c}{c-|d}
\makepolymerdelims[subscript={\small$x$}]{8.15pt}{e|-f}{f}
\makepolymerdelims[subscript={\small$y$}]{8.15pt}{g|-h}{h}
\makepolymerdelims[subscript={\small$z$}]{8.15pt}{i|-j}{j}

\end{document}


Unfortunately the depth of the subscripts still can cause misalignment (here in case of y as subscript). This can be solved by smashing the subscripts.

I will add this to the next version of chemmacros. Meanwhile here is a fix:

\documentclass{article}

\usepackage{chemfig}
\setangleincrement{30}
\usepackage{chemmacros}
\chemsetup{modules={polymers}}

\ExplSyntaxOn
\chemmacros_if_compatibility:nnF {>} {5.8b}
{
\chemmacros_define_keys:nn {polymers}
{
subscript .code:n =
\tl_set:Nn \l__chemmacros_polymer_subscript_tl { \smash {#1} }
}
}
\ExplSyntaxOff

\begin{document}

\small
\setatomsep{18pt}
\chemfig{([:-126]O*5(--(-[@{d}:210]O-[:150]-[:210]-[@{c}:150]HO)-(-[@{e}-1]O-[1]-[-1]-[@{f}1]OH)-(-[1](-[:90]-[@{i}1]O-[-1]-[1]-[@{j}-1]O-[1](=[:90]O)-[-1]-[1]-[-1]-[1]-[-1]-[1]-[-1]-[1]=[-1]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330])-[@{g}-1]O-[1]-[-1]-[@{h}1]OH)-))}

\makepolymerdelims[subscript={\small$w$}]{10pt}{c}{c-|d}
\makepolymerdelims[subscript={\small$x$}]{10pt}{e|-f}{f}
\chemmove{\draw[red,-] (g|-h) -- (h) ;}
\makepolymerdelims[subscript={\small$y$}]{10pt}{g|-h}{h}
\makepolymerdelims[subscript={\small$z$}]{10pt}{i|-j}{j}

\end{document}


• I think there is mistake in the explanation according to the example. "A simple solution is to use the nodes (a-|b) (together with (b)) or (a|-b) (together with (a))". Moreover, one of the polymer brackets (subscript y from my example above) is the only one, which is still not correctly aligned with this method. Bug or am I just incapable? – D5E Mar 1 '18 at 12:48
• Look at the alignment of the subscript y brackets of your example. It looks still not aligned, or? Can we solve this? – D5E Mar 1 '18 at 13:48
• @clemens The two nodes are now indeed vertically aligned, but the braces are aligned only at their respective node and they don’t use the same baseline. Hence, if there is a subscript text attached, the brace is shifted slightly upwards, in order to align brace and attached text vertically, which, however, is not wanted here. Correct alignment would only work with text depth=0pt, I guess. – Jasper Habicht Mar 1 '18 at 18:03
• @DSE see my edited answer – cgnieder Mar 2 '18 at 13:58
• Your solution works now as desired. Thank you a lot! Looking forward to the new version of chemmacro. – D5E Mar 2 '18 at 16:31

I guess, this is because you use the brackets on sloped edges. But you can use different ways to shift the brackets and define their height and depth. However, even with the following setup, the braces are not exactly aligned:

\documentclass{scrreprt}
\usepackage{chemformula}
\usepackage{chemfig}
\setangleincrement{30}
\usepackage{chemmacros}
\chemsetup{modules={polymers}}

\begin{document}

\begin{figure}[ht]
\centering
\small
\setatomsep{18.34pt}
\chemfig{([:-126]O*5(--(-[@{d,.75}:210]O-[:150]-[:210]-[@{c,.75}:150]HO)-(-[@{e,.75}-1]O-[1]-[-1]-[@{f,.75}1]OH)-(-[1](-[:90]-[@{i,.75}1]O-[-1]-[1]-[@{j,.75}-1]O-[1](=[:90]O)-[-1]-[1]-[-1]-[1]-[-1]-[1]-[-1]-[1]=[-1]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330])-[@{g,.75}-1]O-[1]-[-1]-[@{h,.75}1]OH)-))}
\makepolymerdelims[subscript={\small w}]{7pt}[11pt]{c}{d}
\makepolymerdelims[subscript={\small x}]{7pt}[11pt]{e}{f}
\makepolymerdelims[subscript={\small y}]{7pt}[11pt]{g}{h}
\makepolymerdelims[subscript={\small z}]{7pt}[11pt]{i}{j}
\end{figure}

\end{document}


Edit: The first problem is, that the nodes where the braces are attached to are not aligned vertically. This problem can be solved with clemens’s answer. The second problem is, that even if both nodes are aligned, the braces may still not be aligned, because each brace is vertically centred at its respective node, but the right brace has the subscript text attached to it making it being slightly shifted upwards. This is, because the braces do not use the same baseline.

You can, however, create you own custom macro, which is a variant of the predefined macro. It places a third node at the center of the referenced nodes. This node contains both braces plus some horizontal space between. This way, both braces use the same baseline. Drawback: a lot of fine tuning is needed:

\documentclass{scrreprt}
\usepackage{chemfig}
\setangleincrement{30}
%\usepackage{chemformula}
%\usepackage{chemmacros}
%\chemsetup{modules={polymers}}

\newcommand{\makemypolymerdelims}[7][]{%
\chemmove{\path (#6) -- node[pos=#4] {$\left[\vrule height#2 depth#3 width0pt\hspace{#5}\right]_\textrm{#1}$} (#7);}%
}

\begin{document}

\begin{figure}[ht]
\centering
\small
\setatomsep{18.34pt}
\chemfig{([:-126]O*5(--(-[@{c}:210]O-[:150]-[:210]-[@{d}:150]HO)-(-[@{e}-1]O-[1]-[-1]-[@{f}1]OH)-(-[1](-[:90]-[@{i}1]O-[-1]-[1]-[@{j}-1]O-[1](=[:90]O)-[-1]-[1]-[-1]-[1]-[-1]-[1]-[-1]-[1]=[-1]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330]-[:270]-[:330])-[@{g}-1]O-[1]-[-1]-[@{h}1]OH)-))}

\makemypolymerdelims[w]{8pt}{8pt}{.45}{40pt}{c}{d}
\makemypolymerdelims[x]{8pt}{8pt}{.59}{40pt}{e}{f}
\makemypolymerdelims[y]{8pt}{8pt}{.59}{40pt}{g}{h}
\makemypolymerdelims[z]{8pt}{8pt}{.59}{40pt}{i}{j}
\end{figure}

\end{document}


Yields exactly aligned brackets: