FIX BUG: missing checking charAt position < length
{Causing TeXpert to crash when inserting `\section` at the end of file}