0

I am trying to export an org file to latex (to pdf via latex) and to make latex color the inline ~code~ parts.

According to the manual (see [1] "Defining filters for individual files") this should be possible with the following:

#+BIND: org-export-filter-code-functions (tmp-latex-code-filter)
#+BEGIN_SRC emacs-lisp :exports results :results none
  (defun tmp-latex-code-filter (text backend info)
    "red inline code"
    (when (org-export-derived-backend-p backend 'latex) 
        (format "{\color{red} %s }" text)))
#+END_SRC
#+latex_header: \usepackage{xcolor}

~this is a test~

However, it does not work at all and I cannot seem to pin down the problem.

I have tried to c/p the example from the manual (again see [1]) but this does not work either (it should remove the striked out text).

#+BIND: org-export-filter-strike-through-functions (tmp-f-strike-through)
#+BEGIN_SRC emacs-lisp :exports results :results none
  (defun tmp-f-strike-through (s backend info) "")
#+END_SRC

~this is a test~

+this is a test+

Does anyone have an idea of how to achieve coloring ~code~ when exporting to latex?

Thank you

Edit: Using the fixed example from your answer, the produced .tex file (minus personal information):

% Created 2020-06-10 Wed 00:27
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{xcolor}

\hypersetup{
 pdftitle={},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.1.9)}, 
 pdflang={English}}
\begin{document}

\tableofcontents

\texttt{this is a test}

\sout{this is a test}
\end{document}

It is interesting to me, that it simply works for you. I have tried reinstalling emacs. My emacs was at version 25 before, however, there was no change in the output.

I have also tried it with a clean init.el and just org mode enabled and it still does not work for me.

Edit2: Here the Emacs and Org mode version outputs

GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2019-09-16
Org mode version 9.1.9 (release_9.1.9-65-g5e4542 @ /usr/share/emacs/26.3/lisp/org/)

Edit3: That did the trick, Thank you very much. I added

# -*- org-export-allow-bind-keywords: t -*-

as first line in my org file (allowing the #+BIND keyword for this file only) and now it works perfectly.

[1] https://orgmode.org/manual/Advanced-Export-Configuration.html

  • Can you edit your question and add the .tex file that is produced? That should provide a fair idea of what is going on. – NickD Jun 9 at 19:43
  • Can you also add version information both for your emacs and your Org mode? C-u M-x emacs-version and C-u M-x org-version will provide that information and insert it into the current buffer, ready for cut-and-pasting it here. – NickD Jun 9 at 23:47
0

EDIT/ The #+BIND: keyword needs to be turned on as well:

(setq org-export-allow-bind-keywords t)

I had set that variable and forgotten about it, but the default is nil I think (primarily to protect you from yourself I guess).

The doc string for the variable says:

Non-nil means BIND keywords can define local variable values. This is a potential security risk, which is why the default value is nil. You can also allow them through local buffer variables.

The last sentence refers to the File local variables mechanism in Emacs. /EDIT

There is one bug in that backslashes in elisp strings have to be escaped:

  (defun tmp-latex-code-filter (text backend info)
    "red inline code"
    (when (org-export-derived-backend-p backend 'latex) 
        (format "{\\color{red} %s }" text))

but other than that, both of the examples work fine for me - when I export the following to PDF:

#+BIND: org-export-filter-strike-through-functions (tmp-f-strike-through)
#+BIND: org-export-filter-code-functions (tmp-latex-code-filter)
#+BEGIN_SRC emacs-lisp :exports results :results none
  (defun tmp-latex-code-filter (text backend info)
    "red inline code"
    (when (org-export-derived-backend-p backend 'latex) 
        (format "{\\color{red} %s }" text)))
#+END_SRC
#+latex_header: \usepackage{xcolor}
#+BEGIN_SRC emacs-lisp :exports results :results none
  (defun tmp-f-strike-through (s backend info) "")
#+END_SRC

~this is a test~

+this is a test+

I get

cropped PDF output with red text only

and the (slightly sanitized) tex file looks like this:

% Created 2020-06-09 Tue 12:54
% Intended LaTeX compiler: pdflatex
\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{xcolor}
\date{\today}
\title{}

\begin{document}

{\color{red} \texttt{this is a test} }
\end{document}
| improve this answer | |
  • I edited the answer to add info about the org-export-allow-bind-keywords variable (which I had forgotten about). – NickD Jun 11 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.