2

I am trying to use the following custom pgfdecoration to draw bonds with varying length of the double bond while using chemfig. The code was obtained from the following link: How to alter the bond length of a double bond in chemfig?. However, I am unable to run the code after recently installing MikTeX and executing the code throws the following error message: Undefined control sequence. ...]--[::-60,,,,,rsdbond={120}{120}]-[::-60]}. I have tried updating pgf and chemfig from the MikTeX console but to no avail.

Does someone know what package I am missing and how to successfully execute this code (see below).

\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations}
\makeatletter
\newdimen\mystartshorten
\newdimen\myendshorten
\mystartshorten0pt
\myendshorten0pt
\pgfdeclaredecoration{sdbond}{initial}{
  \state{initial}[width=\pgfdecoratedremainingdistance,next state=final]
  {
    {
      \pgftransformyshift{\CF@double@sep}
      \pgfpathmoveto{\pgfqpoint{\mystartshorten}{0pt}}
      {
        \pgftransformxshift{\pgfdecoratedremainingdistance}
        \pgfpathlineto{\pgfqpoint{-\myendshorten}{0pt}}
      }
    }
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
  }
  \state{final}
  {}
}
\tikzset{
  lsdbond/.code 2 args={%
    \tikzset{decorate, decoration=sdbond}%
    \pgfmathsetlength\mystartshorten{\CF@double@sep*cot((#1)/2)}%
    \pgfmathsetlength\myendshorten{\CF@double@sep*cot((#2)/2}%
  },
  rsdbond/.code 2 args={%
    \tikzset{decorate, decoration={sdbond, mirror}}%
    \pgfmathsetlength\mystartshorten{\CF@double@sep*cot((#1)/2)}%
    \pgfmathsetlength\myendshorten{\CF@double@sep*cot((#2)/2}%
  }
}
\setchemfig{
    double bond sep=0.4em,
    atom sep=2em,
}
\makeatother
\begin{document}
\chemfig{[:60]--[::-60,,,,,rsdbond={120}{120}]-[::-60]}
\chemfig{[:90]--[::-90,,,,,rsdbond={90}{60}]-[::-120]}
\end{document}

  • The "protected" character has changed from @ to _, see e.g. here for a related problem. You'll need to go through the steps indicated in section 10 of the chemfig manual. – Schrödinger's cat Sep 20 '19 at 18:01
  • Change \makeatletter to \catcode`\_11, \makeatother to \catcode`\_8, and \CF@double@sep to \CF_doublesep. – Schrödinger's cat Sep 20 '19 at 18:08
  • @Schrödinger'scat, I changed the \makeatletter and \makeatother statements to what you suggested. I also looked at the chemfig manual, and tried implementing the test code in Section 6 on page 29 for creating delocalized double bonds. But I still get the same error as mentioned earlier Undefined control sequence.... The code in chemfig manual, however, still uses @ instead of _. – scorpionwars Sep 20 '19 at 18:24
3

The "protected" character has changed from @ to _. You'll need to go through the steps indicated in section 10 of the chemfig manual. Here is what this yields on my updated TeXLive2019 installation:

\documentclass{article}
\usepackage{chemfig}
\usetikzlibrary{decorations}
\catcode`\_11
\newdimen\mystartshorten
\newdimen\myendshorten
\mystartshorten0pt
\myendshorten0pt
\pgfdeclaredecoration{sdbond}{initial}{
  \state{initial}[width=\pgfdecoratedremainingdistance,next state=final]
  {
    {
      \pgftransformyshift{\CF_doublesep}
      \pgfpathmoveto{\pgfqpoint{\mystartshorten}{0pt}}
      {
        \pgftransformxshift{\pgfdecoratedremainingdistance}
        \pgfpathlineto{\pgfqpoint{-\myendshorten}{0pt}}
      }
    }
    \pgfpathmoveto{\pgfpointorigin}
    \pgfpathlineto{\pgfqpoint{\pgfdecoratedremainingdistance}{0pt}}
  }
  \state{final}
  {}
}
\tikzset{
  lsdbond/.code 2 args={%
    \tikzset{decorate, decoration=sdbond}%
    \pgfmathsetlength\mystartshorten{\CF_doublesep*cot((#1)/2)}%
    \pgfmathsetlength\myendshorten{\CF_doublesep*cot((#2)/2}%
  },
  rsdbond/.code 2 args={%
    \tikzset{decorate, decoration={sdbond, mirror}}%
    \pgfmathsetlength\mystartshorten{\CF_doublesep*cot((#1)/2)}%
    \pgfmathsetlength\myendshorten{\CF_doublesep*cot((#2)/2}%
  }
}
\setchemfig{
    double bond sep=0.4em,
    atom sep=2em,
}
\catcode`\_8
\begin{document}
\chemfig{[:60]--[::-60,,,,,rsdbond={120}{120}]-[::-60]}
\chemfig{[:90]--[::-90,,,,,rsdbond={90}{60}]-[::-120]}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.