@language python
import os
import subprocess
import sys
repository_dir = os.path.abspath(os.curdir)
# The system commands should be run from the folder containing the tex/cls/clo/bib files.
working_dir = os.path.join(repository_dir, 'myproject')
os.chdir(working_dir)
# The commands to run.
run_xelatex = 'xelatex ' + working_dir + os.sep + 'myproject.tex'
run_bibtex = 'bibtex ' + working_dir + os.sep + 'myproject'
g.es('Running XeLaTeX and BibTeX')
# os.system starts a new subshell
# @todo: is it possible to run the below commands in one subshell consecutively?
os.system(run_xelatex)
os.system(run_bibtex)
os.system(run_xelatex)
# Platform-independent file opening
def open_file(filename):
if sys.platform == "win32":
os.startfile(filename)
else:
opener ="xdg-open"
subprocess.call([opener, filename])
open_file('myproject.pdf')
I'm using Leo to organize sections like \chapter, \section and \subsection. Since I'm just writing latex in Leo, I need to keep track of the right hierarchies, so I can't freely move nodes around in the hierarchy or I end up with \subsection at the same level as \section, etc. It would be great to be able to let Leo handle this.
Arjan
I'm using Leo to organize sections like \chapter, \section and \subsection. Since I'm just writing latex in Leo, I need to keep track of the right hierarchies, so I can't freely move nodes around in the hierarchy or I end up with \subsection at the same level as \section, etc. It would be great to be able to let Leo handle this.
Hmm, not sure how that would work. It might be relatively easy to invoke such a script when setting up the node at first, but what happens if/when you decide to move the node and its level changes?
@language python
"""Changes LaTeX section definition levels in the subtree of an @clean file node to their subtree level.Only one LaTeX section level can therefore be used within a single node body."""
import re
section_levels = { 1: 'chapter', 2: 'section', 3: 'subsection', 4: 'subsubsection', 5: 'paragraph', 6: 'subparagraph'}
def latex_convert_section_levels(body, adjusted_level_name): """ Replaces LaTeX section definition levels found on a single line (re multiline mode). Returns the modified node body.""" newbody = re.sub(r'\\(chapter|section|subsection|subsubsection|paragraph|subparagraph)(\[.*?\])?({.*})', r'\\'+adjusted_level_name+r'\g<2>\g<3>', body, re.M) return(newbody)
if re.match(r'@clean [A-Za-z\./]+\.tex', p.h): root_level = p.level() for node in p.subtree(): level = node.level() - root_level if level < 7: level_name = section_levels[level] else: level_name = 'subparagraph' node.b = latex_convert_section_levels(node.b, level_name) g.es('Updated section levels based on outline structure.')else: g.es('not a LaTeX file node?')
Reviving this old topic. I made a crude attempt, hard-coded etc., to just rewrite the section definitions via regex search and replace...
How would I add Undo to this? Any other obvious improvements to make?
@language python
"""
Changes LaTeX section definition levels in the subtree of an @clean file node to their subtree level.
Only one LaTeX section level can therefore be used within a single node body.
"""
import re
section_levels = {
1: 'chapter',
2: 'section',
3: 'subsection',
4: 'subsubsection',
5: 'paragraph',
6: 'subparagraph'
}
def latex_convert_section_levels(p, adjusted_level_name):
""" Replaces LaTeX section definition levels found on a single line (re multiline mode).
Returns the modified node body."""
return re.sub(r'\\(chapter|section|subsection|subsubsection|paragraph|subparagraph)(\[.*?\])?({.*})',
r'\\'+adjusted_level_name+r'\g<2>\g<3>', p.b, re.M)
u, undoType = c.undoer, 'change-latex'
h = p.h.strip()
if g.match_word(h, 0, '@clean') and h.endswith('.tex'):
bunch = u.beforeChangeTree(c.p)
changed, dirtyVnodeList = 0, []
root_level = p.level()
for p in p.subtree():
level = p.level() - root_level
if level < 7:
level_name = section_levels[level]
else:
level_name = 'subparagraph'
s = latex_convert_section_levels(p, level_name)
if s != p.b:
bunch2 = u.beforeChangeNodeContents(p)
p.b = s
u.afterChangeNodeContents(p, undoType, bunch2, dirtyVnodeList=dirtyVnodeList)
p.v.setDirty()
changed += 1
if changed:
u.afterChangeTree(c.p, undoType, bunch)
g.es('Changed %s node%s.' % (changed, g.plural(changed)))
else:
g.es('No nodes changed')
else:
g.es('not a LaTeX file node?')
if g.match_word(h, 0, '@clean') and h.endswith('.tex'):
Thanks, Edward!
The above script used to work, but recently (after not having used it for a long time), I noticed it errors:exception executing scriptTypeError: afterChangeNodeContents() got an unexpected keyword argument 'dirtyVnodeList'