Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
1  
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
1  
@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
1  
@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
1  
@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
show 6 more comments

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
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 comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.