I'm new to Emacs, and dislike the default indentation in LaTeX mode. I want the indentation to do the following:

  • insert a \t character (not spaces)
  • be completely manual (I want to have to press Tab on each line to indent, rather than to have Emacs automatically make it match the previous line)
  • display the \t characters as 4 spaces.

Essentially, I want Emacs to behave the same way as MS Notepad, XFCE's Mousepad or Vim do by default with respect to indentation (after setting \t to be displayed as 4 spaces).

I got close to this result by using the following .emacs file (taken from this q/a):

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (setq indent-tabs-mode t
              indent-line-function 'indent-relative)))

But it causes the following (strange) problem: if I type something like

\begin{thm}[Liouville 1851]

followed by pressing Return and Tab, it indents all the way to the "1" in "1851". I suspect this has something to do with the 'indent-relative, but I am not sure how to fix it.

Do anyone know how I can make Emacs indentation work this way, without having the above problem? Any help is highly appreciated.



That's exactly what indent-relative does. I think what you want instead is tab-to-tab-stop. You'll also need your tab stops set to 4 spaces for this:

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (setq indent-tabs-mode t
              indent-line-function 'tab-to-tab-stop
              tab-width 4)))

If you're still getting automatic indentation, make sure you have turned off electric-indent-mode. You can add that to the hook as:

(add-hook 'LaTeX-mode-hook
      (lambda ()
        (setq indent-tabs-mode t
              indent-line-function 'tab-to-tab-stop
              tab-width 4
              electric-indent-mode nil)))
  • This unfortunately causes Emacs to indent new lines (regardless of whether the previous line is indented). I want Emacs to never indent a new line unless I hit tab, and when I hit tab I want Emacs to indent by exactly one \t at a time. Do you think this is possible? – Alkefanger Apr 12 '19 at 6:01
  • There's a lot of different ways to tweak indentation, it's a bit of a puzzle to find them all! I think electric-indent-mode might be the next wrinkle. – Tyler Apr 12 '19 at 13:39
  • Thanks! Disabling electric-indent-mode makes Emacs behave exactly as I want it to. – Alkefanger Apr 13 '19 at 7:35

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.