On Mon, Nov 1, 2010 at 8:56 PM, Vincent van Ravesteijn <v...@lyx.org> wrote: > A slightly simpler problem is to allow users to modify the generated > preamble. Sort of the same problem, but simpler and useful as well.
I use the attached lyxpp.py. To use 0) Put lyxpp.py in the path. 1) Set the LaTeX (pdflatex) -> PDF (pdflatex) converter (in Preferences->Converters) to be something like: lyxpp.py pdflatex $$i 1b) If you use postscript/dvi also set LaTeX (plain) -> DVI to be lyxpp.py latex $$i 2) Cut-and-paste the generated preamble into a ERT at the top of the document (To get the generated preamble you may want to use View->Source) 3) Prefix every line of the preamble with %PREAM 4) Edit the preamble as required. -- John C. McCabe-Dansted
#!/usr/bin/env python import sys import os import subprocess import re print "PWD: ", os.system("pwd") if len(sys.argv)<2: print 'Usage: '+sys.argv[0]+' COMMAND PARAMS' print """ Pre-processes the first tex file (parameter not starting with '-') and replaces the preamble with the set of lines starting with: %PREAM If no such lines exist it will not modify the file. It will then run COMMAND with PARAMS It is recommended that you put the %PREAM lines near the top of your tex file so that the line numbers of errors are meaningful This program is useful when you want to have precise control over the preamble of a tex file within LyX. I copy the LyX generated preamble into an ERT box, prefix all the lines with "%PREAM " and then remove the lines I don\'t need I configure LyX to use this script by going to Tools->Preferences->LyX. In both "latex(plain) -> DVI" and "latex(pdflatex) -> DVI" prefix latex and pdflatex with """+sys.argv[0]+""" finally click Modify and Save.""" os._exit(1) else: print len(sys.argv) print 'Will run COMMAND: '+sys.argv[1] #testfile=open('/tmp/t','w') #for p in sys.argv: # sys.stderr.write(p + ' ') # testfile.write(p + ' ') #testfile.write('\n') def swap (file1,file2): s=file1+'._swptmp' os.rename(file1,s) os.rename(file2,file1) os.rename(s,file2) for f in sys.argv[2:]: if not f.startswith('-'): infname=f os.putenv('TEX_NAME',infname) infile=open(infname,'r') outfname=infname+'._alt' outfile=open(outfname,'w') lines=infile.readlines() pream=[] i=0 indocument=False while i < len(lines): L=lines[i] if L.startswith('%PREAM '): L=L[7:] del lines[i] pream.append(L) else: if L.startswith(r'\begin{document}'): indocument=True if not indocument: lines[i]='\n' i=i+1 if len(pream) > 0: #file has custom preamble outfile.writelines(pream) outfile.writelines(lines) outfile.close() swap(infname,outfname) retcode=subprocess.call(sys.argv[1:]) swap(infname,outfname) else: #file does not have custom preamble... leave it alone. retcode=subprocess.call(sys.argv[1:]) os._exit(retcode) # retcodes can be negative, so this isn't quite what we want... but close enough.