How can I replace all occurrence of user defined latex macros with their definitions?
For example, given this file
old.tex
\newcommand{\blah}[2]{#1 \to #2}
...
foo \blah{egg}{spam} bar
...
how to generate the file below in an automatic way
new.tex
...
foo egg \to spam bar
...
Instead of reimplementing latex macro logic with perl, can I use latex or tex engine itself to do this?
If you wish to redefine a macro that has optional parameters, you have to use \LetLtxMacro from the letltxmacro package . A detailed description of \LetLtxMacro can be found at this question at closed square root symbol.
Click the + button on the right and select Regular Expression . In the Find dialog box enter \[[0-9]*\] and select Replace All . This should delete all occurrences of text such as [8] , [75] , etc.
In computer jargon, a macro is just a shorthand command that abbreviates a more complicated sequence of commands. For example, LaTeX's \section macro encodes instructions to leave some vertical space, to increment the section number counter, and to format a section heading.
The \begin{} command tells LaTeX to entire a certain environment while the \end{} command concludes that environment. \begin{document} will start the body of the document (end the preamble) and \end{document} will end the document.
VOILÀ http://www.ctan.org/tex-archive/support/de-macro
This is a Python that:
[...] will expand macros defined in (re)newcommand or (re)newenvironment commands, within the document, or in the document’s “private” package file.
Never seen this done, but 2 half-baked ideas:
If the reason why you want to expand all these macros inline is for debugging, then setting \tracingmacros=1
in your document will expand all your macros, but the output goes to a log file.
The CTAN archive provides a package that you can use to inline expansions within definitions (but not newcommand), but I didn't know if you might take a look and see how painful it might be to modify to perform inline expansions of \newcommand instead of \def.
Consider using a template engine such as Jinja2 with Python.
You may wish to change the syntax from the default {%, {{, etc. in order to make it more compatible with LaTeX's own. For example:
env = jinja2.Environment(
loader=jinja2.FileSystemLoader( JINJA_DIRS ),
comment_start_string='["', # don't conflict with e.g. {#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 functions that are passed to the template's environment, Jinja2 supports macros. For example, your above code should work as expected as:
[% 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 this requires a bit of work, but isn't an insurmountable problem at all if you're familiar with Python.
I hope that helps.
I wrote a C program back in 2007 to expand \newcommand: http://www.gtoal.com/src/newcommand/ - I guess it wasn't indexed at the time this question was posted. Mentioning it now for anyone still looking for such a thing and finding 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With