6

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.

student
  • 1,007
  • 9
  • 29

1 Answers1

6

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").

giordano
  • 3,245
  • 13
  • 19