# Indentation in AUCTEX and algorithmic package

When working with EMACS+AUCTEX I encountered the problem of proper indentation when using the algorithmic package.

In this MWE:

\documentclass[12pt]{article}
\usepackage{algorithmic}
\begin{document}
\begin{algorithmic}
\IF {list.first $\neq x_{min}$}
\STATE bla
\STATE bla 2
\ENDIF
\end{algorithmic}
\end{document}


I have a correct indentation, but hitting C-c C-q C-e in the algorithm's environment, produces:

\begin{algorithmic}
\IF {list.first $\neq x_{min}$} \STATE bla \STATE bla 2
\ENDIF
\end{algorithmic}


When trying to add a nested if, I fail to obtain good indentation completely.

Does anyone know the problem? Possible solutions?

-
Simple answer: don't do C-c C-q C-e –  Seamus May 23 '11 at 13:44
@Seamus: but then, in the case of nested statements, I cannot get the proper indentation at all. TAB doesn't do the trick for some reason. –  Dror May 23 '11 at 13:46
@Dror emacs indentation is smart, but it isn't perfect. Don't expect it to do everything for you. You may have to manually add spaces. Also, this seems to be a question about emacs, not about tex or friends. –  Seamus May 23 '11 at 14:31
@Seamus: I can easily imagine that the answer is that there's no solution. This question however directly relates to AUCTeX (and the way it interacts with TeX related packages) which is in turn a friend of TeX. –  Dror May 23 '11 at 14:35
@Dror but the problem is entirely on the emacs end and any solution would involve fixing emacs not fixing anything TeX related. So I maintain that this is off topic here. –  Seamus May 23 '11 at 14:48

Emacs is really smart about indentation. Unfortunately, you have to be even smarter to get it to do something new.

The easiest thing to do in this case is to customize the variable LaTeX-paragraph-commands. By default, this is empty. If you add IF and STATE, then C-c C-q C-e should respect your wishes and not jumble everything up together.

To do this, call M-x customize-variable LaTeX-paragraph-commands, click the INS button and add IF in the string box, click INS again and add STATE to a second string box. The click apply and save.

Getting proper indentation of nested IF statements is going to be trickier. One approach is to customize the variables LaTeX-begin-regexp and LaTeX-end-regexp. The first defaults to begin\b, and the second to end\b. If you extend them to begin\b\|IF\b and end\b\|ENDIF\b respectively, Auctex will indent your IF blocks how you like.

To do this, call M-x customize-variable LaTeX-begin-regexp and change begin\b to begin\b\|IF\b, and then click apply and save. Do the same for LaTeX-end-regexp, adding the \|ENDIF\b bit.

However, if you don't close your IF blocks with an ENDIF, then the indentation doesn't return to normal when you leave your algorithmic environment. To get Auctex to properly handle un-ended IF blocks, you need to cook up a custom indentation function. I don't know how to do that yet, but may return when I do.

-
Can you please add the code I should add to my .emacs in order to append what you suggested to my LaTeX-paragraph-commands variable? –  Dror Jul 25 '12 at 7:35
You can do this with the customization tools, you don't need to modify your .emacs. See above. –  Tyler Jul 25 '12 at 11:36
@Tyler Is there a way to have it fix the indentation for \ELSE as well? –  Jesus Ramos Nov 19 '12 at 3:38

Add algorithmic to LaTeX-indent-environment-list, no need to specify any function. Then it can indent it, but cannot do rebreaking of the lines.

While you are add it, might be an idea to add tikzpicture and scope to the list as well.

M-x customize-variable LaTeX-indent-environment-list

-then use the interface to add a new entry.

-
That will fix the problem of combining lines together (and nicely!), but it won't give provide automatic indentation of nested if statements, which is what I was referring to when I suggested you'd need a new indentation function. –  Tyler Jul 26 '12 at 20:26

This answer is for those who are interested in adding the code manually to the .emacs file. I ran into to one issue that stumped me, so I think future readers may benefit from this. The following code suffices to fix indenting for \IF, \FOR, and \LOOP, as well as keeping each \STATE on it's own line.

(defun my-latex-mode-setup ()
"Redefines and updates variables for various customizations."
(setq LaTeX-paragraph-commands
'("IF" "STATE" "LOOP" "FOR"))
(setq LaTeX-begin-regexp
(concat "begin\\b" "\\|IF\\b" "\\|LOOP\\b" "\\|FOR\\b"))
(setq LaTeX-end-regexp
(concat "end\\b" "\\|ENDIF\\b" "\\|ENDLOOP\\b" "\\|ENDFOR\\b"))
(setq LaTeX-paragraph-commands-regexp (LaTeX-paragraph-commands-regexp-make)))


Each of the setq calls redefines the same variables that were set with Customize in the other answer, except the last one that modifies LaTeX-paragraph-commands-regexp.
This last setq modifies an internal variable that is generated by a call to LaTeX-paragraph-commands-regexp-make which combines the additional commands in the list LaTeX-paragraph-commands with an internal list of commands defined in the variable LaTeX-paragraph-commands-internal.
Updating the variable LaTeX-paragraph-commands-regexp is necessary because the original call to LaTeX-paragraph-commands-regexp-make happens before the hook in your .emacs file is called. Without it LaTeX-paragraph-commands-regexp does not contain the new commands defined in LaTeX-paragraph-commands.
Last, for those not familiar with emacs regular expressions, the double back slashes \\ are important to make sure that the strings are correctly read as regular expressions.