Go to page:

Search:   Help

Edit

ExpandNewcommands

Categories: LaTeX
<< | Page list | >>

Expand newcommands into native LaTeX code that LyX can interpret on LaTeX import.


However, sometimes it is desireable to have LyX render all LaTeX content. Preprocessing of the LaTeX file to expand all such instances of user-defined commands is then necessary since this is not done by the native LyX import code.

This can be done through a simple python script using the python package plasTeX. The LaTeX code formatting is also changed by plasTeX but for LyX import purposes this is irrelevant. The following python script expects the filename of the LaTeX document as a command line argument, and will output a file PlastexProcessed.tex in which all instances of \newcommands are expanded to native LaTeX code throughout the document:

#!/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()

As an example, it will convert this:

\documentclass{article}
\newcommand{\be}{}
\newcommand{\ee}{}
\begin{document}
\be
\int f(x) dx = 0
\ee
\end{document}

into this:

\documentclass{article}
\begin{document} $$\int f(x) dx = 0$$
\end{document}

If desired, the second line containing the \newcommands can now be deleted. Note that the PlasTeX processing might cause other minor unwanted changes in the LaTeX code, so be sure to check the resulting document carefully. For more info, check the PlasTeX documentation.

Torquil Macdonald Sørensen

Requirements

Install first plastex
http://sourceforge.net/projects/plastex/files/
extract the files
and install plastex on your local mac in the terminal with the command:
sudo python setup.py install

Category: LaTeX