GeneralNewcommand expand preprocessor

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
joe314159
Posts: 1
Joined: Sun May 03, 2009 9:52 am

Newcommand expand preprocessor

Post by joe314159 »

Hi all,

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

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
internic
Posts: 3
Joined: Thu May 27, 2010 3:30 am

Re: Newcommand expand preprocessor

Post by internic »

I have the same question as the parent poster. It would be very useful to have a program that would go through latex source expanding new commands. Does anyone know of a way to do this? It seems this is not something that can be accomplished with a simple sed command, but I'm guessing there must be some way.

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.
tmac
Posts: 2
Joined: Tue Aug 31, 2010 9:17 pm

Re: Newcommand expand preprocessor

Post by tmac »

I think I have found a way to accomplish this. My goal was to import Latex into Lyx. This is of course possible even with newcommand definitions and use thereof in the rest of the document, but I wanted lyx to be able to interpret/render all the latex code properly.

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
Post Reply