# Polymode with python and latex-mode

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 '16 at 16:43

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

  (require 'polymode)

(defcustom pm-inner/python
(pm-hbtchunkmode "python"
:mode 'python-mode
: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)

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.
• 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 '16 at 16:28