MarcvdSluys / Orgmode-convert

Scripts to convert information from and to Emacs Orgmode format.
European Union Public License 1.2
0 stars 0 forks source link

Some observations #2

Open uliw opened 1 year ago

uliw commented 1 year ago

This looks pretty great!

running it on typical file I noticed the following

  1. section titles sometimes miss a newline, so that the whole paragraph is rendered as a heading
  2. complex tables fail to convert but produce no warning. Would it be possible to simply wrap them in a latex block, rather than converting them to an org-table? I.e.,
    #+BEGIN_EXPORT latex
    ....
    .....
    #+END_EXPORT

    This could be extended to any non-standard environment

  3. complex cite commands do not convert (e.g., \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}?

uliw commented 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)
uliw commented 1 year ago

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)
MarcvdSluys commented 1 year ago

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?

uliw commented 1 year ago

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.

uliw commented 1 year ago

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