# Indent LaTeX code NOT in an environment using latexindent

I really like how latexindent does a good job indenting most parts of my code in a document. However, there are a few instances where latexindent doesn't do what I want it to do.

These instances generally involve codes that I'd like to be indented, but are not wrapped within an environment/braces/brackets. So they end up being ignored due to lack of "identifiers", so to speak.

The following examples illustrate my problem. In the first two, stuff in <...> are not indented by latexindent. In the third one, the content of the matrix as a whole will be indented (since they are wrapped by {}), but & and \\ are not necessarily aligned (but they would be if this were in an array environment).

• LaTeX conditional statements

\if
<...>
\else
<...>
\fi

• TikZ path that spans several lines. For example, latexindent doesn't produce the following indentation:

\path
<some complicated operations>
;

• Alignment of & and \\ in TikZ \matrix command:

\matrix[matrix of nodes]{
looooong cell & looooong cell \\
short cell    & short cell    \\
};


So my question is: Is there a way to configure the .yaml file to deal with the above issues.

• As the author Thanks for trying latexindent, it's nice to have the support :) Bullets 1 and 3 are at the very top of my to do list- it's nice that someone thought of them independently :) Bullet 2 sounds tricky, but it would be a good challenge. I'll continue to develop the script, and post back with updates. For the moment, the answer is essentially: 'no, but I'm working on it :)' – cmhughes Nov 27 '13 at 4:26
• @cmhughes: Thank you for such a great script! I hope this question won't remain unanswered for long ;) – Herr K. Nov 27 '13 at 5:08
• I'm currently working with the TL folks to iron out a few kinks- once that's sorted, I'll move on to the to do list :) – cmhughes Nov 27 '13 at 5:10
• @cmhughes: And I'll be patiently waiting :) Good luck with the TL people! – Herr K. Nov 27 '13 at 5:11

The latest version of latexindent.pl is available on github; once I've done some more testing, I'll release it to ctan. This latest version addresses your question, and some more things, too- let's go through them one by one (all of this is also covered in the documentation).

# if-else-fi constructs

These can be controlled by using the constructIfElseFi field- any commands found within this field will make latexindent.pl look for \else and \fi commands.

constructIfElseFi:
ifnum: 1
ifodd: 1


You can, of course, add any other commands to this (I'll update defaultSettings.yaml with a more comprehensive list later today)

Sample before

\ifnum\radius>5
\draw[decorate,...
\fi
\fi


Sample after

\ifnum\radius>5
\draw[decorate,...
\fi
\fi


# tikz paths spanning several lines

The new version of latexindent.pl contains switches to add indentation after \item commands

indentAfterItems:
itemize: 1
enumerate: 1
list: 1


and you can have any item names you like specified in

itemNames:
item: 1
myitem: 1


As such, you can trick latexindent.pl into thinking that the tikzpicture environment has item commands called path, node, draw, and anything else by using (for example) the following code in one of your mysettings.yaml files:

indentAfterItems:
tikzpicture: 1
itemNames:
path: 1
node: 0
draw: 1


Sample before

\begin{tikzpicture}
\path
<some complicated operations>
;
\node bunch of other code
<some complicated operations>
<some complicated operations>
\draw bunch of other code
<some complicated operations>
<some complicated operations>
\end{tikzpicture}


Sample after

\begin{tikzpicture}
\path
<some complicated operations>
;
\node bunch of other code
<some complicated operations>
<some complicated operations>
\draw bunch of other code
<some complicated operations>
<some complicated operations>
\end{tikzpicture}


Note that, because I had the node set to 0, latexindent.pl does not treat it like an item command- switch it to 1, and you obtain the following:

\begin{tikzpicture}
\path
<some complicated operations>
;
\node bunch of other code
<some complicated operations>
<some complicated operations>
\draw bunch of other code
<some complicated operations>
<some complicated operations>
\end{tikzpicture}


# Alignment outside of environments

The 'brace matching' routine and the 'alignment' routine are pretty robust, but I don't want to make them fight for control- as such, to answer your last request I have added support for the following mark-up

\matrix{%
%* \begin{tabular}
1 & 2 & 3 & 4 \\
5 &   & 6 &   \\
%* \end{tabular}
}


You can use %* followed by as many spaces as you like (possibly none) together with any environment that you have specified in lookForAlignDelims. I appreciate that this solution isn't ideal, as it requires a bit of additional mark-up, but the alternative (a fight between brace matching and alignment) scares me.