olsak / OpTeX

OpTeX - LuaTeX format with extended Plain TeX macros
http://petr.olsak.net/optex/
35 stars 14 forks source link

lower index after cs causes an error sometimes #12

Closed gromadan closed 3 years ago

gromadan commented 3 years ago

Using the underscore for making a lower index after some control sequence (such as sum) causes some random errors sometimes.

For example

Summation $\sum_k$  %needs to be the first line of the file
\bye

yields an error, while

%comment
Summation $\sum_k$
\bye

works fine.

Some more likely issue:

\sum_k\sum_l

yields an error, while

\sum_k \sum_l %with an additional space
\sum\sum_l    %without the first index
\sum_k\sum    %without the second index

works fine.

Also \sum_kA yields an error, while \sum_k A is fine.

I am not sure if this is an easily repairable bug, or whether it is an unavoidable consequence of changing the category of the underscore. From the user point of view, it is surely easily solvable by just adding the space as I mentioned. But it might be slightly confusing that a formula, which works well in plainTeX as well as LaTeX, does not work here.

olsak commented 3 years ago

Unfortunately, we must to accept this as the feature of OpTeX because there are limitations of features of TeX and luaTeX. The decision in OpTeX is: to set catcode of _ as letter, because we want to have simple access to the control sequences like \_foo or \_foo_AB or \foo_AB

On the other hand we want to keep the math behavior of _, so it is set as math-active character with meaning "math subsript" and the occurrences of \int_a^b or \int_{ab}^c are transformed to \int _a^b or \int _{ab}^c by lua pre-processor. See the technical documentation about it for more details.

Unfortunately, LuaTeX is able to intitalize its lua preprocessors only after first line of the document is already read. So, the preprocessor works from second line line in whole document. This is reason of your problems: if your math is written to second (or more) line of your document then preprocessor works.

Moreower, we suppose that user write \sum_k A becuase \sum_kA is less readable in source code. So, we were decided that preprocessor ignores the occurrences of \foo_more_than_one_letter. User can define his/her own macros of type \mymacro_first, \mymacro_second.

If you need not to use any control sequence with _ in its name in your document then you can set \mathsboff \catcode=8at the beginning of your document. Then the behaviour of` in math is exactly as in plain TeX.

I will add more information about it into the documentation.