[Tex/LaTex] How to use LyX to create PDF file from the command line

lyxpdfxetex

I'm using LyX for technical documentation and want to automate the process of creating PDF files.

Within LyX, I can simply click the button "View PDF (XeTeX)", and LyX performs the whole build process for me: it creates a .tex file, does some bibliography magic, and uses XeTeX to create the final PDF file.

What I'm looking for is a way to call LyX from the command line and get the same result. So far, I've only managed to generate a plain .tex file (using --export-to xetex). However, when I run xelatex on the generated file, it keeps showing errors that don't occur when building using LyX. Is there a way to get LyX to do the work?

My system: Windows 7 64bit, LyX 2.1.0

Best Answer

The command sequence for exporting .lyx files to pdf/dvi (etc) from command prompt is

lyx --export FORMAT myfile.lyx

where FORMAT is the short identifier of the format you want to export, e.g., "dvi" for DVI files or "pdf2" to export PDF via pdflatex. You may see the File Formats pane in LyX's preferences dialog for a list of the available formats.

For example, to convert .lyx file to pdf using xelatex, the command will be

lyx --export pdf4 myfile.lyx

On the other hand, you may prefer to use a .bat file so that you can put the .bat file in the same directory as your .lyx file and simply double click on it.

@ECHO ON
cls

REM export lyx files to pdf using xelatex

CD /D %~dp0
SET Programa="lyx.exe"
for %%A in (*.lyx) do %Programa%  --export pdf4 %%A
Pause

Save the above as lyxbatch.bat (say), in the same folder as your .lyx file. Double clicking on this file will export all .lyx files in the current folder to pdf files using xelatex. You have to havelyx.exe` added to your system path.

Untested as I don't have lyx installed in this computer. If there are any hassles please let me know.

Related Question