The polymode manual suggests that creating new polymodes should be very easy:

Creating new polymodes typically takes a few lines of code.

I wanted to set it up to make emacs to switch to python mode inside of pythontex commands and environments. For the object oriented approach seems to be more complex to modify than just adding some delimiters to a list in my .emacs file. So I don't even see how to start and where to save apply my modifications.

So how would the equivalent setup for polymode look like as that given by JeanPierre for mmm-mode: https://emacs.stackexchange.com/a/20150/2323

  • Note that the package author is in the midst (as of February 2016) of a complete rewrite of polymode. It may be a little unstable until this transition is complete. It sounds like that may be finished shortly, but beware (and patient) in the meantime.
    – Tyler
    Feb 20, 2016 at 16:43

1 Answer 1


Following the example at https://github.com/vspinu/polymode/tree/master/modes:

  (require 'polymode)

  (defcustom pm-inner/python
    (pm-hbtchunkmode "python"
                     :mode 'python-mode
                     :head-reg  "\\\\begin{pycode}"
                     :tail-reg  "\\\\end{pycode}")
    "python typical chunk."
    :group 'innermodes
    :type 'object)

  (defcustom pm-poly/latex-python
    (pm-polymode-one "latex-python"
                     :hostmode 'pm-host/latex
                     :innermode 'pm-inner/python)
    "latex-python typical polymode."
    :group 'polymodes
    :type 'object)

  (define-polymode poly-latex+python-mode pm-poly/latex-python)
  (add-to-list 'auto-mode-alist '("\\.tex$" . poly-latex+python-mode))

It appears to work, though I only tested it briefly. This snippet can go in your .emacs, ~/.emacs.d/init.el or wherever you put your personal emacs configuration.

  • Thanks for your help. It works, but with some problems: Syntax highlighting seems to be unreliable, Indenting doesn't work as well (TAB works only for two levels). Since polymode uses indirect buffers, everything inside the pycode environment should work just as in a python buffer.
    – student
    Feb 20, 2016 at 15:43
  • It mostly works as expected for me. Syntax highlighting is as it is in a .py buffer, indenting with tab works correctly. I have emacs 24.5.1, python 3.5.1.
    – Ista
    Feb 20, 2016 at 16:28

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.