[Not a complete answer: in the first part, I show the TeX file for the preview and identify the problematic component of the pipeline, but I don't know why that component misbehaves. In the second part, I describe a couple of debugging methods for Org mode LaTeX preview problems that I've used in the past - and used here to get the TeX file in the first part of the "answer"].
The TeX File
The problem here is that in the imagemagick
pipeline (which indeed works better than the dvipng
pipeline, which does not work at all with the witharrows
package), the fragment is turned into a LaTeX
file that is then processed through pdflatex
to a PDF
file and that in turn is processed through convert
(part of the ImageMagick suite of programs) to produce a PNG
file.
The LaTeX
file looks like this in its entirety - you should be able to cut-and-paste it into your own file, process it through pdflatex
and verify that the resulting PDF
file looks correct:
\documentclass{article}
\usepackage[usenames]{color}
% Package minted omitted
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
% Package longtable omitted
% Package wrapfig omitted
% Package rotating omitted
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{amssymb}
% Package capt-of omitted
% Package hyperref omitted
\pagestyle{empty} % do not remove
% The settings below are copied from fullpage.sty
\setlength{\textwidth}{\paperwidth}
\addtolength{\textwidth}{-3cm}
\setlength{\oddsidemargin}{1.5cm}
\addtolength{\oddsidemargin}{-2.54cm}
\setlength{\evensidemargin}{\oddsidemargin}
\setlength{\textheight}{\paperheight}
\addtolength{\textheight}{-\headheight}
\addtolength{\textheight}{-\headsep}
\addtolength{\textheight}{-\footskip}
\addtolength{\textheight}{-3cm}
\setlength{\topmargin}{1.5cm}
\addtolength{\topmargin}{-2.54cm}
\usepackage{witharrows}
\begin{document}
\definecolor{fg}{rgb}{0,1,0}%
\definecolor{bg}{rgb}{0,0,0}%
\pagecolor{bg}%
{\color{fg}
\begin{equation*}
\begin{WithArrows}
a\cdot{}x^{2} + b\cdot{}x + c &= 0 \Arrow[xoffset=1cm]{Test} \\
\frac{1}{2}
\end{WithArrows}
\end{equation*}
%
}
\end{document}
Processing this through pdflatex
produces a PDF
file that looks correct, with the arrow pointing from the first line to the fraction. But for some reason, convert
mucks up the PNG
output the way you've shown. Why that is remains a mystery, but I guess it's either a bug in convert
or an incompatibility with the PDF
that is produced for the arrow. EDIT: This is almost certainly wrong. See the OP's answer for a solution. It seems that pdflatex
is indeed to blame.
Debugging Techniques
Diagnosing problems in org-latex-preview
is often an exercise in frustration. The problem is that the underlying org-create-formula-image
takes the LaTeX fragment as a string argument and then internally it writes a .tex
file containing the LaTeX header and the fragment (with enough additional stuff to make it into a legal LaTeX file), it processes the file (to either dvi
or pdf
depending on the value of org-preview-latex-process
) and then converts that file to a PNG
image (using either dvipng
or convert
- the imagemagick
method goes the pdflatex/convert
route). The function then copies the output file to the ltximg/
subdirectory of wherever the Org mode file is and then cleans up everything, leaving no trace of what it did and how it did it.
I have used one of two methods to debug such problems:
- I instrument
org-create-formula-image
with edebug and single-step through the function almost all the way to the end to just before the loop that does the clean up. That code looks like this:
...
(copy-file image-output-file tofile 'replace)
(dolist (e post-clean)
(when (file-exists-p (concat texfilebase e))
(delete-file (concat texfilebase e))))
image-output-file)))
You want to stop just before this point. Then I examine various things with the e
command. In particular, the variable texfile
contains the path of the .tex
file that is used to generate the .pdf
file and thence the .png
file. In most cases, I just go to a terminal and copy that file to a safe place and then process it through the rest of the pipeline (pdflatex
and convert
in the imagemagick
case.
- Another method I have used is to copy the source code of the function
org-create-formula-image
, modify it and then load the modified version into Emacs. For example, I replace the few lines shown above with the following:
...
(copy-file image-output-file tofile 'replace)
(print latex-compiler t)
(message texfile)
(print image-converter t)
(message image-input-file)
(message image-output-type)
;; (dolist (e post-clean)
;; (when (file-exists-p (concat texfilebase e))
;; (delete-file (concat texfilebase e))))
image-output-file)))
where I've commented out the clean-up loop and added some message
and print
calls to print out interesting stuff. I then evaluate the defun with C-x C-e
after the closing paren and do C-c C-x C-l
to do the LaTeX preview in the Org mode buffer. I can then check what was printed out by visiting the *Messages
buffer. After I am done, I reload the official org.el[c]
file to restore the previous definition of the function.