Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically escaping slashes in m4 for shell commands

Tags:

m4

I'm writing macros with m4 to easily embed math in HTML code. Slashes need to be escaped and I want to automate this process but I have not yet figured out a good way.

Running m4 on the following shows the problem:

define(`_imath', `esyscmd(`echo "$ $1 $"')')dnl
_imath(y = \frac{1}{2} x^{2.3} + 2)

My output in the following:

$ y = 
      rac{1}{2} x^{2.3} + 2 $

If I replace \frac with \\\frac then I get the desired result. I could do that every time, but I want to automate this process if I can. The desired result, by the way, is

$ y = \frac{1}{2} x^{2.3} + 2 $

which is piped to another command that'll produce HTML code or an image (outside of this minimal example).

I know one unsatisfactory way to get around this problem. I could use a different character (or combination of characters) for TeX's \ and define another macro to automatically replace it. This is unsatisfactory because I want to use straight LaTeX code without modification and it is not significantly simpler than using three slashes.

How can I change my macro to automatically escape the slash correctly so I can use straight LaTeX code?

like image 687
Ben Trettel Avatar asked Oct 09 '22 02:10

Ben Trettel


1 Answers

It took me long enough to figure this out, but this works:

define(`_imath', `esyscmd(echo "$ patsubst(`$1',`\\',`\\\\\\') $")')dnl
_imath(y = \frac{1}{2} x^{2.3} + 2)
like image 93
Ben Trettel Avatar answered Oct 13 '22 09:10

Ben Trettel