2

If possible, I want to align condition statements like: \If, \Else, \For statements in between \begin{algorithmic} ... \end{algorithmic} section.

example:

\begin{algorithmic}[1]
\For{a for statement}                | for a for stament
    \If{some coding}                 |     if some coding    
        \State do something          |         do something  
    \EndIf                           |
\EndFor                              | 
\If{some coding}                     | if some coding
    \State do something              |      do something  
\EndIf 
\end{algorithmic}

Following example latex code taken from: https://tex.stackexchange.com/a/615852/127048. When I select all lines in the buffer an press (TAB indent-for-tab-command) it aligns all if else statements in the same vertical line. In python-mode indent-for-tab-command successfully align indents.

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
\For{a for statement}
    \If{some coding}
    \State do something
    \ElsIf{another condition}
        \For{some condition}
            \If{another statement}
                \State do something
            \EndIf
        \EndFor
    \ElsIf{problem is here}
        \State whatever
    \EndIf
\EndFor
\end{algorithmic}
\end{algorithm}
\end{document}

After TAB indent-for-tab-command the buffer is converted into:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Main D Algorithm}\label{alg:cap}
\begin{algorithmic}[1]
    \For{a for statement}
    \If{some coding}
    \State do something
    \ElsIf{another condition}
    \For{some condition}
    \If{another statement}
    \State do something
    \EndIf
    \EndFor
    \ElsIf{problem is here}
    \State whatever
    \EndIf
    \EndFor
\end{algorithmic}
\end{algorithm}
\end{document}
2
  • Does emacs.stackexchange.com/questions/35908/… help?
    – NickD
    Commented Jan 4, 2022 at 18:48
  • @NickD I have applied the answer on the link, but it does not make any indent changes. add entry as Environmet: algorithmic Function: current-indentation
    – alper
    Commented Jan 7, 2022 at 9:52

3 Answers 3

1
+50

Add the following to your init file:

(with-eval-after-load 'latex
    (add-to-list 'LaTeX-indent-environment-list '("algorithmic" current-indentation)))

That's the equivalent of the customization, but maybe you got the customization wrong somehow.

You should check the value of LaTeX-indent-environment-list and make sure that there is an algorithmic entry in it:

Value:
(("algorithmic" current-indentation)
 ("verbatim" current-indentation)
 ("verbatim*" current-indentation)
 ("filecontents" current-indentation)
 ("filecontents*" current-indentation)
 ("tabular" LaTeX-indent-tabular)
 ("tabular*" LaTeX-indent-tabular)
 ("align" LaTeX-indent-tabular)
 ("align*" LaTeX-indent-tabular)
 ("array" LaTeX-indent-tabular)
 ("eqnarray" LaTeX-indent-tabular)
 ("eqnarray*" LaTeX-indent-tabular)
 ("displaymath")
 ("equation")
 ("equation*")
 ("picture")
 ("tabbing"))

You should also check that LaTeX-indent-environment-check is set to t. That is the default, but you never know. In fact, you should probably do both of these checks in your current setup and maybe that will point out what you did wrong in the customization.

After restarting Emacs and opening the file, the indentation of the contents of an algorithmic environment should not be affected by TAB.

I have a feeling that even though this answers the current question, it is not going to be satisfactory in the long run, but I'll let you experiment and ask more questions about it later.

8
  • 1
    TAB affect is removed. I think I have to align inner If indents manually, right? It wouldn't add 4 space before the inner Ifs like its done in python. For example, If i provide the converted version (second version algorithm on my question), and press TAB, the indent alignment wouldn't be done automatically
    – alper
    Commented Jan 9, 2022 at 11:34
  • @alper, if you do not have imposed constraints, you can use the package algorithm2e where TAB works when typing. If forced to use algorithm, note that other editors (TeXShop, Vim, Vscode) can be used, TAB works when typing.
    – Ian
    Commented Jan 9, 2022 at 12:40
  • @Ian I have tried algorithm2e but when I press TAB all the lines moved into same indent level like I asked on my question
    – alper
    Commented Jan 9, 2022 at 13:51
  • @alper, take care, algorithm2e has a different syntax - you have an algorithm2e.pdf documentation in your LaTeX installation. One more note: your observed behaviour in the second version algorithm snippet is as requested by the package algorithm and is imposed by the style file algorithm.el in your auctex/style folder.
    – Ian
    Commented Jan 9, 2022 at 14:12
  • @Ian sorry I didn't get it. As I understand if I use algorithmic environment , indent spaces alignment would not be applied so instead I have to use algorithm2e?
    – alper
    Commented Jan 10, 2022 at 1:06
1

AUCTeX is able to indent conditionals better with this change. At the same time, the code in algpseudocode.el was adjusted to take advantage of this feature.

If I take your example, indent the line with \caption and \begin{algorithmic}[1] manually by hitting TAB, then put point before \For and hit M-q, I get this:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\begin{document}
\begin{algorithm}
  \caption{Main D Algorithm}\label{alg:cap}
  \begin{algorithmic}[1]
    \For{a for statement}
      \If{some coding}
        \State do something
      \ElsIf{another condition}
        \For{some condition}
          \If{another statement}
            \State do something
          \EndIf
        \EndFor
      \ElsIf{problem is here}
        \State whatever
      \EndIf
    \EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

Just wait for next AUCTeX release from ELPA which should happen soon, and you don't need to rely on external tools anymore.

0

I found a solution using https://github.com/raxod502/apheleia and latexindent.pl together.

localSettings.yaml for latexindent.pl its issue:

defaultIndent: "    "
specialBeginEnd:
  If:
    begin: '\\If'
    middle: '\\ElsIf'
    end: '\\EndIf'
    lookForThis: 1
  specialBeforeCommand: 1

Installing latexindent.pl:

mkdir ~/bin && cd ~/bin
git clone https://github.com/cmhughes/latexindent.pl.git
chmod +x latexindent.pl/latexindent.pl
export PATH=~/bin:$PATH

latexindent: should be executable and its path exist in PATH

#!/bin/bash
cp $HOME/localSettings.yaml .
~/bin/latexindent.pl/latexindent.pl -l $1

apheleia will run latexindent automatically when a .tex file is opened.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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