3

So I was looking at my preamble, with Overleaf yelling at me for not putting a closing bracket after the piece of code attached below, which is strange, since that group is indeed closed. To confirm, I removed several commands until I realised that the problem arose on line 6 with \cs:w.

Any thoughts?

\ExplSyntaxOn
\NewDocumentCommand{\newenvcommand}{ m m } % #1 = env name, #2 = command name
{
    \cs_if_exist:cF { g_envc_#1_list_tl } { \tl_new:c { g_envc_#1_list_tl } }
    \tl_gput_right:cn { g_envc_#1_list_tl } { #2 }
    \exp_after:wN \newcommand \cs:w envc_#1_\cs_to_str:N #2 \cs_end:
}
\NewDocumentCommand{\checkenvcommands}{ }
{
    \cs_if_exist:cT { g_envc_\use:c {@currenvir} _list_tl }
    {
        \tl_map_inline:cn { g_envc_\use:c {@currenvir} _list_tl }
        {
            \cs_set_eq:Nc ##1 { envc_\use:c {@currenvir} _\cs_to_str:N ##1 }
        }
    }
}
\ExplSyntaxOff
Matt
  • 407
  • Welcome to TeX.SX! Are you talking about Overleaf's syntax highlighting? Or the code is not working? If it's the latter, then please add a compilable example. – Phelype Oleinik Nov 22 '19 at 22:36
  • If a moderator could help me out with the tags for this question, that'd be great. – Matt Nov 22 '19 at 22:36
  • Oh, hey there! I am using Overleaf, and my entire preamble is highlighted from line 3 in the sample and down. Only when I removed \cs:w did the colour and warning disappear. Is it just Overlead, or a general TeX thing? And how do I get rid of the warning? – Matt Nov 22 '19 at 22:38
  • Yes, I just tested and it's just Overleaf being annoying (in my opinion, their syntax highlighting is far too intrusive). In my editor everything shows up normally. Usually you can ignore syntax highlighting problems (although this one is pretty annoying) if you know the code is working. Overleaf seems to be happy if you add a commented {} after \cs_end:: \cs:w envc_#1_\cs_to_str:N #2 \cs_end: % { }... – Phelype Oleinik Nov 22 '19 at 22:40
  • Well, that worked! Thanks a lot, Phelype! :-) – Matt Nov 22 '19 at 22:44
  • You're welcome :-) I'll vote to close this question as off-topic, because it's an Ovealeaf-specific issue, but you are welcome to ask again should you have other problems :-) – Phelype Oleinik Nov 22 '19 at 22:46
  • 4
    I'm voting to close this question as off-topic because the issue is due to Overleaf's syntax highlighting expecting a {} after a \newcommand. The problem was solved adding a commented {} after \newcommand. – Phelype Oleinik Nov 22 '19 at 22:47
  • Why are you using \cs:w in the first place? Use \exp_args:Nc \newcommand { envc_#1_\cs_to_str:N #2 } instead. – egreg Nov 22 '19 at 23:00
  • I think I copied it from an old answer of yours? https://tex.stackexchange.com/a/48122/183025 – Matt Nov 26 '19 at 14:44

2 Answers2

6

(I'm on support staff at Overleaf.)

The code aspects have already been addressed in another answer. I'll focus on "Overleaf yelling at you" :-).

Overleaf uses chktex as the in-editor syntax checker. This works well for typical documents. But if you have certain TeX programming constructs in your code, chktex can report false positives, where valid code is flagged as an error.

You can use a few strategies to disable the syntax checker for problematic parts of your project:

  1. You can disable code check entirely in the project settings (Overleaf menu on the upper left).
  2. You can disable code check for a particular file by adding %%novalidate as the first line of the file.
  3. You can disable code check for portions of a file by surrounding the lines to ignore with %%begin novalidate and %%end novalidate.

Source: Code Check on Overleaf

Paul Gessler
  • 29,607
  • I was not aware of this. Thank you for the information! :0

    I'm not sure who to mark as having answered, or if I can even mark you both. You technically answered my question in the most direct way, while egreg generously optimised my code to also eliminate the problem.

    But still, thanks a lot. This information was very useful! :-)

    – Matt Nov 26 '19 at 14:37
5

I don't know about Overleaf yelling at you. But you can surely avoid it by using better code:

\ExplSyntaxOn
\NewDocumentCommand{\newenvcommand}{ m m } % #1 = env name, #2 = command name
{
    \tl_if_exist:cF { g_envc_#1_list_tl } { \tl_new:c { g_envc_#1_list_tl } }
    \tl_gput_right:cn { g_envc_#1_list_tl } { #2 }
    \exp_args:Nc \newcommand { w envc_#1_\cs_to_str:N #2 }
}
\NewDocumentCommand{\checkenvcommands}{ }
{
    \tl_if_exist:cT { g_envc_\use:c {@currenvir} _list_tl }
    {
        \tl_map_inline:cn { g_envc_\use:c {@currenvir} _list_tl }
        {
            \cs_set_eq:Nc ##1 { envc_\use:c {@currenvir} _\cs_to_str:N ##1 }
        }
    }
}
\ExplSyntaxOff

With \exp_args:Nc, TeX will jump over the next token and make a control sequence name out of the tokens in the following braced group, which is the higher level expl3 counterpart of

\expandafter\newcommand\csname <tokens>\endcsname

in “old style” programming.

Note also \tl_if_exist:cF instead of \cs_if_exist:cF. You're checking a variable, not a function.

egreg
  • 1,121,712