Search:   Help

Navigation

Groups

LyX documentation

Edit

Shared groups

Links

ExpandNewcommands

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:

#!/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}{\begin{equation}}
\newcommand{\ee}{\end{equation}}
\begin{document}
\be
\int f(x) dx = 0
\ee
\end{document}

into this:

\documentclass{article}
\newcommand{\be }{\begin {equation}} \newcommand{\ee }{\end {equation}}
\begin{document} \begin{equation} \int f(x) dx = 0 \end{equation}
\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
Download the plastex package from the sourceforge project website:
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

Categories

Category: LaTeX

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