Blob


1 I just recalled how cool macros are. I was helping to convert a manuscript for a book from LibreOffice to LaTeX, and to speed the conversion we used pandoc. The problem was that pandoc added a lot of noise to the generated code. Take for instance this bit:
3 ```
4 \hypertarget{foo-bar-baz}{%
5 \subsubsection[foo bar baz]{\texorpdfstring{\protect\hypertarget{anchor-92}{}{}Foo Bar Baz}{Foo Bar Baz}}\label{foo-bar-baz}}
6 ```
8 that needs to be converted to like
10 ```
11 \section{Foo Bar Baz}
12 ```
14 i.e. subsection → section and remove some crufts. If there were only a handful of those, I could have done it by hand, but given that were around 700 instance of those, it was unfeasible.
16 My first idea was to fire up sam and play with it a bit. Unfortunately, it’s been a while since I’ve used it extensively, so I’m a bit rusty. The plan was to use the command x to select every paragraph, then g to filter that type of paragraphs and then something with s, but I failed.
18 Given that I didn’t want to spend too much time on this, I tried to use an Emacs macro. Oh boy, it went so well.
20 The macro goes along the lines of
21 ```
22 C-s ;; isearch-forward
23 \ ;; self-insert-command
24 section ;; self-insert-command * 7
25 RET ;; newline
26 C-a ;; move-beginning-of-line
27 2*C-M-SPC ;; mark-sexp
28 C-w ;; kill-region
29 C-p ;; previous-line
30 2*C-o ;; open-line
31 C-y ;; yank
32 C-n ;; next-line
33 M-h ;; mark-paragraph
34 C-w ;; kill-region
35 ```
37 it goes to the next \section, select the LaTeX command and the text within the square brackets (using mark-sexp two times), move that bit before and then killing the paragraph.
39 Then ‘C-- C-x e’ and the whole file was fixed.
41 The bottom line is, I guess, use your editor and learn it, or something along those lines.