What is the best way to escape special latex characters when using freemarker to create a .tex file?
Currently, I am passing a java function for escaping symbols and I have to wrap every variable into this function call. I know that freemarker has built-in escaping for some formats, but sadly latex is not supported.
This is how I do it at the moment:
${escapeFormatter.escapeSymbols(some.value)!}
I don't like it that I have to manually call this function for every value. How can I make it more generic?
You can register your own "output format" via freemarker.template.Configuration#setRegisteredCustomOutputFormats
. Then those can be used with the auto-escaping feature, just like the standard ones (see: https://freemarker.apache.org/docs/dgui_misc_autoescaping.html). See also OutputFormat
and MarkupOutputFormat
API documentation, and implementations in the source code as an example.