2

I would like to write a script for doing my biochemistry homework that utilizes variable substitution. For example, I could create a base molecule using ChemFig with an X on it, then substitute that X for the appropriate group.

\documentclass[graphicx,longbibliography,notitlepage]{revtex4-1}
\usepackage{chemfig}
\newcommand{\X}{}
\newcommand{\propyl}{\chemfig{CH_3-[:30]-[:-30]\X}}
\begin{document}
%if input=propane:
\renewcommand{\X}{CH_3}
\propyl\\ %substitute 'CH_3' for X
%if input=propanol
\renewcommand{\X}{-[:30]OH}
\propyl %substitute '-[:30]OH' for X
\end{document}

Then, if the input was propane the 'X' would be replaced with 'CH_3', if the input was propanol, the X would be replaced with '-[:30]OH'. However, instead of making a new branch with the OH, it just displays the -[:30]OH

What I get: 30OH What I want: OH

Is something like that possible with latex/chemfig? I looked at some variable substitution things on here, but didn't seem to be quite close enough to what I'm looking for. Sorry for not providing more code for it, but I don't even know if it's possible. If it is, if someone could point me in the right direction, that'd be great (don't need to write my code for me). Thanks!

1 Answer 1

1

Something like this, perhaps?

\documentclass{article}
\usepackage{chemfig}
\newcommand{\propyl}[1]{\chemfig{H_3C-[:30]-[:-30]!{#1}}}

\definesubmol{propane}{CH_3}
\definesubmol{propanol}{-[:30]OH}
\begin{document}
%if input=propane:
\propyl{propane} %substitute 'CH_3' for X

\bigskip

%if input=propanol
\propyl{propanol} %substitute '-[:30]OH' for X
\end{document}

chemfig

The \definesubmol command plays the role of your X.

2
  • That's awesome! Thanks! I didn't see the \definesubmol command, I guess I missed that. Thank you very much.
    – devin
    Oct 21, 2017 at 16:24
  • No problem :) --- Welcome to TeX.SE! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you.
    – Troy
    Oct 21, 2017 at 16:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .