1

I have problems getting correct latex fragments rendered. For some reason, they look different than the exported PDF, which looks fine. What is not working is the package witharrows, to place vertical arrows in equations.

Org LaTeX equation:

\begin{equation*}
\begin{WithArrows}
a\cdot{}x^{2} + b\cdot{}x + c &= 0   \Arrow[xoffset=1cm]{Test} \\
\frac{1}{2}
\end{WithArrows}
\end{equation*}

Which is rendered inline as:

Inline fragment

But the arrow should look like:

Rendered PDF

The arrow is way too small and does not stretch to the next line. Is there some way to get the same output one gets when exporting to PDF? Or is there any other similar package that would work?

What I have done so far is: Adding '("" "witharrows" t)) to my org-latex-packages-alist and setting 'imagemagick as org-preview-latex-default-process which helped to show the arrows at all. For some reason, it did not work with dvipng at all.

3
  • I'm able to reproduce this but I don't know what causes it yet.
    – NickD
    Feb 4, 2022 at 2:15
  • @NickD Do you know how to get the tex source org-mode generates for rendering the preview? Thanks. Feb 5, 2022 at 15:25
  • Too long for a comment, so I posted an incomplete answer with my findings and a couple of debugging methods I use for Org mode LaTeX preview problems.
    – NickD
    Feb 6, 2022 at 22:56

2 Answers 2

1

Thanks to @NickD I have found a solution.

The solution is to register a custom org-preview-latex-process and run pdflatex/lualatex twice for generating the latex preview image:

(setq luamagick '(luamagick :programs ("lualatex" "convert")
       :description "pdf > png"
       :message "you need to install lualatex and imagemagick."
       :use-xcolor t
       :image-input-type "pdf"
       :image-output-type "png"
       :image-size-adjust (1.0 . 1.0)
       :latex-compiler ("lualatex -interaction nonstopmode -output-directory %o %f && lualatex -interaction nonstopmode -output-directory %o %f") ;; <-- HERE
       :image-converter ("convert -density %D -trim -antialias %f -quality 100 %O")))

(add-to-list 'org-preview-latex-process-alist luamagick)
(setq org-preview-latex-default-process 'luamagick)

witharrows seems to be need to run twice to produce the correct output.

2
  • Nice find! I think the reason that lualatex works, whereas pdflatex does not is that lualatex (like xelatex) supports Unicode out of the box; pdflatex does not (there are some hacks that you can do with pdflatex to more or less deal with Unicode but they are hacks. It seems that the witharrows package needs Unicode in order to work properly. I tried with xelatex as well and that works too.
    – NickD
    Feb 7, 2022 at 4:31
  • BTW, the "running twice" is a standard LaTeX gotcha and has nothing to do with witharrows: it's just that you need to run LaTeX (any LaTex) multiple times in order to produce the files that it needs (e.g for table of contents, list of figures, glossaries, bibliographies etc). That's why things like latexmk exist.
    – NickD
    Feb 7, 2022 at 4:33
1

[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.

4
  • Many many thanks! I found a solution, see my answer. Your answer helped me a lot! Feb 7, 2022 at 0:49
  • … but I have also discovered that there are still problems. Maybe witharrows does not work that well at all? I will give up on it… Feb 7, 2022 at 0:59
  • What other problems have you encountered that cannot be resolved by the use of lualatex or xelatex?
    – NickD
    Feb 7, 2022 at 4:41
  • My goal is to write “steps” on the right side of equations, e.G. \frac{1}{x} | \times x, where the equation should be left and/or = aligned, and the “step description” should be left aligned on a second or third column. Feb 10, 2022 at 21:33

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.