# Converting LyX to LaTeX

I'm working with a colleague who uses LyX (I use LaTeX), and we have a hard time exchanging documents because of conversion issues. Is there any easy way to convert LyX to LaTeX and vice versa?

-
–  Daniel Oct 5 at 21:12

Exporting Lyx to Latex is easy, although it is quite possible to have documents that look fine in Lyx, but which correspond to Latex code that doesn't compile. This happens if you have odd Latex code in the preamble or in Tex code boxes, but these are obvious enough to the Lyx user, because you can't produce dvi/pdf from the document. — Addendum: Another possible problem: inclusion of local files; then the document might compile locally for the author, but not for the coauthor. But this can also be a problem for one user editing a Lyx file they receive from another user.

The other way is more likely to be problematic:

1. Parsing — When Lyx imports Latex code, it has to parse it to recognise the structure of the document: section headings, formulae, &c. If it doesn't manage to parse a part of the Latex source, it gobbles up the code until it does recognise something and puts the part not understood inside a Tex code box. These code boxes might contain structure Lyx should have spotted, or lead it to fail to recognise arguments passed to a macro which is very easy to do if you use, say, classes or packages that Lyx has not been programmed to digest (see, e.g., Can Lyx syntax files override document class defaults?). Unless you are doing something very strange, like changing catcodes, you should be able to edit text paragraphs in the usual way. The Tex code boxes could be a problem for your colleague;
2. Character encoding — Make sure you use ASCII or UTF8 as the character encoding in your Latex source;
3. Lyx preamble — Lyx puts some code in the document preamble. There is the possibility that this inclusion will clash with the imported Latex preamble.

I'd say the course of least resistance is to use Lyx yourself. You have a fair bit of freedom to mess about with Lyx's appearance using the preamble and Tex code boxes, and Lyx is pretty intuitive to a Latex user.

-

LyX has LaTeX import/export functions accessible in its menu. I don't have it installed right now, but from what I remember, you can't miss 'em.

-
I believe that's what my colleague uses, but he indicated that there was some minor issues with the export facility. But maybe I'm mistaken there. –  Suresh Sep 10 '10 at 5:34

I'm using the export feature a lot nowadays. I actually always build PDFs with a script calling pdfLaTeX and don't use the PDF export feature of LyX. You don't have to use the GUI to export from LyX to LaTeX. This does the same on the commandline (replace pdflatex with latex if you're not using pdfLaTeX):

`````` \$ lyx --force-overwrite --export pdflatex document.lyx
``````

There is also a commandline version for importing, but since your colleague is using LyX anyway the GUI is probably good enough.

-