I would like to redefine the \section macro in my document and I found that LaTeX3e was one of the easiest way to make integer operations inside it. But I get a "Missing $ inserted" error when I compile. A minimal example is the following :


    \int_zero_new:N \g_int

        \int_incr:N \g_int


Why doesn't the above code work ?

  • The \ExplSyntaxOn and \ExplSyntaxOff markers should go around the definition, not inside it. – egreg Jul 8 '16 at 12:57
  • I thought it only made _ and : letters. If you turn your comment into a more detailed answer (why wouldn't it work), I will accept it. – Florian Jul 8 '16 at 12:58
  • Analogous with \makeatletter here. – Manuel Jul 8 '16 at 13:22

You should read What do ExplSyntaxOn and ExplSyntaxOff do? and its sibling What do \makeatletter and \makeatother do?

When TeX reads a definition, it just stores the tokens it is given, without doing any assignment. In particular, \ExplSyntaxOn in your definition is not performed and so \fooinc is defined to expand to the following tokens (I use • as token separator, for better reading):

\ExplSyntaxOn•\int•_•i•n•c•r•:•N• •\g•_•i•n•t• •\ExplSyntaxOff

so the macro \ExplSyntaxOn will be executed too late.

The correct way is

\int_zero_new:N \g_florian_whatever_int

\cs_new_protected:Npn \fooinc
  \int_gincr:N \g_florian_whatever_int

You have declared your integer parameter as global, so it should always be managed with global assignments. And \g_int is a badly chosen name.

Instead of \newcommand, it's better to use \cs_new_protected:Npn, if not \NewDocumentCommand of xparse (probably even a better choice).

| improve this answer | |
  • I would like to use large portions of text inside my macros, is there a way not to replace all the spaces with ~ ? – Florian Jul 10 '16 at 12:07
  • @Florian Not sure what you mean. – egreg Jul 10 '16 at 12:12
  • If I put \ExplSyntaxOn and \ExplSyntaxOff around my macro, then spaces inside it are ignored. But I would like to put a large portion of text with spaces inside it. Is there a way to write them normally (" " instead of "~") to ensure legebility ? – Florian Jul 10 '16 at 12:19
  • @Florian There are several ways. An example in a new question would be better. – egreg Jul 10 '16 at 12:20

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.