Search:   Help



LyX documentation


Shared groups



Categories: LaTeX
<< | Page list | >>

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

Some LaTeX files make use of user-defined \newcommands in the preamble. When importing such a file into LyX, all instances of each \newcommand are represented within LyX as a grey ERT box, and its contents is not interpreted and rendered by LyX.

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:

# _*_ 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')

As an example, it will convert this:

\int f(x) dx = 0

into this:

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

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


Install first plastex
Download the plastex package from the sourceforge project website:
extract the files
and install plastex on your local mac in the terminal with the command:
sudo python install


Category: LaTeX

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2010-09-06 14:43 UTC