3

I'm trying to use a macro to generate arguments that should be passed to \charge. I'm trying this:

\documentclass{minimal}
\usepackage{chemfig}

\begin{document}

\charge{-135:1pt=5}{C}

\NewDocumentCommand{\myxyz}{}{-135:1pt=5}
\charge{\myxyz}{C}

\end{document}

The first \charge command works, but the second does not.

I get these error messages:

line 10: Paragraph ended before \charge_g was complete.
line 11: Improper \prevdepth. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing } inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing \endgroup inserted. \end{document}
line 11: Missing } inserted. \end{document}

I have also tried

\NewDocumentCommand{\myxyz}{}{-135:1pt=5}
\expandafter\charge{\myxyz}{C}

and

\NewDocumentCommand{\myxyz}{}{{-135:1pt=5}{C}}
\expandafter\charge\myxyz

What is the problem here and how can I make it work?

1 Answer 1

3

The problem is that \myxyz needs more than one expansion step to deliver the replacement text, due to how \NewDocumentCommand works.

Using \NewExpandableDocumentCommand doesn't solve the issue. But you can force full expansion.

\documentclass{article}
\usepackage{chemfig}

\NewExpandableDocumentCommand{\myxyz}{}{-135:1pt=5}

\begin{document}

\charge{-135:1pt=5}{C}

\bigskip

\ExpandArgs{e}\charge{\myxyz}{C}

\end{document}

enter image description here

Please, don't use the minimal class for examples: it's not meant for such jobs.

It would be possible to redefine \charge in such a way that it fully expands its argument. But I can't recommend doing it, as it's necessary to redefine internal commands of chemfig.

\documentclass{article}
\usepackage{chemfig}

\NewExpandableDocumentCommand{\myxyz}{}{-135:1pt=5}

\catcode`_=11
\def\charge_a#1#2{% #1=TF #2=liste emplacements
  \expanded{\unexpanded{\CF_testopt{\charge_b{#1}}{}}#2}\_nil
}
\catcode`_=8

\begin{document}

\charge{-135:1pt=5}{C}

\bigskip

\charge{\myxyz}{C}

\end{document}
4
  • Is there a way to make this work with \def? i.e. if I define the command like \def\myxyz{-135:1pt=5}?
    – ttsc
    Commented May 23 at 10:02
  • 1
    @ttsc No, you'd still need \expandafter\charge\expandafter{\myxyz} or just \ExpandArgs{e}\charge{\myxyz}. However, see the addition, if you want to live dangerously.
    – egreg
    Commented May 23 at 11:23
  • Ok. I see. Thank you for explaining. And btw, why is it not a good idea to use the minimal class?
    – ttsc
    Commented May 23 at 19:07
  • 1
    The minimal class provides nothing for typesetting. It's a skeleton for testing other classes, particularly with respect to class options.
    – egreg
    Commented May 23 at 20:00

You must log in to answer this question.

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