1
votes

What’s the best way to let org-mode in Emacs add a strike-through on every heading with a “DONE” keyword when I export my document to LaTeX? Right now my settings are

(defun my-latex-filter-headline-done (text backend info)
  "Ensure dots in headlines."
  (when (org-export-derived-backend-p backend 'latex)
    (save-match-data
      (when (let ((case-fold-search t))
              (string-match "\\\\\\([a-z]+\\){\\(.*DONE.*\\)}"
                            text))
        (if (not (string-match ".*hsout.*" text))
            (replace-match "\\\\\\1{\\\\hsout{\\2}}"
                       t nil text))))))

(eval-after-load 'ox
  '(progn
     (add-to-list 'org-export-filter-headline-functions
                  'my-latex-filter-headline-done)))

And in my document I have

#+LATEX_HEADER: \DeclareRobustCommand{\hsout}[1]{\texorpdfstring{\sout{#1}}{#1}}

So that if I have an org document like

* DONE test

It will export to something like

% header stuff…
\begin{document}
\section{\hsout{{\bfseries\sffamily DONE} test}}
\end{document}

while normally without the strike-through it’s like

% header stuff…
\begin{document}
\section{{\bfseries\sffamily DONE} test}
\end{document}

I have to use the \hsout macro to make strike-through work in headings. This approach has several faults:

  • It depends on the way org-mode generates the LaTeX code. If org-mode changes the way it does that, this approach may break.
  • I have to add the LATEX_HEADER in all my documents.
  • Some headings have a short form (\section[aaaa]{bbbbbbb}). If I want to include that, I have to add another ugly regexp to my elisp, which is already quite ugly.

Hence my question at the beginning. Anyone knows a better way?

1
This is a very interesting question. Would it be possible for you to please add a LaTeX code example of what you would like the export to look like as a finished product inside a LaTeX document so that readers of this thread have a clear picture of the before and desired after? In my mind, I am envisioning the export potentially calling up a LaTeX template and inserting the export after \begin{document}.lawlist
Hi @lawlist, I add some examples as you described. As to the template, I couldn’t find anyway to change it. I know I can change the template for previewing latex fragment, but not sure for the exported document.MetroWind
It looks like ox-latex.el has a specific template that is hard-coded. If you like the way that ox-latex.el converts org headings into a \section definition (e.g., at line 190 as part of org-latex-classes), then you are probably limited to the type of formatting that is permitted within a definition of that nature. Other than modifying the source, another option would be to have a cleanup-function that runs after ox-latex.el finishes compiling -- e.g., replace-regexp . . ..lawlist

1 Answers

0
votes

Here is a partial . . . this will save you the entry in each org-file for the LATEX-HEADER:

(setq org-latex-default-packages-alist
  '(("AUTO" "inputenc"  t)
    ("T1"   "fontenc"   t)
    (""     "fixltx2e"  nil)
    (""     "graphicx"  t)
    (""     "longtable" nil)
    (""     "float"     nil)
    (""     "wrapfig"   nil)
    (""     "rotating"  nil)
    ("normalem" "ulem"  t)
    (""     "amsmath"   t)
    (""     "textcomp"  t)
    (""     "marvosym"  t)
    (""     "wasysym"   t)
    (""     "amssymb"   t)
    (""     "hyperref"  nil)
    "\\tolerance=1000"
    "\\DeclareRobustCommand{\\hsout}[1]{\\texorpdfstring{\\sout{#1}}{#1}}"
  ))

Another option is to use:

(setq org-latex-packages-alist
  '(
    "\\DeclareRobustCommand{\\hsout}[1]{\\texorpdfstring{\\sout{#1}}{#1}}"
  ))

Additional methods of adding to the alist:

(add-to-list 'org-latex-packages-alist
  "\\DeclareRobustCommand{\\hsout}[1]{\\texorpdfstring{\\sout{#1}}{#1}}")

or

(add-to-list 'org-latex-default-packages-alist
  "\\DeclareRobustCommand{\\hsout}[1]{\\texorpdfstring{\\sout{#1}}{#1}}")