Escaping unkown user input to LaTeX

I’m using PGFPlotsX and LaTeXTabulars to generate PDF reports which include (amongst other things) string comments (user-input via some beyond-my-control GUI) that might contain any of these characters:

"""!@#$%^&*()_-+={[}]|\"':;?/>.<,~`""" # I basically went through the keyboard with/out pressing SHIFT

I need to be able to insert these comments without LaTeX crashing or misinterpreting them in math-mode. I honestly don’t care about how the text will look like (in terms of typography), I just want it to work. I thought about surrounding everything with

"\\detokenize{$x}"

where x contains the suspicious string, but I hear that might not cover all cases?

Is there a way to fix this

  1. from Julia?
  2. or if you know, from LaTeX? I couldn’t find anything definitive, but I imagined that warpping every such string in some protective environment might do?

Thanks in advance!

You can define a simple function in Julia that escapes all LaTeX reserved chars, which I think are

# $ % ^ & _ { } ~ \
1 Like

Awesome, simple and works. Thanks.