There have been a handful of questions in this group about using Latex
& Leo
and I'd like to encourage more user development in this area.

So below I offer my script for creating a pdf from a latex file to
help new users get started.

I link the script to both a button and a shortcut key and I include a
step to open the result (a pdf file).   Suggestions are welcome.

Some program notes:

In the settings section I name the pdf creation program.  I use
'pdflatex' (that I think is part of MikTex) to process the tex file.
Other programs could be used, just need to change
       runstring = "%s  -output-directory=%s  %s " % (create_pdf,
odir, fn)

The main trick or value in my script is to find the filename.tex so I
know filename.pdf to open.
The script assumes I am working on a node in the Latex file tree and
the script backtracks to find the node that starts @file, @auto or
@nosent.

A more elegant version would make use of the file path in the @file
headline. I had to set a work directory (wdir) in the Settings section
because of problems with spaces in the path (on a Windows system).   I
also use an 'out' subdirectory to hold the tex log and the pdf, but
these files could be in the same work directory as the tex file (just
leave odir empty).

And the latex file is written using c.save() because I could not make
a node specific way work.

I am working (slowly) on a  more sophisticated version that would
-- determine if the node held a rst file and first turn this into
Latex
-- open the log file or read it into a node if there is a compiling
error
-- remove the os.chdir(wdir) step and solve the problem with path
spaces in the @file headline
-- find the rst or tex file node (assuming only one) from any current
position

It might be useful to have some settings in the file node that let the
script automatically
basic latex packages, items and definitions for a particular type of
document  (Title page, TOC, set date, use the beamer package to create
slides, etc)

And it would be really cool to be able to render TEX to PDF in real
time like viewrendered handles HTML and ReST.   That might be asking
too much

================================
Headline:
@button MakePDF  @key=Alt-m

Body:
import os

## Settings

## Work directory (wdir) is where the latex file is assumed to be
saved
##     Changing to wdir solves problems created by spaces in a Windows
OS path
##      and lets the  pdf->latex program use only filename.tex (no
path) as an argument
##  Output directory (odir)  should be a sub-directory of work
directory
##      and will hold the pdf file and tex log

wdir=  r'C:\Users\fn lname\Desktop\Latex\doc1'
odir= 'out'

create_pdf= 'pdflatex'
validnodes= ('@file', '@auto',  '@nosent' )

ready= False
open_pdf= True


## Set base directory
os.chdir(wdir)

## if odir does not exist, make it
try:
     os.mkdir(odir)
except:
    pass

## Find the current position and move backward on tree to find the
headline
## that names the tex file (in the node headline)
p1 = c.currentPosition()
h1= p1.h
hx=None
hx=  h1.split(' ')[0]
while not (hx in validnodes) and not (p1.level()==0):
    p1= p1.parent()
    h1= p1.h
    hx= h1.split(' ')[0]

##Prepare to process file, need path and filename
if (hx in validnodes):
    h1=p1.h
    h2= h1[len(hx):].strip()
    fn2=  os.path.split(h2)
    fdir= fn2[0]
    fn= fn2[-1]
    fn1= os.path.splitext(fn)
    ## Set .tex latex file with path
    fn2=os.path.join(wdir,fn)
    ## Set .pdf output file with path
    fpdf= os.path.join(wdir,odir,fn1[0])+'.pdf'
    c.save()  #used c.save() to re-rewrite node to file,
c.writeFileFromNode() did not work
    ready=True
else:
    ready=False
    g.es('*** Could not process node wiith latex file link')

## Create pdf from tex file
if ready:
    os.chdir(wdir)
    if odir:
        runstring = "%s  -output-directory=%s  %s " % (create_pdf,
odir, fn)
    else:
        runstring = "%s    %s" % (prog, fn)
    g.es('MakePDF using:  \n  %s' % runstring)
    os.system(runstring)

if ready and open_pdf:
    # note: pdf opened using the full path + filename.
    g.es('Opening PDF: %s' % fpdf)
    os.startfile(fpdf)

g.es('*** Done ***')

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to