reddit / emacs

Customize how Org exports links to Latex

AuroraDraco · 2022-02-15 18:36
So, to understand my problem, let me explain my situation. I am Greek and naturally a lot of my files are in greek. To make org export to a latex pdf properly, if it has greek characters, you need to add some packages to it. I have those in my org-latex-packages-alist and everything exports just fine. However, when we get to exporting org-roam files (or any file that contains file links for that matter), there is a problem. The org latex export backend typically exports a link like \\href{file\_name}{description}. If the file name is in greek (which odds are it is), latex starts to freak out a bit. Org will export perfectly to latex, but latex won't export to pdf. To my understanding, it doesn't understand the greek characters in the file name (which it should as it understands them everywhere else) and it chooses not to export. After a lot of searching, I did manage to find a solution to this in [this](https://tex.stackexchange.com/questions/131171/unicode-symbols-in-hyperref-links) post. Basically, if the link is of the form \\href{\\detokenize{file\_name}}{description}, it exports perfectly. From what I understand, that command just makes it recognize any unicode character for file names. So my question is, can you configure the org latex export backend to export links in the form \\href{\\detokenize{file\_name}}{description} and not \\href{file\_name}{description} as it does by default. I couldn't find any option that changes how hyperref makes its links in the hyperref manual, nor could I find how org exports its links to latex in the org-export manual, so if anyone here knows, it would help me greatly. Thanks in advance!
h-m-b-tu · 2022-02-15 18:40
Look at `org-export-filter-link-functions`.
AuroraDraco · 2022-02-15 19:44
This is definitely it. But how can I replace the string \\href{} with \\href{\\detokenize{}}, while keeping the contents of the href in tact. I am thinking of doing something with replace-regexp-in-string, but I don't know how to do the replacement, putting \\detokenize in the correct position and without losing the contents. Any idea how this could be done? EDIT: Looks like I figured it out with this `(defun latex-filter-greek-hrefs (link backend info)` `"Change \\href to \\href{\detokenize} so that latex exports` `don't break if they see a file with greek letters"` `(when (org-export-derived-backend-p backend 'latex)` `(let* ((bracket (search "\}" link))` `(size (length link))` `(name` `(concat (substring link 0 6)` `"\\detokenize\{"` `(substring link 6 bracket)` `"\}"` `(substring link bracket size))))` `(format "%s" name))))`
cazzipropri · 2022-02-15 20:42
I'd also ask Protesilaos Stavrou [https://protesilaos.com/contact/](https://protesilaos.com/contact/) Given his focus on org, I bet he knows the best way.
h-m-b-tu · 2022-02-16 07:46
Try something like `(replace-regexp-in-string "\\\\href{\\([^}]*\\)}" "\\\\href{\\\\detokenize{\\1}}" link t)`. This makes use of capture groups (between `\\(` and `\\)`) which can be included in the replacement text (via `\\1`).
AuroraDraco · 2022-02-16 13:41
Thanks, will check this out