1

I'm trying to write c/c++ code with comments in LaTeX.

My .emacs file has the single line

(require 'org)

My source c/c++ file has the single line

\[ \int x \]

If I'm in python mode, fortran mode, org mode (or almost any mode) then doing org-latex-preview renders my LaTeX correctly. However, if I'm in c/c++ mode I get the error "wrong argument type: stringp, nil" and I'm just left with the raw LaTeX.

The debug backtrace is

looking-at(nil)
org-element-context()
org-toggle-latex-fragment(nil)

Note that on my home machine I'm actually doing org-toggle-latex-fragment rather than org-latex-preview because the latter does not exist. I guess that's a different mystery, but the above problem applies to both my home and work setups.

My emacs lisp knowledge is almost non-existent but it seems to be failing to find anything that it classifies as LaTeX.

7
  • this is the first time I’ve learned that I can preview LaTeX in the code comments. amazing
    – shynur
    Commented Jul 14, 2023 at 11:40
  • I probably misunderstand what you are doing: can you post a complete file (in, say, python mode) and give a complete recipe of what you are doing and what the result is? AFAICT, previews only work if the file is in Org mode. They don't work for me if I have a comment in a python file (but no error) and they give me an error in a C file. My tentative conclusion is that any case where it "works" is accidental.
    – NickD
    Commented Jul 14, 2023 at 14:13
  • @NickD Thanks for looking into it. I've already posted everything though. The .emacs file is empty expect for "(require 'org)". My bob.py file is empty except for "[ \int x ]". To display the LaTeX I run either org-latex-preview or org-toggle-latex-fragment. That's it.Do you have LaTeX installed? Do you have dvipng installed (I think that's important). Commented Jul 14, 2023 at 14:53
  • 1
    When I try this it works for tmp.py, but gives an error in tmp.c, along with the recommendation to report this as a bug to the org mailing list. I'm not sure it's an actual bug though, I think org-latex-preview is meant to work in org-mode files, and it may just be a happy accident that it works in python as well? I'm not sure, but the org mailing list would be the best place to get an authoritative answer.
    – Tyler
    Commented Jul 14, 2023 at 15:06
  • My org-preview-latex-default-process is imagemagick rather than dvipng but I now see that it does work in a python mode buffer - not sure what I did earlier. I still think it's accidental, so I second @Tyler's recommendation.
    – NickD
    Commented Jul 14, 2023 at 15:15

1 Answer 1

0

You can apply AUCTeX's preview system to source code comments for C++ (or any language) by upgrading to AUCTeX 14.0.5 and adding the following to your config:

(defun my/set-TeX-master ()
  (setq TeX-master
        ;; Replace with the name of some basic tex file containing
        ;; your favorite packages and macros.
        "~/doit/preview-master.tex"))

(add-hook 'prog-mode-hook #' my/set-TeX-master)

(unless (package-installed-p 'preview-auto)
  (package-vc-install "https://github.com/ultronozm/preview-auto.el"))
(use-package preview-auto
  :after latex
  :config
  (setq preview-protect-point t)
  (setq preview-locating-previews-message nil)
  (setq preview-leave-open-previews-visible t)
  :custom
  (preview-LaTeX-command-replacements
   '(preview-LaTeX-disable-pdfoutput)))

Then use M-x preview-auto-mode to toggle (automatic) previews and (e.g.) M-x preview-clearout-buffer to get rid of them.

Your Answer

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

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