# How to prevent Mustache from replacing & with &amp; etc.?

6 messages
Open this post in threaded view
|

 Hi Pharoers, I may be a bit wrong on this list. I try anyways. I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database. Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help. BTW: I already took the hurdle of Mustache excaping $& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost... So is there any best practice for this problem? Joachim Reply | Threaded Open this post in threaded view | ## Re: How to prevent Mustache from replacing & with &amp; etc.?  Hi, > Am 16.04.2019 um 09:02 schrieb [hidden email]: > > Hi Pharoers, > > > I may be a bit wrong on this list. I try anyways. > > I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database. > > Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help. It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped. > > BTW: I already took the hurdle of Mustache excaping$& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost... > > > So is there any best practice for this problem? > Yes, use it this way '{{{ value }}}' asMustacheTemplate value: { #value -> '&' } asDictionary Norbert
 Norbert, thanks for answering. I had tried the {{{ }}} option. In case of LaTeX, this is not so easy, however, because {{{ and }}} are completely valid syntactical elements of LaTeX. It seems like you can replace {{ and }} in Mustache, but not {{{ and }}}. At least it didn't work when I tried. Using '{{{=### ###=}}} ' in my MustacheTemplate would leave text like '###myObject.printString###' unchanged in the resulting text instead of replacing it with the text representation of myObject. So what I tried last and what gave me quite some mileage on the way to a sulotion was to use \catcode to replace & with ; in my LaTeX file for the Column separator problem {{=## to make sure I can fill in my table rows with ; as column separator. Thus my Mustache-Template looks like this (excerpt): % redefine Mustache-Insertion syntax {{=## ##=}} \catcode\;=4 % redefine ; for & to allow non-escaped column-separators in \tabular ... \begin{tabular}{ccp{6.0cm}rrr} ##myObject.asLatexTableRows##  % This is where & is needed for LaTeX to separate table columns, but Mustache would replace the with the html entity & \end{tabular} This is a bit hacky, and as I wrote, also still presents problems, because if any of the Strings being inserted using Mustache contains an &, there will be an & in the .tex file, which will lead to a LaTeX error. So, essentially, what I am most likely looking for is a way to replace {{{ and }}} in Mustache to make sure the $&'s will remain untouched in my inserted text... Sorry for asking the wrong question, I was so glad how far I'd gotten with what I did that I didn't see the forest between all the trees. So my real question is: how can I replace {{{ in Mustache with something that will not irritate LaTeX? Any ideas? Joachim Am 16.04.19 um 12:00 schrieb Norbert Hartl: > Hi, > > >> Am 16.04.2019 um 09:02 schrieb [hidden email]: >> >> Hi Pharoers, >> >> >> I may be a bit wrong on this list. I try anyways. >> >> I am using Mustache to fill text into a String which will then be handed over to LaTeX. The Data I fill in comes from the Database. >> >> Now the problem is this: if a String from the database contains an ampersand (like in 'Katz & Maus'), Mustache will replace special chars with HTML entities. That is fine in a web context, but LaTeX doesnt actually work well with HTML entities. I could, of course, remove the html entities after Mustache invested a lopt of work into creating them. But it seems more logical to keep Mustache from being so eager to help. > It is not logical because mustache is defined that way. So redefining does not improve it. But just use triple {{{ }}} to have strings not being escaped. > >> BTW: I already took the hurdle of Mustache excaping$& fir tabular environments (LaTeX uses the ampersand as a divider for table cells) by using \catcode in our .tex template files. But now the String comes from the database and so I am a bit lost... >> >> >> So is there any best practice for this problem? >> > Yes, use it this way > > '{{{ value }}}' asMustacheTemplate value: { #value -> '&' } asDictionary > > Norbert > > > -- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchel          mailto:[hidden email] Fliederweg 1                         http://www.objektfabrik.deD-71640 Ludwigsburg                  http://joachimtuchel.wordpress.comTelefon: +49 7141 56 10 86 0         Fax: +49 7141 56 10 86 1