# Indentation in square brackets

Yet another question about indentation in AUCTeX...

I would like to be able to indent content inside square brackets [...]. It would be useful for tikz option lists, for example, or trees drawn using forest or qtree: both these packages use square brackets for their syntax, which makes it easy to scan in source code in an editor supporting paren-indentation.

Unfortunately, AUCTeX only indents the content of environments and content between braces {...}.

The forest example is also a situation where cheating by adding braces around the content we want indented won't work, because braces will escape all content from being evaluated as a tree.

Example:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
[A
[B]
[C
[D]
[E]
]
]
\end{forest}

\end{document}


Which I would like indented as:

\documentclass{article}
\usepackage{forest}

\begin{document}

\begin{forest}
[A
[B]
[C
[D]
[E]
]
]
\end{forest}

\end{document}


Is there a way?

• Just inside forest environment? – giordano Oct 15 '15 at 12:39
• @giordano Ideally, everywhere. But if you have a solution working specifically for forest, that will still be a good start. – T. Verron Oct 15 '15 at 12:52
• Well, I had an idea but wasn't really working, so I have to think more – giordano Oct 15 '15 at 14:15
• @giordano @T.Verron As some time has past since the last post: Have you found a solution to this? I've tried to adapt the solution for Lisp mentioned here but without results. More precisely, I've tried (modify-syntax-entry ?$"(]" LaTeX-mode-syntax-table) (modify-syntax-entry ?$ ")[" LaTeX-mode-syntax-table). – Timm Oct 25 '16 at 14:18
• @Timm Sadly, I still don't have any solution. The syntax table is correct: character: [ (...) syntax: (] which means: open, matches ] but it seems that AUCTeX's indentation mechanism disregards this information. – T. Verron Oct 25 '16 at 14:23

Setting LaTeX-begin-regexp and LaTeX-end-regexp does not help here because inside LaTeX-indent-level-count it searches a backslash before checking the regexp.

I got it working by modifying TeX-brace-count-line. This solution counts [] as brace {}. Compare it will original function if you want to see the difference.

(defun TeX-brace-count-line ()
"Count number of open/closed braces."
(save-excursion
(let ((count 0) (limit (line-end-position)) char)
(while (progn
(skip-chars-forward "^{}[]\\\\" limit)
(when (and (< (point) limit) (not (TeX-in-comment)))
(setq char (char-after))
(forward-char)
(cond ((eq char ?\{)
(setq count (+ count TeX-brace-indent-level)))
((eq char ?\})
(setq count (- count TeX-brace-indent-level)))
((eq char ?$) (setq count (+ count TeX-brace-indent-level))) ((eq char ?$)
(setq count (- count TeX-brace-indent-level)))
((eq char ?\\)
(when (< (point) limit)
(forward-char)
t))))))
count)))