General ⇒ Newcommand expand preprocessor
Newcommand expand preprocessor
how many times did it happen to any of you guys to copy-and-paste some document fragment from one LaTeX source to another, and then having to manually editing most macros because they were incompatible with the destination source file? This happens often to me, and sometimes it requires so much intensive editing that, at the end of the day, I realize that just rewriting the text from scratch would have been faster than a brutal copy and paste.
We would save hours, if only we had all \newcommand definitions expanded in the LaTeX source before doing the copy-and-paste into another source file.
So, here are are questions:
Q1) does there exist any "fairly-simple-to-use" preprocessing utility that expands \newcommands in LaTeX source files?
Q2) is this the best suited forum where to post this topic? If not, where do you think I should post Question 1?
Thank you all for reading!
Thanks in advance for giving some hint!!
Josef
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
Re: Newcommand expand preprocessor
As the parent said, one use for this sort of capability is sharing snippets of tex source for use in others' documents, but it would also be useful for using snippets of code in other places that accept latex but may not allow one to define new commands, like latex facilities in wikis, instant messaging clients, Google wave, etc. I'm hoping that in the year since this thread was initiated someone will have come along with a good suggestion on this topic.
Re: Newcommand expand preprocessor
I found that a python package called plastex can expand all the instances of the \newcommand definitions. Here is a link to the thread on the LyX mailing list for more details:
http://www.mail-archive.com/lyx-users@l ... 82576.html
Essentially, this python script will do it:
--------------------------------------------
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
import sys
from plasTeX.TeX import TeX
doc = TeX(file=sys.argv[1]).parse()
# The processed document is contained in the string doc.source
# Print to file
f = open('PlastexProcessed.tex', 'w')
f.write(doc.source.encode("utf-8"))
f.close()
--------------------------------------------
It wants your *.tex filename as its only argument. It will write a file called PlastexProcessed.tex which doesn't use any of the user-defined commands in the text, although the \newcommand's are still present in the document. They can be removed. The resulting *.tex might not be beautifully formatted, though...
Ref:
http://wiki.lyx.org/LaTeX/ExpandNewcommands
http://wiki.lyx.org/LaTeX/LaTeX
Best regards
Torquil Sørensen