9

When writing LaTeX using latex-mode, I often want to write "half-open intervals" in my text. For instance, [0,1), which refers to the set of all numbers x with 0 <= x < 1.

Unfortunately, these "unbalanced delimiters" seem to confuse the indentation parser, causing following text to be indented inappropriately, usually drifting to the right.

Here's an example document. The text below is the result after doing C-x h M-x indent-region. The correct behavior would be to have all of both paragraphs indented by 0 spaces.

\documentclass{article}
\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam volutpat
at dui et mattis. Here is a half open interval $[0,1)$.  Proin
  fringilla lectus nec mi tincidunt aliquet. Vestibulum fermentum eu
  neque id egestas. Here is another $[1,2)$.  Nullam et lectus
    convallis, placerat neque non, vehicula nisi.  Fusce imperdiet
    dignissim ante, eget fringilla nisi.

    Integer fermentum nunc eget purus molestie commodo. Integer
    tristique tincidunt odio, vel rutrum diam commodo quis. Fusce a
    aliquet quam. Cras suscipit est et nisl sollicitudin
    iaculis. Aliquam erat volutpat. Phasellus sapien arcu, aliquet ut
    hendrerit in, lobortis nec ante.

\end{document}

Currently I am working around this with an ugly hack: I define a LaTeX macro that expands to nothing, and write the "matching" delimiters inside an invocation of this macro. This gets the indenter back in sync.

\documentclass{article}
\newcommand{\ugh}[1]{}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam volutpat
at dui et mattis. Here is a half open interval $[0,1)$.  Proin
  fringilla lectus nec mi tincidunt aliquet. Vestibulum fermentum eu
  neque id egestas. \ugh{(]} Now on the next line we are back in sync.
Nullam et lectus convallis, placerat neque non, vehicula nisi.  Fusce
imperdiet dignissim ante, eget fringilla nisi.
\end{document}

But this is really unpleasant. Is there a better solution?

I'm using GNU Emacs 24.4.1.

  • 3
    Not answering your question, but have you considered giving AUCTeX a try? It doesn't suffer from this little bug, for one. – giordano Aug 22 '15 at 22:42
  • @giordano: I've looked at it. Its default behavior is a lot heavier than what I want. I prefer to edit LaTeX like code, and I don't really want WYSIWYG-style in-editor rendering. Maybe it's possible to turn all of that off and have a text editor experience, but it still makes AUCTeX seem like overkill. – Nate Eldredge Aug 22 '15 at 23:06
  • 6
    Which WYSIWYG-like behavior are you referring to? Only such features are preview and fold-mode and none of them is active by default (you don't even need to load corresponding packages to use AUCTeX). The main difference between AUCTeX and vanilla *TeX modes is a far superior autocompletion mechanism when inserting macros and environments. If you value autocompletion AUCTeX is the way to go. In addition, in AUCTeX you always have to edit *TeX code, there are no other possibilities, I don't know why you had this wrong impression. – giordano Aug 23 '15 at 7:48
7

Not an Emacs solution, but my LaTeX code writes such intervals as \halfopen{0}{1}, where

\newcommand\halfopen[2]{\ensuremath{[#1,#2)}}

This convention pushes the balancing issue to a place where it's unlikely to cause problems. And it's searchable :-)

  • 2
    What do you use for (0,1]? – Andrew Swann Sep 13 '15 at 12:26
  • @andrew-swann Perhaps \openhalf hehehe – Dox Oct 31 '15 at 0:59
  • @AndrewSwann \halfclosed – Norman Ramsey Mar 22 '16 at 23:23
2

The indentation function latex-indent of latex-mode sets the syntax-table to tex-latex-indent-syntax-table and calls latex-find-indent which in turn calls latex-backward-sexp-1.

The syntax of ?\( and ?\) is set to "." which stands for "punctuation". So it is no longer a delimiter.

Therefore, expressions like [0,1) result into a scan-error in latex-backward-sexp-1 which leads to the unexpected behavior.

You can avoid this by adding the following to your init file:

(eval-after-load "tex-mode"
    '(progn (modify-syntax-entry ?\( "()" tex-latex-indent-syntax-table)
             (modify-syntax-entry ?\) ")(" tex-latex-indent-syntax-table)))

But, I do not know the negative consequences of this setting since I use auctex. So, expect the worst;-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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