How to replace latex macros with your definitions (using latex)

How can I replace all user-generated latex macros with my definitions?

For example, this file

old.tex

\newcommand{\blah}[2]{#1 \to #2} ... foo \blah{egg}{spam} bar ... 

how to generate the file below automatically

new.tex

 ... foo egg \to spam bar ... 

Instead of overriding latex macro logic with perl, can I use the latex itself or the tex engine to do this?

+8
substitution latex
source share
4 answers

I never saw this done, but 2 ideas and a half baking:

  • If the reason you want to deploy all of these built-in macros is for debugging, then setting \tracingmacros=1 in the document will expand all your macros, but the output will be sent to the log file.

  • The CTAN archive provides a package that you can use for built-in extensions in definitions (but not in a new command), but I did not know if you can take a look and see how painfully this can be changed to run built-in extensions \ newcommand instead of \ deferred

+1
source share

voila http://www.ctan.org/tex-archive/support/de-macro

This is Python, which:

[...] will expand the macros defined in the (re) newcommand or (re) newenvironment commands, inside the document or in the file of personal document files.

+7
source share

Consider using a template engine, such as Jinja2 with Python.

You can change the default syntax {%, {{etc.) to make it more compatible with LaTeX. For example:

 env = jinja2.Environment( loader=jinja2.FileSystemLoader( JINJA_DIRS ), comment_start_string='["', # don't conflict with eg {#1 comment_end_string = '"]', block_start_string = '[%', block_end_string = '%]', variable_start_string = '[=', variable_end_string = ']', autoescape=True, finalize=_jinja2_finalize_callback, # make a function that escapes TeX ) template = env.get_template( self.template ) tex = template.render( content ) 

In addition to the functions that are passed to the template environment, Jinja2 supports macros . For example, your above code should work as expected, like:

 [% macro blah(egg, spam) -%] foo [=egg] \to [=spam] bar [%- endmacro %] [= blah("chicken","pork") ] % substitutes with "foo chicken \to pork" 

I'm not sure what your goals are, and it does take a little work, but it is not an insurmountable problem if you are familiar with Python.

I hope this helps.

+1
source share

I wrote a C program back in 2007 for the \ newcommand extension: http://www.gtoal.com/src/newcommand/ - I think it was not indexed at the time this question was published. Mentioning it now for those who are still looking for such a thing and find this page.

From the code ...

 // FOR DOCUMENTATION, SEE MY BLOG POST: // http://techennui.blogspot.com/2007/11/quick-hack-17-in-series-of-42-inlining.html // Expands LaTeX \newcommand macros to allow submission of documents // to print services which do not allow user-defined macros. // Valid input formats are: // \newcommand{\whatever}{Replacement text} // \newcommand{\whatever}[2]{Expand #1 and #2 but not \#1 or even $\#1$} // - anything else ought to be passed through verbatim; if an insurmountable // error is detected, the program exits with a non-0 return code. // The purpose of this utility is similar to: // http://winedt.org/Macros/LaTeX/uncommand.php // which I wasn't aware of when I wrote it. Though I would like to see how // well that program handles the test input file, to see if it does the // right thing with some of the more complex definitions :-) // // See also http://texcatalogue.sarovar.org/entries/de-macro.html // and http://www.mackichan.com/index.html?techtalk/685.htm~mainFrame 
0
source share

All Articles