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.

%if input=propane:
\propyl\\ %substitute 'CH_3' for X
%if input=propanol
\propyl %substitute '-[:30]OH' for X

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


Something like this, perhaps?


%if input=propane:
\propyl{propane} %substitute 'CH_3' for X


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


The \definesubmol command plays the role of your X.

  • 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 .