3

Context I am embedding org-mode source code blocks and their results into my document and am exporting it to LaTeX. I usually wrap the output of my code into helper functions that generate formatted LaTeX code, for example equation environments or TikZ graphics.

Question Unfortunately I stumbled over blank lines after the results block leading to new paragraphs in the LaTeX document. Do you see a reasonin org-mode for this behauvior?

Related On StackOverflow there is a question regarding newline characters at the end of each python print invocation but this does not resolve the issue:

StackOverflow: Print to the same line and not a new line in python

Modifying the end paramter of print does only insert abc before the newline but is not replacing it.

print('\\begin{equation}c+d\\end{equation}', end='abc'),

Minimum working example:

#+TITLE: Blank lines after source block results on export
#+LaTeX_HEADER: \usepackage{lsthighlight}

* Example
In this example I would like to show that exporting a paragraph with
the embedded two source blocks
#+BEGIN_SRC python :exports results :results output latex
print('\\begin{equation}a+b\\end{equation}')
#+END_SRC
and
#+BEGIN_SRC python :exports results :results output latex
print('\\begin{equation}c+d\\end{equation}')
#+END_SRC
will result in blank lines after the result listing in the exported
LaTeX document.

LaTeX export:

\section{Example}
In this example I would like to show that exporting a paragraph with
the embedded two source blocks
\begin{equation}a+b\end{equation}

and
\begin{equation}c+d\end{equation}

will result in blank lines after the result listing in the exported
\LaTeX{} document.

Exported LaTeX document

Instead it should be exported as follows:

\section{Example}
In this example I would like to show that exporting a paragraph with
the embedded two source blocks
\begin{equation}a+b\end{equation}
and
\begin{equation}c+d\end{equation}
will result in blank lines after the result listing in the exported
\LaTeX{} document.

Expected LaTeX document

I really have no idea where to look for this issue. That's why I have the hope that you can help me with this. Thank you!

3
  • Your MWE produced different results for me via org-latex-export-as-latex, do you have a variable set somewhere that asserts that equations must be exported? They are simply ignored at export time on my system. Jun 8, 2015 at 22:18
  • 1
    OK, I can reproduce the behavior you described if I evaluate: (org-babel-do-load-languages 'org-babel-load-languages '((python . t))) (setq org-confirm-babel-evaluate nil). Jun 8, 2015 at 23:05
  • 1
    It doesn't seem to be specific to python, I can reproduce the same behavior with an emacs-lisp :exports results :results value latex block containing (print "foo"). Jun 8, 2015 at 23:46

1 Answer 1

4

After looking under the hood of org-mode, here's what I'd suggest.

Comment out Line 1987 in the current/latest version ob-core.el, evaluate, test, and if you like what you see, don't forget to byte compile.

If you are using a different version of Org, the line is part of the function org-babel-where-is-src-block-result, and it reads as follows:

(unless beg (insert "\n") (backward-char))

If you want a cleaner solution, there is probably another function you could advise. But I'll leave you to find that; now you know where the newline comes from.

method

Maybe useful for you to know how I went about it. I dug into the export routines, starting with org-latex-export-as-latex, until I found the ob-core.el file. There are lots of newlines introduced there. To pinpoint which was responsible, I replaced each "\n" with some "N\n" (where N is a counting number) then reran the export.

2
  • Oh my god, thank you very much! Replacing ´"\n"´ by ´""´ resolves the issue! :) :)
    – Christoph
    Jun 9, 2015 at 9:16
  • I will try to improve my debugging capabilities, such that I can find it myself next time. Thank you again :)
    – Christoph
    Jun 9, 2015 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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