# Index unmatched braces in LaTeX

I am trying to make the following work

\documentclass{book}
\usepackage{makeidx}
\makeindex
\begin{document}
\chapter{One}
\section{One}
Let us try to index the characters '()'. \index{()@()} \\
Let us try to index the character '('. \index{(@(} \\
Let us try to index the character ')'. \index{)@)} \\
Let us try to index the characters '{}'. \index{\{\}@\{\}}
Let us try to index the character '{'. \index{\{@\{}
Let us try to index the character '}'. \index{\}@\}}
\backmatter
\printindex
\end{document}


But seems like one cannot escape the characters { or } in makeidx.

The index file generated looks like this

\indexentry{()@()}{1}
\indexentry{(@(}{1}
\indexentry{)@)}{1}
\indexentry{\{\}@\{\}}{1}
\indexentry{\{@\{} Let us try to index the character '}'. \index{\}@\}{1}


\indexentry{()@()}{1}
\indexentry{(@(}{1}
\indexentry{)@)}{1}
\indexentry{\{\}@\{\}}{1}
\indexentry{\{@\{}{1}
\indexentry{\}@\}}{1}


How can I escape unmatched } and { in an index entry.

The unmatched } and { are used as I have a legacy True Type Devanagari font which has special glyphs for these characters.

The documentation of MakeIndex, page 6, says:

Remember that the argument of an \index command must always have matching braces, where the brace in a \{ or \} command counts.

So, in your case, the matching braces \{\} are indexed well but the last two lines, which do not have matching braces, are the ones creating the problem. You can easily wrap these with macros:

\documentclass{book}
\usepackage{makeidx}
\makeindex
\begin{document}
\chapter{One}
\section{One}
\def\lcurly{\{}
\def\rcurly{\}}
Let us try to index the characters '()'. \index{()@()} \\
Let us try to index the character '('. \index{(@(} \\
Let us try to index the character ')'. \index{)@)} \\
Let us try to index the characters '\{\}'. \index{\{\}@\{\}} \\
Let us try to index the character '\{'. \index{\lcurly@\lcurly} \\
Let us try to index the character '\}'. \index{\rcurly@\rcurly}
\backmatter
\printindex
\end{document}


Furthermore, the braces that you had in quotes in your text should be escaped too.

• A typo above - '\def\rcurly{\{}' should be changed to '\def\rcurly{\}}'. I cannot make the edit since an edit has to be at least 6 characters. – user22209 Jan 10 '14 at 9:15
• Sorry about that... Copy-paste is a bad habit! :-) It has been fixed, thanks egreg. – nickie Jan 10 '14 at 14:39