Open uliw opened 1 year ago
this will parse the latex headers. Writing to a separate file would be cleaner but works in a pinch.
def read_header(lines, ofl) -> tuple([str, bool]):
"""header lines are all lines between \documentclass and \begin{document}
precede all read lines with #+latexheader:
parameters:
lines: io_wrapper
ofl: iowrapper
return values:
header: bool value
"""
nl: str = next(lines)
header: bool = True
if "documentclass" in nl:
header = False
while "begin{document}" not in nl:
if nl.strip() != "":
ofl.write(f"#+latexheader: {nl}")
nl: str = next(lines)
return nl, header
call just before you read the next line
if header:
header = read_header(lines, ofl)
line = next(lines)
and here is a snippet that deals with tables etc:
ef special_environments(nl, lines, ofl):
"""Some environments a better handled by a latex export block
parameters:
nl: current line
lines: io_wrapper
ofl: iowrapper
"""
se: list = ["table", "tabular", "tabularx", "longtable"]
for e in se:
if f"begin{{{e}}}" in nl:
ofl.write("#+BEGIN_EXPORT latex\n")
while f"end{{{e}}}" not in nl:
if nl.strip() != "":
ofl.write(nl)
nl: str = next(lines)
ofl.write("#+END_EXPORT\n")
break
call as:
line = next(lines)
special_environments(line, lines, ofl)
line = line_latex_to_orgmode(line)
Hi @uliw,
Thank you for your feedback. I must admit I haven't spent a great deal of time on these - rather hacked something together until my needs were satisfied for the (sometimes single) case at hand. However, that doesn't mean I'm not open to improvements :-)
Is it much work to post these updates as Pull requests?
will do later. Do you recall why you match section commands, including the newline character? The following will remove the newline at the end of the section command
line = re.sub(r"^ *\\section[* ]*\{([^}]*)\} *\n", r"\n* \1", line)
whereas this
line = re.sub(r"^ *\\section[* ]*\{([^}]*)\}", r"\n* \1", line)
results in the correct translation.
Not much of the original code survived, and my purpose (round-trip editing) differs from yours. I made my own version. Feel free to link to https://github.com/uliw/l2org
This looks pretty great!
running it on typical file I noticed the following
This could be extended to any non-standard environment
\cite[e.g.,][]{hansen2012}
). Would it be possible that any latex command that does not parse defaults to a newline with#+latex: foo....
Would it be possible that rather than parsing all the pre-amble commands to simply collect them in a l_headers.tex file, and include this with
#+latex_header: \input{l_headers.tex}
?