I'm writting a ruby-on-rails application that fills latex formulars. Now I run into an pdflatex error when a user filed variable includes the character &
(e.g., Company & Co. KG). For latex it is a new table column and I need to escape these characters with \&
.
I'm using Rails 5.2.3
with ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]
on Ubuntu 18.04.3 LTS
.
I've tried using gsub
to replace all &
in a string but ran into two problems.
\&
is replaced by the matched string- I can only insert a double backslash
\\
So I also tried replace the character before a &
with a \
with no success.
a = "T & T"
a.gsub(&, "\&") -> "T & T"
a.gsub(/(.{1})(?=&)/, " \") -> error: unterminated string meets end of file
a.gsub(/(.{1})(?=&)/, " \\") -> "T \\& T"
How can I replace a character with a single \
or can I escape the reversed \&
somehow?