3

Latex has the ability to include the contents of a file using the lstinputlisting command. When exporting an org-mode file to latex #+include: somefile inserts the file contents at that point in the document, this makes sense for org files, but for many files it would make more sense to use the listing command when exporting to latex. Is there a way to do this?

Edit: Here's some examples of what the org code and respective latex code would be. One file is a plain text file, the other is marked as a c file so that it gets syntax highlighting.

In the .org file:

#+INCLUDE: results.txt
#+INCLUDE: main.cpp src C++

In the .tex file:

\lstinputlisting[]{results.txt}
\lstinputlisting[language=c]{main.cpp}

The main advantage with using the lstinputlisting over copy/pasting the file contents into the latex document is when the exported document will be edited further. The listing makes it more readable and allows easily incorporating changes made to the included files.

Edit: I've looked a little bit at exporter's source code, and I'm not sure this is possible at the moment.

  • @Ista, The manual doesn't seem to have anything about changing the way includes are exported. – user2699 Mar 6 '15 at 15:31
  • Could you exemplify the expected output for those of us not familiar with the listing commands? – Malabarba Mar 6 '15 at 16:39
  • I think at the org level you should be linking to the external file rather than including it. With a custom link format, you can then generate the appropriate export code. – Andrew Swann Mar 7 '15 at 9:55
  • @AndrewSwann, thanks, I'll look into that. – user2699 Mar 7 '15 at 18:04
1

Try the next in your org file

#+LaTeX_HEADER: \usepackage[parameters]{listings}

#+begin_latex
\lstinputlisting[]{results.txt}
\lstinputlisting[language=c]{main.cpp}
#+end_latex

References: http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-LaTeX.html

0

You need to a) include the src language key in the #+include statement (as you already did) and b) tell org to export literal examples using listings, as described in C-h v org-latex-listings. The example there suggests

  (setq org-latex-listings t)
  (require 'ox-latex)
  (add-to-list 'org-latex-packages-alist '("" "listings"))
  (add-to-list 'org-latex-packages-alist '("" "color"))

Edit This should give the desired result if you export to pdf, but it won't use lstinputlisting in the .tex file.

  • This adds highlighting, which is good for source files, but it doesn't change how the include files are actually handled. – user2699 Mar 6 '15 at 20:31
  • Yes, the original question wasn't clear (or I miss-read it). – Ista Mar 6 '15 at 21:53
0

Using the link filter on org export, this inserts the linked files contents (for .c files) as a src block. At the moment, it only seems to work when exporting to text. If anyone knows how to fix that, please let me know.

(setq src-block-string
"#+begin_src
%c
#+end_src")
(defun org-export-link-to-block (text backend info)
  "Translate code files int src blocks."
  (when (string-match "\\[file:\\(.+\\.c\\)\\]" text)
    (let* ((file (match-string 1 text))
            (buffer (find-file-noselect file)))
       (set-buffer buffer)
       (format-spec src-block-string
                    (format-spec-make
                     ?c (buffer-string)))
       )))
(add-to-list 'org-export-filter-link-functions
             'org-export-link-to-block)

I started creating a custom link type as well, but this seems like a much simpler solution.

Update: This implements it as a filter before any changes are made, which allows it in more modes, or even to pre-process a file.

(setq src-block-string
      "
#+begin_src %m
%c
#+end_src
")
(setq link-to-src-extensions '("h" "hpp" "c" "cc" "py"))
(defun org-links-to-src-block (backend)
  "Translate code files int src blocks."
  (let ((pos 0)
        (prev-buffer (current-buffer))
        (extensions (concat "\\[\\[file:\\(.+\\.\\("
                            (mapconcat 'identity link-to-src-extensions "\\|")
                            "\\)\\)\\]\\]")))
    (message extensions)
    (while (string-match extensions (buffer-string) pos)
      (setq pos (match-end 0))
      (let* ( (start (match-beginning 0))
              (end (match-end 0))
              (mode (match-string 2 (buffer-string)))
              (file (match-string 1 (buffer-string)))
             (buffer (find-file-noselect file)))
        (delete-region start (+ end 1 ))
        (set-buffer buffer)
        (let ((block (format-spec src-block-string
                     (format-spec-make
                      ?c (buffer-string)
                      ?m mode))))
          (set-buffer prev-buffer)
          (goto-char start)
          (insert block)
          )))))

(add-to-list 'org-export-before-parsing-hook 'org-links-to-src-block)

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.