5

Suppose I enter the following code using LaTeX-environment

\begin{tikzpicture}
    \begin{pycode}
        a = 2
    \end{pycode}
\end{tikzpicture}

Then auctex automatically indents as shown above. This is not desired since it will bring error messages if I compile it via pythontex because of wrong indentation.

So I have two question:

  1. How to prevent that the pycode environment becomes indented if it is insied any other environment (for example tikzpicture)
  2. How to prevent that code inside a pycode environment is indented by auctex.

Note that this is a follow up question to Pythontex and auctex, so the solution should work together with mmm-mode.

Edit

After giordanos suggestion I made a minimal .emacs file:

(require 'package)
;; (add-to-list 'package-archives 
;;     '("marmalade" .
;;       "http://marmalade-repo.org/packages/"))
;; (add-to-list 'package-archives
;;              '("melpa" . "http://melpa.milkbox.net/packages/") t)

;; (setq package-archive-enable-alist '(("melpa" deft magit)))


(add-to-list 'load-path "~/.emacs.d/auctex")

(package-initialize)

(add-hook 'LaTeX-mode-hook (lambda ()

(add-to-list 'LaTeX-verbatim-environments "pycode")
(add-to-list 'LaTeX-indent-environment-list
        '("pycode" current-indentation))
))


(require 'python)
(defun python-indent-guess-indent-offset ()
  "Guess and set `python-indent-offset' for the current buffer."
  (interactive))

(require 'mmm-auto)
(mmm-add-group 'latex-python
 '((latex-python-envs
    :submode python-mode
    :face mmm-default-submode-face
    :front "\\\\begin{\\(pycode\\|pyverbatim\\)}\n"
    :back "\\\\end{~1}\n"
    :save-matches 1)

   (latex-python-cmds
    :submode python-mode
    :face mmm-default-submode-face
    :front "\\\\pyc?{"
    :back "}")))

;; (setq mmm-global-mode 'maybe)
;; (setq mmm-submode-decoration-level 0)
(mmm-add-mode-ext-class 'latex-mode nil 'latex-python)
;; (global-set-key [(f11)] 'mmm-parse-buffer)

(advice-add 'LaTeX-insert-environment ':after '(lambda (env) (mmm-parse-block 2)))

It works but messes up the syntax highlighting:

output 1

If I do C-c C-n, it becomes ok, but the mmm-mode get's disabled:

output 2

If I enable the mmm-mode again, I get the first picture again.

5

Add to your init file

(add-to-list 'LaTeX-verbatim-environments "pycode")
(add-to-list 'LaTeX-indent-environment-list
        '("pycode" current-indentation))

See (info "(auctex)Verbatim content") and (info "(auctex)Indenting").

  • Thanks, that solves problem 2. For the first problem, the \begin{pycode} remains indented. Any idea? – student Feb 10 '16 at 14:29
  • See the updated answer, you should add pycode also to LaTeX-verbatim-environments. – giordano Feb 10 '16 at 14:35
  • This solves the indentation problem, but messes up the syntax highlighting, here is a screenshot: anony.ws/image/J4fd – student Feb 10 '16 at 14:48
  • Uh, doesn't happen for me. Try to revert the buffer with C-c C-n. – giordano Feb 10 '16 at 14:58
  • Doesn't help. I will try it with a fresh .emacs file and report my results. – student Feb 10 '16 at 15:05

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.