6

Working with LaTeX, I often have a compilation error when writing exponentials in mathematical expressions, even though the syntax is valid. Recently I discovered this was due to a 'Backspace' character (ASCII 0x08) that is typed by mistake, but doesn't appear in my IDE (TeXstudio, and more recently I've made LaTeX flashcards in Anki and I had the same problem).

I have a Canadian Multilingual Standard keyboard. In order to do the caret ^ character to write exponents, I press ^ (the key to the right of P), release then press Space. But sometimes I type too fast, I make a mistake and I want to go back, so I press ^ then Backspace right away, which inserts the infamous 'Backspace' character even though it's not displayed. pdfLaTeX will fail to compile because of the invisible character, so I have to copy/paste into Sublime Text (which displays the control character BS) or in Notepad (which displays a little box), erase a full line and type the text a second time before copying it again in my LaTeX editor.

This is very time consuming, so I'm hoping there is an easier way to solve the problem (besides not making any mistake when typing). Ideally, pdfLaTeX would simply ignore the 0x08 character and compile anyway. Perhaps I have to add something to the preamble regarding the character encoding? Or if there were a way to display the control character in TeXstudio and Anki like Sublime Text and Notepad do, at least I would spot the mistake right away without having to copy/paste in another editor.

Final comment: I thought the problem might be how my CMS keyboard is configured, since the combination ^, Backspace should not insert a 'Backspace' character. But I noticed the behaviour of this combination depends on the program I'm using: Notepad and Word do nothing, Sublime Text and TeXworks - which I don't like to use - delete the last character (like a simple Backspace), SE editor inserts a ^, etc. So the problem seems to come from some IDEs themselves (although I don't care much if the 0x08 character is inserted but I can find a way to ignore it when compiling).

6

LaTeX allows you to assign to each character a so-called catcode (category code), that determines how that character then is handled internally. This works for visible but also for invisible characters. The syntax for that is \catcode<char number>=<catcode number>.

You now have several options what to do with that character. Here are some ideas (note the (invisible) backspace characters between Invisible and backspace):

\documentclass{article}
\usepackage{xcolor}

\begin{document}
% 10 = Make it an ordinary space character, like '\x20'
\catcode8=10
Invisiblebackspace

% 9 = Ignore the character completely
\catcode8=9
Invisiblebackspace

% 13 = Make the character "active", i.e. treat it like a macro name
\catcode8=13
% ... and define a macro that adds a visual hint about that character in the final output
\def^^H{\textbf{\textcolor{red}{[\texttt{\char`\\0x08}]}}}
Invisiblebackspace
\end{document}

enter image description here

Note the \def^^H{...}, where ^^H is an alternative way to input the character '\x08'. The character position of H is 72 of which LaTeX subtracts 64 here to get to position 8.

  • It's a nice workaround. However Anki prevents the use of catcode in the header of flashcards. Instead it suggests putting the command in a separate package then importing that package. It works, but I would prefer not to modify an existing package. – Jasmeru Aug 28 '18 at 2:59
  • 3
    @Jasmeru You can of course put that definition into an extra .sty file, see this thread about how to write package files – siracusa Aug 28 '18 at 3:14
  • This is an excellent answer. – Steven B. Segletes Aug 28 '18 at 11:21

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.