[Tex/LaTex] How to display special symbols in ConTeXt which are processed by Lua

contextcontext-mkivluasymbols

I have a ConTeXt document containing many special characters. I have followed the instructions at Which symbols need to be escaped in ConTeXt? to ensure they can all display properly in the document, however, as most of my text is processed by Lua, I found some problems. When \# appears in the document, because I want the symbol "#" to be displayed, Lua cannot compile. Also, when double quotes appear in the document, e.g. ", Lua cannot compile.

  • How can I get these symbols to be displayed in ConTeXt when used with Lua?
  • What other special characters need special treatment when used with Lua?

Update:

Here is some example code:

\define[2]\showtext{%
    \startlua
        if #1 < 2 then
            context("#2")
        end
    \stoplua%
}

\starttext
    \showtext{1}{This is text \#1.}
\stoptext

Best Answer

As @rdhs mentioned in his comment, you can use [[...]] for Lua strings. ConTeXt provides \!!bs ... \!!es macros that map to [===[ ... ]===] (this way your string can have literal [[ as well). So, the easiest way to define such a macro is:

\unprotect
\define[2]\showtext
  {\startlua
        if #1 < 2 then
            context(\!!bs#2\!!es)
        end
    \stoplua}
\protect

Another option is to use commands.doif series of Lua functions, which follow the same syntax as \doif series of ConTeXt macros:

\define[1]\showtext{\ctxcommand{doif(#1 < 2)}}

The latter has the advantage of being slightly faster (#2 is not scanned) and slightly more robust, e.g.,

\showtext {5}{\undefined}

works with the second definition but not the first.