Drawing histidine with chemfig

I'm trying to draw histidine with chemfig and I'm stuck. I drew most parts correctly but I'm having trouble attaching the imidazole ring properly. Here's what I have so far:

\documentclass{article}
\usepackage{chemfig}
\definesubmol{ring}{N*5(=-{NH}-(-)=-)} % imidazole ring

\begin{document}
\chemfig{!{ring}-[:30]-[:-30](-[:-90]NH_2)-[:30](=[:90]O)(-[:-30]OH)} % histidine
\end{document}


Is there an easy way to use my sub-molecule and attach it to the main part at the correct atom and angle?

Cheers :)

-

chemfig's molecules can get a default rotation by specifying an angle as option first in the molecule:

\chemfig{[:<angle>]...}


Choosing the right angle will do the trick here:

\documentclass{article}
\usepackage{chemfig}
\definesubmol{imidazole}{N*5(=-{NH}-(-)=-)} % imidazole ring

\begin{document}
\chemfig{[:168]!{imidazole}-[:30]-[:-30](-[:-90]NH_2)-[:30](=[:90]O)(-[:-30]OH)} % histidine
\end{document}


Before:

After:

The necessary further corrections should be simple (NH must be switched...)

Actually, to have some automatism you could exploit the optional argument to \definesubmol:

\definesubmol{<name>}[<molecule to the left>]{<molecule to the right>}


In this case:

\documentclass{article}
\usepackage{chemfig}
\definesubmol{imidazole}[N*5(=-{HN}-(-)=-)]{N*5(=-{NH}-(-)=-)} % imidazole ring

\begin{document}
\chemfig{!{imidazole}}

\chemfig{[:168]!{imidazole}}
\end{document}


\documentclass{article}
\usepackage{chemfig}
\definesubmol{imidazole}[N*5(=-{HN}-(-)=-)]{N*5(=-{NH}-(-)=-)} % imidazole ring

\begin{document}
\chemfig{[:168]!{imidazole}-[:30]-[:-30](-[:-90]NH_2)-[:30](=[:90]O)(-[:-30]OH)} % histidine
\end{document}


-
That worked like a charm, thank you! How did you determine the rotation angle though? –  dreddy Jul 12 at 23:10
Also, I'm drawing all amino acids and struggling with tryptophan's double ring right now. Would you mind if I shoot you a message if I can't figure it out? –  dreddy Jul 12 at 23:10
I just played a bit... 168=150+18 and 18° are all over the place in a pentagon... :) –  cgnieder Jul 12 at 23:14
Makes sense. Also, the angles aren't perfect but I figured out tryptophan: \chemfig{*6(-*5(-{NH}-=(-[:30]-[:-30](-[:-90]NH_2)-[:30](=[:90]O)(-[:-30]OH))-)=‌​-=-=)}. –  dreddy Jul 12 at 23:25

If it should like (near) exactly as in the picture, the following modification may be useful. The angles are assummed to be 30 and 120 degrees; hence the correction: 120+72=192.

Your original definition is left in the preamble for comparision.

\documentclass{article}
\usepackage{chemfig}
\definesubmol{ring}{N*5(=-{NH}-(-)=-)} % imidazole ring

\begin{document}

\definesubmol{ring}{*5(-{N}=-{HN}-=)} % imidazole ring

\chemfig{[:-192]!{ring}-[:30]-[:-30](-[:-90]NH_2)-[:30](=[:90]O)(-[:-30]OH)} % histidine
\end{document}
`

-