# How to auto indent latex code with Kile?

Is there a way for Kile to automatically correct the indentation of the latex code of an entire document?

If yes, what is combination of keys (shortcut) or the menus that I should use?

I made a search in Google but I did not find the answer. Thanks in advance!

• ctrl+a to select all text, then TAB to indent. Is this what you mean? – Nico Boni Jun 25 '13 at 16:52
• No, if do I that the entire document gets +2 (or +4) extra spaces. What I want is an automatic way to correct indentation in my latex code. Like the one available in Eclipse (per example) that corrects you Java's indentation when you press CTRL+I. – Flame_Phoenix Jun 25 '13 at 17:03
• I don't know about Kile, but you could try latexindent. – Torbjørn T. Jun 25 '13 at 17:07
• I do not want to indent Java. For that I actually use the listigns package. All I want, is to know if there is a way to auto indent my latex code. As for @Papiro, is my question not clear? – Flame_Phoenix Jun 25 '13 at 17:13
• I see now... what you're looking for is under Tools -> indentation mode. There you'll find a selection of programming languages indentation styles. Unfortunately there is indentation style for LaTeX. So if you want to have this feature you have to write your own style. To do that you should check the "Configuring With Document Variables" part in kate manual (Kile uses the kate part for the text editor). After you have your style, the command to apply the indentation is Tools -> align – Nico Boni Jun 25 '13 at 17:22

I was looking for the same thing, and could not find it. Below is a Kate script I've written to manage indentation for {} \begin and \end, handling escape characters.

The script is submitted in the Master-branch in Kate's repository. I won't update any changes here. If it checks out, they plan to include it in the next release. If you want to try it out, save what is underneath into a file called latex.js. On my system the indentation scripts are in folder: /usr/share/kde4/apps/katepart/script. After you restart Kile, you get a "Latex" indentation option in your menu. Turn that on, and all newly typed text is properly indented, for existing documents you can use "Align" from the menu. Kile's multiple line inserts (e.g. "Bulleted list") are not processed by the indenter, but you can configure the indentation for that in Kile. Multi-line user tags are not properly aligned when inserted, but you can re-align those lines or the entire doc (I have Align under alt-A as a shortcut).

Let me know if you encounter problems.

/** kate-script
* name: Latex
* author: Jeroen Vuurens <jbpvuurens at gmail.com>
* revision: 1
* kate-version: 3.4
* type: indentation
*
* Simple indentation for Latex. This script indents sections within
* \begin{ and \end{ parts, as well as within { and }. Parts after
* an \ escape character are ignored for proper handling of
* \{ \} and \%. In other cases every { is regarded as an extra
* indent, every } as a de-indent, and everything after % is comment.
*/

// specifies the characters which should trigger indent, beside the default '\n'
var triggerCharacters = "{}";
var lineStartsClose = /^(\s*\}|\s*\\end\{)/;

function indent(line, indentWidth, character) {
// not necessary to indent the first line
if (line == 0)
return -2;

var c = document.line(line); // current line

// Search backwards for first non-space, non-comment line.
var prev = line;
while (prev--) {
if (!document.line(prev).match(/^\s*\$|^%/)) {
var previousLine = document.line(prev); // previous non-space line
var prevIndent = document.firstVirtualColumn(prev);
var end = document.lineLength(prev);

var delta = 0;    // count normal openers/closers

// Walk over openers and closers in the remainder of the previous line.
if (previousLine.match(lineStartsClose))
delta++;
var escaped = false;
for (var i = 0; i < end; i++) {
var char = previousLine.charAt(i);
if (char == "\\") { // escaped affects detection of rules
escaped = true;
continue;
}
if (escaped) {
if (char == "b" && end > i + 5 && previousLine.substr(i, 6) == "begin{" ) {
delta++;
} else if (char == "e" && end > i + 3 && previousLine.substr(i, 4) == "end{" ) {
delta--;
}
} else {
if (char == "%") { // ignore rest of line as comment
break;
} if (char == "{") {
delta++;
} else if (char == "}") {
delta--;
}
}
escaped = false;
}

// now count the number of closers in the beginning of the current line.
if (c.match(lineStartsClose))
delta --;
return Math.max(0, prevIndent + delta * indentWidth);
}
}
return 0;
}

// kate: space-indent on; indent-width 2; replace-tabs on;

• Hey, could you post your script as part of the answer? It would be pretty sweet. – Flame_Phoenix Nov 8 '13 at 18:36
• By the way, there are no options for personal messages at this site. – Torbjørn T. Nov 8 '13 at 19:00
• Thanks, it is in the message now. Hope it works for you. – Jeroen Vuurens Nov 9 '13 at 13:08
• I have this by default in Kile now which is fantastic. Thanks for your work on this. I'm just wondering if there is anyway to make it play nicely with the 'bracket' notation which many tree packages use. (forest is one example but it is not the only one.) Or just to make Kile not touch the indentation in forest or other tree environments? It keeps 'realigning' everything which makes it impossible to keep track of the tree and means I have to turn the indentation feature off :(. – cfr Jun 28 '14 at 21:39
• Could you please post an example of code that is incorrectly indented? – Jeroen Vuurens Jun 29 '14 at 14:38

Kile text editor is inherited from the KDE general purpose editor (Kate), which provides an an option to set indentation according to some programming language conventions in:

Tools -> indentation mode

After the mode is set, Tools -> align is used to indent selected code.

Unfortunately there is no indentation style for LaTeX, so you can

1. write your own style (I have no idea how hard this is);

2. ask Kile/Kate devs to provide a LaTeX style (good luck with that);

3. as suggested by Torbjørn T. you can switch to a editor-agnostic solution using a package like latexindent.